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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-24-2001, 06:40 AM
|
#1
|
Member
Registered: Aug 2001
Location: UK
Distribution: Mandrake 8.0
Posts: 33
Rep:
|
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
|
|
|
10-24-2001, 07:18 AM
|
#2
|
LQ Newbie
Registered: Jul 2001
Location: UK
Distribution: RH 7.1
Posts: 24
Rep:
|
could you use awk maybe? eg
$USER=`echo $ARGS | awk -F"|" '{print $1}'`
|
|
|
10-24-2001, 07:25 AM
|
#3
|
Member
Registered: Aug 2001
Location: UK
Distribution: Mandrake 8.0
Posts: 33
Original Poster
Rep:
|
Perfect...
Hi,
Yup... used:
user=`echo $args | awk -F"|" '{print $1}'`
pass=`echo $args | awk -F"|" '{print $2}'`
Perfect...
Thanks
- Nick
|
|
|
10-24-2001, 07:42 AM
|
#4
|
Member
Registered: Aug 2001
Location: UK
Distribution: Mandrake 8.0
Posts: 33
Original Poster
Rep:
|
Chomp function?
Hullo,
Also, is ther achomp function in bash scripts?
Chomp removes the newline from the end of a string...
Thanks,
- Nick
|
|
|
10-24-2001, 09:02 PM
|
#5
|
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821
Rep: 
|
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
|
|
|
09-28-2011, 09:45 PM
|
#6
|
LQ Newbie
Registered: Sep 2011
Posts: 2
Rep: 
|
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
|
|
|
09-28-2011, 09:56 PM
|
#7
|
LQ Guru
Registered: Apr 2005
Location: /dev/null
Posts: 5,818
|
Quote:
Originally Posted by Hogdahl
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
|
|
|
09-29-2011, 03:00 PM
|
#8
|
root 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,626
|
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.
|
09-29-2011, 05:44 PM
|
#9
|
LQ Guru
Registered: Apr 2005
Location: /dev/null
Posts: 5,818
|
Quote:
Originally Posted by jeremy
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.
|
|
|
09-30-2011, 12:20 AM
|
#10
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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
|
|
|
03-09-2012, 09:08 PM
|
#11
|
Member
Registered: Jun 2008
Location: Dallas, TX
Distribution: Alpine and Void
Posts: 114
Rep:
|
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
|
|
|
03-10-2012, 06:53 AM
|
#12
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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 02:24 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|