#!/bin/sh
for line in `cat Results.txt`
do
FEILD1=`echo $line |awk -F"|" '{print $1}'`
FEILD2=`echo $line |awk -F"|" '{print $2}'`
FEILD3=`echo $line |awk -F"|" '{print $3}'`
FEILD4=`echo $line |awk -F"|" '{print $4}'`
echo "$FEILD1 $FIELD2 $FIELD3 $FIELD4"
done
Hi All,
I am trying to execute the above script. Ideally I shud get all that before the fisrt '|' in $1 anything before secon pipe and after first pipe in $2 etc. But the echo of my result is not as expected could you please help.
Thats great this works thanks for the help but still not sure why it didnt work in my for loop. Anyways thanks so much for the help
---------- Post updated at 05:57 AM ---------- Previous update was at 04:54 AM ----------
Thanks to the qucik help on the group. I could do much of my coding from the pointers given. I have another question
If I want to just separate the parts coming in feild F_3 i.e. say F_3 = 'SYCPRP2_yyyyMMdd_RISK.CSV, SYCPRP2_yyyyMMdd_PCA.CSV, RMMPRP2_yyyyMMdd_ORIGINAL_Details.CSV'
I mean I want to extarct whatever is coming before the comma and pass it as parameters how do I go about it.
I tried using cut but that didnt help much as number of chars coming before comma may vary. Any quick help would be highly appreciated
Thanks dr.house . Overall that site doesn't look too bad if you only write for bash. Most of the time they avoid constructs which easily break like:
for i in `ls -1R`
---------- Post updated at 14:12 ---------- Previous update was at 12:50 ----------
Back on topic.
The original problem with:
for line in `cat Results.txt`
The "cat" executes first and create a very wide "for line in" statement with lots of fields made up from the entire contents of Results.txt:
The field separator in "for" is a space character.
The "for" loop executes once for every space separated field. Thus $line never contains the whole record. In fact $FIELD2 only contains something if the contents of $line happen to contain a pipe character.