LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Split function (bash script) (https://www.linuxquestions.org/questions/programming-9/split-function-bash-script-7880/)

NiM 10-24-2001 06:40 AM

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

GOLDF1NG3R 10-24-2001 07:18 AM

could you use awk maybe? eg

$USER=`echo $ARGS | awk -F"|" '{print $1}'`

NiM 10-24-2001 07:25 AM

Perfect...
 
Hi,

Yup... used:

user=`echo $args | awk -F"|" '{print $1}'`
pass=`echo $args | awk -F"|" '{print $2}'`

Perfect...

Thanks :)

- Nick

NiM 10-24-2001 07:42 AM

Chomp function?
 
Hullo,

Also, is ther achomp function in bash scripts?

Chomp removes the newline from the end of a string...

Thanks,

- Nick

crabboy 10-24-2001 09:02 PM

Code:

#!/bin/sh

VAR="cool|dude"

ONE=`echo $VAR | cut -d'|' -f1`
TWO=`echo $VAR | cut -d'|' -f2`

echo "One: [$ONE]"
echo "Two: [$TWO]"

Use $1 in place of VAR if the input is a single argument.

Gary

Hogdahl 09-28-2011 09:45 PM

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

corp769 09-28-2011 09:56 PM

Quote:

Originally Posted by Hogdahl (Post 4485003)
Its an old topic .. but still there is built ins in most shells
arg="USER|PASSWORD"
echo ${arg/*|/ }
echo ${arg/|*/ }

PASSWORD
USER

Hello,

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

jeremy 09-29-2011 03:00 PM

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

corp769 09-29-2011 05:44 PM

Quote:

Originally Posted by jeremy (Post 4485820)
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

Point taken... I just find it odd, since the last post was back in 2001.

David the H. 09-30-2011 12:20 AM

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

stoggy 03-09-2012 09:08 PM

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'` )


$ echo ${IP[0]}
192
$ echo ${IP[1]}
168
$ echo ${IP[2]}
1
$ echo ${IP[3]}
0
$ echo ${IP[4]}
24


David the H. 03-10-2012 06:53 AM

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"

IP=( ${IP//[.\/]/ } )

echo "${IP[0]}"

For more advanced manipulation, you can use the IFS environment variable to control the word-splitting. Among other techniques.

It's all covered in the link I posted above.


All times are GMT -5. The time now is 07:09 PM.