Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I should write a shell script able to parse a comma separated string.
An examle:
x,x,x,x,xThe 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

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,bufordawk '{ print $NF }' comma.txt | tr "," "\012"
pippo
pluto
paperino
bob
mary
bart
jim
bufordRedirect 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

looks homeworky to me. If instructor required "pure shell" (no awk,sed ,tr ) and file contains
pippo,pluto,paperino
bob,mary,bart,jim,bufordyou could submit:
Script < file
where Script is:IFS=,
while read a
do
for f in $a
do
echo $f
done
donefor production, David's is the best way.

![]() |
![]() |
![]() |

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