Computing.Net > Forums > Unix > scripting string parsing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

scripting string parsing

Reply to Message Icon

Name: Irene
Date: August 18, 2003 at 06:42:38 Pacific
OS: Win 2000
CPU/Ram: P4/256 MB
Comment:

Hi all,
I should write a shell script able to parse a comma separated string.
An examle:
x,x,x,x,x

The problem is that the number of different 'x' is not known so how can I get avery separated x string ?
Then I should write each x on a different line of a text file.
Does a unique command exist or a script is necessary ?


I would appraciate very much any help!
Thank You,
Irene



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: August 18, 2003 at 11:45:15 Pacific
Reply:

You could parse it with awk.

echo "x,x,x,x,x" | awk -F"," '{ print NF }'


0

Response Number 2
Name: David Perry
Date: August 19, 2003 at 04:23:24 Pacific
Reply:

If the field count is unimportant but you just want to deal with the values of each field, the tr command could be used to translate commas to carriage returns.

cat comma.txt
File Server = pippo,pluto,paperino
File Server = bob,mary,bart,jim,buford

awk '{ print $NF }' comma.txt | tr "," "\012"
pippo
pluto
paperino
bob
mary
bart
jim
buford

Redirect stdout to append to a file if you want to save the results. You can process many files in a for loop.

for file in *.log ; do
awk '{ print $NF }' $file | tr "," "\012" >> outfile
done


0

Response Number 3
Name: ArnoldF
Date: August 19, 2003 at 07:22:36 Pacific
Reply:

looks homeworky to me. If instructor required "pure shell" (no awk,sed ,tr ) and file contains
pippo,pluto,paperino
bob,mary,bart,jim,buford

you could submit:
Script < file
where Script is:

IFS=,
while read a
do
for f in $a
do
echo $f
done
done

for production, David's is the best way.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: scripting string parsing

Need Help with KornShell script www.computing.net/answers/unix/need-help-with-kornshell-script/5978.html

UNIX FTP Scripting www.computing.net/answers/unix/unix-ftp-scripting/5999.html

sed/awk - line feeds? www.computing.net/answers/unix/sedawk-line-feeds/5237.html