Error while executing a script

My script is as below :

#!/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.

Might be the fact that you assign the values to FEILDx variables, but try to print FIELDx variables?

That said, this script can be written just as easily as

awk -F '|' '{print $1,$2,$3,$4}' Results.txt

It seems a simple typo..

FEILD2 & FIELD2

but I need to use the FEILD 1, 2 ,3 etc as parameters further and hence I have to store them somewhere

---------- Post updated at 04:31 AM ---------- Previous update was at 03:22 AM ----------

Hi Thanks

I corrected the typo still there is something wrong

My Results.txt contains
374|Jane - Sensitivities (EUI8)|SYCEUI8_yyyyMMdd_RISK.CSV, SYCEUI8_yyyyMMdd_PCA.CSV, RMMEUI8_yyyyMMdd_ORIGINAL_Details.CSV|3
472|Jane - Sensitivities (PRP2)|SYCPRP2_yyyyMMdd_RISK.CSV, SYCPRP2_yyyyMMdd_PCA.CSV, RMMPRP2_yyyyMMdd_ORIGINAL_Details.CSV|3

So my feilds shud look like
FEILD 1 = 374
FEILD 2 =Jane - Sensitivities (EUI8)|
FEILD 3 = SYCEUI8_yyyyMMdd_RISK.CSV, SYCEUI8_yyyyMMdd_PCA.CSV, RMMEUI8_yyyyMMdd_ORIGINAL_Details.CSV
FEILD 4 = 3

which is not happening at the moment. Can some one please help. i have to send this code for UAT by tomorrow . Please help

Show us the output of your script.

Jean-Pierre.

Output is as below
374 Jane
-
Sensitivities
(EUI8) SYCEUI8_yyyyMMdd_RISK.CSV,
SYCEUI8_yyyyMMdd_PCA.CSV,
RMMEUI8_yyyyMMdd_ORIGINAL_Details.CSV 3
472 Jane
-
Sensitivities
(PRP2) SYCPRP2_yyyyMMdd_RISK.CSV,
SYCPRP2_yyyyMMdd_PCA.CSV,
RMMPRP2_yyyyMMdd_ORIGINAL_Details.CSV 3

Not sure what your problem is, but the following works for me:

#! /bin/bash

cat results.txt | \
while read LINE
do
  F_1=$( echo $LINE | awk -F '|' '{ print $1 }' )
  F_2=$( echo $LINE | awk -F '|' '{ print $2 }' )
  F_3=$( echo $LINE | awk -F '|' '{ print $3 }' )
  F_4=$( echo $LINE | awk -F '|' '{ print $4 }' )
  echo "F_1 = '$F_1'"
  echo "F_2 = '$F_2'"
  echo "F_3 = '$F_3'"
  echo "F_4 = '$F_4'"
done

exit 0

# [game over]
[house@universe] sh test.bash
F_1 = '374'
F_2 = 'Jane - Sensitivities (EUI8)'
F_3 = 'SYCEUI8_yyyyMMdd_RISK.CSV, SYCEUI8_yyyyMMdd_PCA.CSV, RMMEUI8_yyyyMMdd_ORIGINAL_Details.CSV'
F_4 = '3'
F_1 = '472'
F_2 = 'Jane - Sensitivities (PRP2)'
F_3 = 'SYCPRP2_yyyyMMdd_RISK.CSV, SYCPRP2_yyyyMMdd_PCA.CSV, RMMPRP2_yyyyMMdd_ORIGINAL_Details.CSV'
F_4 = '3'

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

F_3_1=$( echo $F_3 | awk -F ',' '{ print $1 }' )

Hi

I have added a if loop to the script viz

if [`echo $F_4` -eq 3]
then
{
echo "Two FW Jobs"
---
----
---
} 
else 
{
----------
--------
} 
fi 

but this is not working it gives the message ./Automation_2.sh[29]: [3: not found
when I run this. Could you please help.

In the line

if [`echo $F_4` -eq 3]

You need to put at least one whitespace on each side of each of the square brackets, otherwise it won't be interpreted as the 'test' instruction.

I will take care of these instrucitons going forward. BTW thanks it worked now after adding white spaces

You don't need to use echo in your if statement :

if [ $F_4 -eq 3 ]

Jean-Pierre.

Something I've been wondering for months:
Where does this construct come from?

I've never seen it in a commercial script and never seen it in a book or manual. It does however crop up at least twice a week on this board.

Maybe from the "ABS" (- see example 10.7 et sqq.) ...?

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.