Split function (bash script)
Hi,
Is there a split function like the one in perl, but for a bash script? Here's the perl for what I want to achieve: ($user,pass) = split(/\|/, $args); The variables are: args = "theuser|thepass"; And I want to get: $user = "theuser"; $pass = "thepass"; I want to do the same thing as I have done above, but in a bash script :) Any ideas how? Thanks, - Nick |
could you use awk maybe? eg
$USER=`echo $ARGS | awk -F"|" '{print $1}'` |
Perfect...
Hi,
Yup... used: user=`echo $args | awk -F"|" '{print $1}'` pass=`echo $args | awk -F"|" '{print $2}'` Perfect... Thanks :) - Nick |
Chomp function?
Hullo,
Also, is ther achomp function in bash scripts? Chomp removes the newline from the end of a string... Thanks, - Nick |
Code:
#!/bin/sh Gary |
Use built in
Its an old topic .. but still there is built ins in most shells
arg="USER|PASSWORD" echo ${arg/*|/ } echo ${arg/|*/ } PASSWORD USER |
Quote:
Please don't dig up very old threads like this. If you have an issue, search the forums first, and if that doesn't help, then by all means create a new thread for your issue. Thank you for your understanding, Josh |
Actually, in cases like this I think what Hogdahl did was acceptable. The topic in question really isn't time sensitive and someone finding this thread via Google could certainly benefit from the answer provided.
--jeremy |
Quote:
|
In the spirit of adding to the discussion then, this page has an excellent rundown of bash's built-in string manipulation options:
http://mywiki.wooledge.org/BashFAQ/100 |
This is easier. just replace whats in the []s in the sed command
In this case i was splitting an IP. the "period" has to go first in the []s. I needed to escape the / so i had to put \/. But you could split on anything with sed. tr will also split but not very well here since i needed . and / Code:
$ IP=( `echo 192.168.1.0/24 | sed -e 's/[.\/]/ /g'` ) |
There's no need to use sed, as long as you're using bash or a similar advanced shell.
Code:
IP="192.168.1.0/24" It's all covered in the link I posted above. |
All times are GMT -5. The time now is 07:09 PM. |