AWK Command

I have a file seperated by pipe(|) operator
I want to fetch lines from the file having the word "Task started" in 7th column

awk -F"[|]" '{if($7 == "Task started") print $0}' file.txt

But this code is not giving any output.
Please kindly point out where I went wrong

Try -F"|"

Otherwise, post the input data.

If line CONTAINS "Task started" in 7th columns:

awk -F'|' '$7 ~ /Task started/ {print}' file.txt

If line HAS "Task started" as 7th column:

awk -F'|' '$7 == "Task started" {print}' file.txt

Thanks balajesuri the first command worked for me.

If i have to add more than one condition then what is the syntax?
I need to match values stored in my variables(var1,var2) along with this Task started condition with the data in the file

 
awk -v var1="TEST" -F\| '$7~/Task started/ && $7~/TEST/' input.txt

Hi itkamaraj.
I didn't get you command. Can you please explain it? and I don't know what all are the values in variable since I am fetching values from array

awk -v var1="TEST" -F\| '$7~/Task started/ && $7~/TEST/' input.txt

sorry it should be $7~var1

---------- Post updated at 11:51 AM ---------- Previous update was at 11:50 AM ----------

$ echo "AB|CD|EF|GH|IJ" | awk -v var1="B" -F\| '$1~/A/ && $1~var1'
AB|CD|EF|GH|IJ
$ echo "AB|CD|EF|GH|IJ" | awk -v var1="C" -F\| '$1~/A/ && $1~var1'
$

Thanks for the help
if i don't know the value of var1 then will this command work?

awk -v var1 -F\| '$7~/Task started/ && $9~var3  {print $0}' input.txt >> file1.txt
ka="B"
kb="A"
echo "AB|CD|EF|GH|IJ" | awk -v var1=$ka -v var2=$kb -F\| '$1~var2 && $1~var1'

you need to assign the value in var1 ( in the awk command -v var1="your array value" )

I tried the command

awk -v var4=$var2 -v var5=$var3 -F\| '$7~/Task started/ && $8~var4 && $9~var5  {print $0}' file.txt >> file1.txt
 
$var3 has value 
xengine:CERR to ECR CF

it displays an error message

awk: fatal: cannot open file `ECR' for reading (No such file or directory)

use double quotes

 
var4="$var2" -v var5="$var3"

This looks pretty close to your previous thread?

I was trying the same thing with awk as I studied about it somewhere but was facing some issues so posted here

---------- Post updated at 02:58 AM ---------- Previous update was at 01:18 AM ----------

I want to match whole word "Task started"

awk -v var4="$var2" -v var5="$var3" -F\| '$7~/Task started/ && $8~var4 && $9~var5  {print $1}' input.txt >> file1.txt

As this command matches line having word "Task started" but I want just the word not anything else

 
$7~/Task started/
 to 
$7=="Task started"

Its not working.It's not giving any output neither any error

provide the input file and the expected output

1 Like

this is the sample input

2012-05-09 10:28:47.942 | SYNC  | 00040 | INFO    | Customer_Receive Payment | Task            | Task started                           | B678C56D-96DA-4FFC-B40E-9A032A2EB12E          | filesystem:CERR                     | {}
2012-05-09 10:28:47.981 | SYNC  | 00041 | DEBUG   | Customer_Receive Payment | Task            | Task started on a component            | B678C56D-96DA-4FFC-B40E-9A032A2EB12E          | filesystem:CERR                     | {}

the desired output

2012-05-09 10:28:47.942

the output it is giving is

2012-05-09 10:28:47.942
2012-05-09 10:28:47.981

your 7th field has space also.

so, use this

gsub(" ","",$7) -- remove all the space. So your 7th field becomes "Taskstarted"

 
awk -v var4="$var2" -v var5="$var3" -F\| '{gsub(" ","",$7)} $7=="Taskstarted" && $8~var4 && $9~var5  {print $1}' input.txt >> file1.txt
1 Like

if I have to check more than one word like "Task started" or "Exchange started"
so will this code work??

awk -v var4="$var2" -v var5="$var3" -F\| '{gsub(" ","",$7)} ($7=="Taskstarted" || $7=="Exchangestarted")&& $8~var4 && $9~var5  {print $1}' Sri1.log >> file1.txt