LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash - pull out variables from a | deliminated string (https://www.linuxquestions.org/questions/programming-9/bash-pull-out-variables-from-a-%7C-deliminated-string-621403/)

elinenbe 02-15-2008 12:05 PM

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

ta0kira 02-15-2008 12:39 PM

What about this:
Code:

#!/bin/bash

input="PDFJ37455JSJDF| 156|BACON AND EGGS TRANSIT |2005-11-01 02:20:00| 7300|4051 | 0"

echo "$input" | tr "|" "\n" | while read Name && read Type && read Food && read Time; do
        echo Name: $Name
        echo Type: $Type
        echo Food: $Food
        echo Time: $Time
done

ta0kira

unSpawn 02-15-2008 12:57 PM

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

elinenbe 02-15-2008 01:30 PM

Rock on you guys. Both work rather perfectly!

Thanks!

angrybanana 02-15-2008 01:35 PM

Quote:

Originally Posted by unSpawn (Post 3058329)
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

I was trying something similar to that (using IFS with read) but I couldn't figure out how to get rid of the whitespace in " 156". Does bash have an easy way of doing that?

elinenbe 02-15-2008 01:58 PM

I actually have the same problem. Working on something now...

ta0kira 02-15-2008 02:36 PM

Add this:
Code:

str="$(echo "$str" | tr " " "\0")"
ta0kira

elinenbe 02-15-2008 02:41 PM

or this:

Code:

# remove leading white spaces
str=${str## }
# remove trailing white spaces
str=${str%% }


radoulov 02-15-2008 05:44 PM

With zsh:

Code:

$ s="PDFJ37455JSJDF| 156|BACON AND EGGS TRANSIT |2005-11-01 02:20:00| 7300|4051 | 0"
$ a=(${${${(s:|:)s}# }% })                                                         
$ print -$a[2]-
-156-


ghostdog74 02-15-2008 07:47 PM

Quote:

Originally Posted by angrybanana (Post 3058384)
I was trying something similar to that (using IFS with read) but I couldn't figure out how to get rid of the whitespace in " 156". Does bash have an easy way of doing that?

how about this
Code:

# IFS="|"
# echo ${str}
PDFJ37455JSJDF  156 BACON AND EGGS TRANSIT  2005-11-01 02:20:00  7300 4051  0
# IFS="| "
# echo ${str}
PDFJ37455JSJDF 156 BACON AND EGGS TRANSIT 2005-11-01 02:20:00 7300 4051 0


angrybanana 02-15-2008 08:40 PM

Quote:

Originally Posted by ghostdog74 (Post 3058705)
how about this
Code:

# IFS="|"
# echo ${str}
PDFJ37455JSJDF  156 BACON AND EGGS TRANSIT  2005-11-01 02:20:00  7300 4051  0
# IFS="| "
# echo ${str}
PDFJ37455JSJDF 156 BACON AND EGGS TRANSIT 2005-11-01 02:20:00 7300 4051 0


That splits "BACON AND EGGS" into separate fields. Guess string substitution is the best way to do it.

ghostdog74 02-15-2008 11:36 PM

Quote:

Originally Posted by angrybanana (Post 3058748)
That splits "BACON AND EGGS" into separate fields. Guess string substitution is the best way to do it.

ah.yes.my bad for missing that one.

Code:

# set -- $str
# echo $3
BACON AND EGGS TRANSIT



All times are GMT -5. The time now is 10:45 AM.