LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2001, 06:40 AM   #1
NiM
Member
 
Registered: Aug 2001
Location: UK
Distribution: Mandrake 8.0
Posts: 33

Rep: Reputation: 15
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
 
Old 10-24-2001, 07:18 AM   #2
GOLDF1NG3R
LQ Newbie
 
Registered: Jul 2001
Location: UK
Distribution: RH 7.1
Posts: 24

Rep: Reputation: 15
could you use awk maybe? eg

$USER=`echo $ARGS | awk -F"|" '{print $1}'`
 
Old 10-24-2001, 07:25 AM   #3
NiM
Member
 
Registered: Aug 2001
Location: UK
Distribution: Mandrake 8.0
Posts: 33

Original Poster
Rep: Reputation: 15
Perfect...

Hi,

Yup... used:

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

Perfect...

Thanks

- Nick
 
Old 10-24-2001, 07:42 AM   #4
NiM
Member
 
Registered: Aug 2001
Location: UK
Distribution: Mandrake 8.0
Posts: 33

Original Poster
Rep: Reputation: 15
Chomp function?

Hullo,

Also, is ther achomp function in bash scripts?

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

Thanks,

- Nick
 
Old 10-24-2001, 09:02 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
Old 09-28-2011, 09:45 PM   #6
Hogdahl
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
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
 
Old 09-28-2011, 09:56 PM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by Hogdahl View Post
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
 
Old 09-29-2011, 03:00 PM   #8
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,626

Rep: Reputation: 4177Reputation: 4177Reputation: 4177Reputation: 4177Reputation: 4177Reputation: 4177Reputation: 4177Reputation: 4177Reputation: 4177Reputation: 4177Reputation: 4177
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
 
1 members found this post helpful.
Old 09-29-2011, 05:44 PM   #9
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by jeremy View Post
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.
 
Old 09-30-2011, 12:20 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 03-09-2012, 09:08 PM   #11
stoggy
Member
 
Registered: Jun 2008
Location: Dallas, TX
Distribution: Alpine and Void
Posts: 114

Rep: Reputation: 22
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
 
Old 03-10-2012, 06:53 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script split array robertngo Programming 13 06-19-2011 11:01 PM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
PERL Q, split function and reg expressions amytys Programming 1 12-08-2004 10:54 AM
search function (bash script) LYK Programming 2 05-27-2004 10:51 AM
C built-in function for a Bash script Linh Programming 3 04-23-2004 09:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration