I want to fetch value from a particular location from a file but in each line in the file it appears at a different position so i tried using variable with cut command but it is not working properly. The code i have written is
#!/bin/sh
cat Sri1.log |
while read d2
do
grep -w 'RemittanceId' | awk -v find='RemittanceId' '{loc=index($0,find)}'
locations=`expr $loc + 13`
locatione=`expr $location + 15`
cut -c $locations,$locatione >> ab.txt
done
I am storing the start and end values in the variables but it's not working fine. It doesn't give any output
there is no input for grep command
you didnt use d2 value anywhere in the script
echo $d2 | grep -w 'RemittanceId' | awk -v find='RemittanceId' '{loc=index($0,find)}'
Also, instead of using cat, you can use it like this
while read d2
do
...
...
done < Sri1.log
---------- Post updated at 11:42 AM ---------- Previous update was at 11:41 AM ----------
also, you didnt print the loc value in the awk
and what is $loc and $locations ?
Thanku for the reply
I tried doing that.It works fine but not givingme the desired output since
loc returns value 0
but when a try to write the output of awk in a file it gives the all the locations at which value is present.
Hiw can a fetch the value of loc the moment it encounters taht line and moving on to next line.
I tried defining the variable locations and locatione inside the action part of awk but it didn't worked
echo $d2|grep -w 'RemittanceId' | awk -v find='RemittanceId' '{loc=index($0,find), locations=`expr $loc + 13` , locatione=`expr $location + 15`}'
can you provide the sample log file contents and the expected output
I can't share complete file but below is the line from the file :-
2012-05-09 10:29:30.041.... | SYNC | 00052 | DEBUG | Payment | ....Exchange | Exchange processing completed.....| B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003 | ....... | {previous-component=Validation, Split, exchange-properties={....., RemittanceId=IUxBNzKlZfghcMxf101209090000024_1, ParserSchema=....., }
I want to fetch this RemittanceId from the file..
There are many occurrences of this Id in the whole file and at different locations
The desired output is
IUxBNzKlZfghcMxf101209090000024_1
$ nawk -F"[=,]" '{for(i=1;i<=NF;i++)if($i~/RemittanceId/)print $(i+1)}' input.log
IUxBNzKlZfghcMxf101209090000024_1
if you dont have nawk, then try with awk or gwak
---------- Post updated at 12:48 PM ---------- Previous update was at 12:45 PM ----------
$ nawk -F, '/RemittanceId/{split($4,a,"=");print a[2]}' input.log
IUxBNzKlZfghcMxf101209090000024_1
Thanx a lot it is working fine.. I am greatful
Can you please explain me this command since I am new to Unix and don't have much understanding how it is working
-F"[=,]" --> we are using two field seperators ( = and , )
for(i=1;i<=NF;i++) --> loop all the fileds
if($i~/RemittanceId/) --> check if the field has the word RemittanceId
print $(i+1) --> print the next field ( only the above condition is true )
Thnax a lot fro the explaination...
I was struggling with this from so many days...
Thanx once again
The above command worked properly but now I have to check three conditions and then print the value.
The command i am using is
awk -F"[=,]" '{for(i=1;i<=NF;i++)if($r~/RemittanceId/ && $f~/Exchange sent/ && $d~/DEBUG/)print $(r+1)}' Sri1.log >>out8.txt
I want to print value next to the word RemittanceId but this is printing complete line