开发者

Processing comma separated values using a loop in Unix

I have a file which contains values like:

CA, PA, NY
ND, MO, MI

I need to process these values one by one. The flowchart will be as follows:

Enter loop -> Process CA; Process PA; Process NY -> Other commands -> Process ND; Process MO; Process MI -> End;

Is this possible using 开发者_C百科shell scripting?


I can think of two obvious ways. If you'll have access to the tr utility (standard on any UNIX/Posix host) then you could tr ',' '\n' < "$your_data_file" | while read each; do $process $each; done If not then you could probably still use the shell's IFS (inter-field separator) using something like: cat "$your_data_file"| { IFS=','; while read line; do for each in $line; do echo $each; done; done; } (Note you can use {} grouping or () for a subshell ... they are effectively the same in this example).

Note you might have some extraneous whitespace in $each which you might want to filter out separately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜