LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-15-2008, 12:05 PM   #1
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Rep: Reputation: 15
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

Last edited by elinenbe; 02-15-2008 at 12:36 PM.
 
Old 02-15-2008, 12:39 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 02-15-2008, 12:57 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,987
Blog Entries: 54

Rep: Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742
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
 
Old 02-15-2008, 01:30 PM   #4
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Original Poster
Rep: Reputation: 15
Rock on you guys. Both work rather perfectly!

Thanks!
 
Old 02-15-2008, 01:35 PM   #5
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by unSpawn View Post
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?
 
Old 02-15-2008, 01:58 PM   #6
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Original Poster
Rep: Reputation: 15
I actually have the same problem. Working on something now...
 
Old 02-15-2008, 02:36 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Add this:
Code:
str="$(echo "$str" | tr " " "\0")"
ta0kira
 
Old 02-15-2008, 02:41 PM   #8
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Original Poster
Rep: Reputation: 15
or this:

Code:
# remove leading white spaces
str=${str## }
# remove trailing white spaces
str=${str%% }
 
Old 02-15-2008, 05:44 PM   #9
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
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-
 
Old 02-15-2008, 07:47 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by angrybanana View Post
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
 
Old 02-15-2008, 08:40 PM   #11
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 02-15-2008, 11:36 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by angrybanana View Post
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

Last edited by ghostdog74; 02-15-2008 at 11:38 PM.
 
  


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
kdevelop 3.4 string variables impeteperry Ubuntu 5 07-07-2007 09:01 PM
Java Question - referencing variables by name as a string qscomputing Programming 3 01-31-2007 04:49 AM
string with content of variables chloraldo Programming 4 08-18-2006 09:34 AM
how to divide two string variables user_linux Programming 3 06-20-2005 04:54 PM
String variables in DDD Qt debugger impeteperry Linux - Software 0 06-12-2005 07:57 AM


All times are GMT -5. The time now is 06:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration