BASH: Split a string without 'cut' or 'awk'
April 10th, 2006 Posted in Bash
For a little test script I'm writing I needed to split a line on a ';' but preservere the "s and 's, something that echo doesn't like to do. Digging deeper into the bash docs I see that there are some handy string handling functions.
#!/bin/bash
line='this "is" a command;this "is" a pattern'
COMMAND=${line%;*}
PATTERN=${line#*;}
echo $COMMAND
echo $PATTERN
And the output would be:
this "is" a command
this "is" a pattern
June 15th, 2006 at 9:15 am
This one should win a price… I works great and is noce and simple…
July 23rd, 2006 at 11:55 pm
very nice.
Tnx for sharing …
August 4th, 2006 at 9:28 am
Brilliant. Solved a big one for me
August 23rd, 2006 at 2:05 pm
you rock.
August 24th, 2006 at 7:34 pm
EXCELENT, FANTASTIC, DONE
October 25th, 2006 at 12:06 am
Just what I needed!
November 14th, 2006 at 6:34 pm
very nice, used it for a simply archive handling bash script i wrote and put on my site
February 6th, 2007 at 12:25 pm
The only problem is that it doesn't deal with multiple instancse of the separator character. What then?
February 13th, 2007 at 12:27 am
nihilist,
Then you break out some perl or sed regex action and break it apart accordingly.
February 22nd, 2007 at 6:04 pm
Outstanding!
Very usefull when splitting configuration files like:
PARAM=VALUE
PARAM=VALUE
.
.
.
Congratulations!
February 26th, 2007 at 6:09 pm
This is so much more elegant than a nasty sed/awk expression. Thanks for improving my code!
April 17th, 2007 at 8:28 am
@nihilist:
If you have multiple separator characters, like this:
string1;string2;string3
then you can split it this way:
original='string1;string2;string3'
part1=${original%%;*}; rest=${original#*;}
part2=${rest%%;*}; rest=${rest#*;}
part3=${rest%%;*};
This should work for arbitrary number of parts.
June 7th, 2007 at 1:25 am
very nice … thanks
How to do if I have 3 tags (or more) like
line=’this “is” a command;this “is” a pattern;this "is" a bla’
COMMAND=${line%;*}
PATTERN=${line#*;}
echo $COMMAND
echo $PATTERN
June 7th, 2007 at 1:39 am
nice job! i'll use it for my scripting. thanks!
June 11th, 2007 at 1:04 pm
@oli:
I'd just use 'cut -d";" -f1' (and 2 and 3) to get each element then.
COMMAND=$(echo $line | cut -d";" -f1)
PATTERN=$(echo $line | cut -d";" -f2)
BLAH=$(echo $line | cut -d";" -f3)
I am sure there is a way to split it properly, but I don't recall how.
June 20th, 2007 at 4:35 am
Great! I use it to test if a site is changed (blackboard is down so when it changes I want to know)
July 19th, 2007 at 10:10 pm
Thanks for the way here.
And I would like to share the way for 3 tag line, I tested this and used it.
COMMAND=${line%;*;*}
PATTERN=${line#*;}
PATTERN=${PATTERN%;*}
BLAH=${line#*;*;}
November 5th, 2007 at 3:03 pm
Splitting a string in bash…
I found this little trick to split a string in bash, so I'm sharing. Maybe it's obvious to some people, but I don't do a lot of shell scripting, so it was helpful to me.
http://anton.lr2.com/archives/2006/04/10/bash-split-a-string-wit...
December 30th, 2007 at 4:09 am
Than you very much for this hint! It saved me from some tricky shell scripting
February 10th, 2008 at 8:20 am
If I am right, this just works if you know how much delimiter are in the string you wish to split. Nice would be if this should work without knowing this…
cheers — jerik
March 17th, 2008 at 7:43 am
Very elegant, you are no bookmarked!
Many thanks
June 6th, 2008 at 10:00 am
Thanks, you saved me lot of time and valuable bytes!
August 24th, 2008 at 1:57 am
You sir are Awesome! Not that this makes me enjoy bash any more(I prefer real programming languages), but this is much more understandable than that sed/awk stuff.
Bookmarked, Stumbled, Saved.
September 1st, 2008 at 2:53 am
This cool tip was a lot helpful one…..I did a srch for my problem of interpreting a .CSV and this works like a charm
September 5th, 2008 at 9:39 am
#!/bin/bash
# Split the command line argument on the colon character.
IFS=":"; declare -a Array=($*)
echo "Array[0]=${Array[0]}"
echo "Array[1]=${Array[1]}"
echo "Array[2]=${Array[2]}"
echo "Array[3]=${Array[3]}"
September 5th, 2008 at 9:46 am
Jim,
Thanks! I hadn't known about the IFS internal variable.
September 5th, 2008 at 4:25 pm
Go to:
http://www.linuxquestions.org/questions/programming-9/bash-shell-script-split-array-383848/?posted=1#post3270996
And see:
IP=1.2.3.4; IP=(${IP//./ }); Rev=${IP[3]}.${IP[2]}.${IP[1]}.${IP[0]}