Sunday, 11 August 2013

how do i search for a pattern in a file and print its line after performing changes on some fields in that line in unix bash?

how do i search for a pattern in a file and print its line after
performing changes on some fields in that line in unix bash?

I have two files: file1 and file2 as follows:
file1 file2
orangejuice orangejuice_9.88_9.88
pineapplejuice appleslices_6.3_2.2
appleslices pineapplejuice_1.2_3.9
Mangojuice Mangojuice_5.55_5.55
The output should be:
orangejuice_988
pineapplejuice_120_390
appleslices_630_220
Mangojuice_555
While reading line by line from file1, search for the pattern found in the
line of file1 in file2, once found compare the 2nd and 3rd fields of file2
, if they are the same print it once, if not print the two numbers.(The
number should be multiplied by 100 anyways)
I thought of this:
while read -r -u 3 line1
do
nawk ' "$line1" print $0}' file2.txt
if "$2" == "$3"
then echo "scale=2;$2*100" |bc
else echo "$2_$3"
fi
done 3<file1.txt
So, I want to know if the logic is correct and fix the multiplication that
would give me 988 instead of 988.0. Thankyou

No comments:

Post a Comment