bash - pull out variables from a | delimited string
I have a string such as:
PDFJ37455JSJDF| 156|BACON AND EGGS TRANSIT |2005-11-01 02:20:00| 7300|4051 | 0 I would like to put each value into a separate variable (or array) to be used later on. eg: Name=PDFJ37455JSJDF Type=156 Food=BACON AND EGGS TRANSIT Time=2005-11-01 02:20:00 etc... I can't figure out a nice way to do this (though I do have some crappy ways). Any help would be appreciated. Thanks, Eric |
What about this:
Code:
#!/bin/bash |
str='PDFJ37455JSJDF| 156|BACON AND EGGS TRANSIT |2005-11-01 02:20:00| 7300|4051 | 0'
IFS=$'|\t\n' array=(${str}) ]$ echo ${array[2]} BACON AND EGGS TRANSIT |
Rock on you guys. Both work rather perfectly!
Thanks! |
Quote:
|
I actually have the same problem. Working on something now...
|
Add this:
Code:
str="$(echo "$str" | tr " " "\0")" |
or this:
Code:
# remove leading white spaces |
With zsh:
Code:
$ s="PDFJ37455JSJDF| 156|BACON AND EGGS TRANSIT |2005-11-01 02:20:00| 7300|4051 | 0" |
Quote:
Code:
# IFS="|" |
Quote:
|
Quote:
Code:
# set -- $str |
All times are GMT -5. The time now is 10:45 AM. |