Text processing

Hi,

Need an advise on

$ cat test.txt 
START
field1
field2
field3
field4
field5
field6
END

12345|6|1|2|3|4|111|119
67890|6|1|3|8|9|112|000

$

Above is the sample file, I would like to check if "field5" exists in file, if exists check if 67890 exits, if exists then print corresponding values for "field5"

sample output:
112

Thanks in advance

Maddy

112 is actually the seventh field - assuming the pipe symbols are field delimiters.

This works with your sample, which does not necessarily match what you describe.
It prints the last field on the 67890 line (where 67890 is the first field) -- if the line exists.

if  [ grep -q "field5" somefilename  ] ; then    # check for field5
   grep -q '^67890' somefilename  && awk '{print $(NF) } ' || echo ' 67890 not found'
else
   echo 'not found'   # field5 is not found
fi

This is not very good code but your description kind of limits what I can show you.

first column is search pattern
|6| in both lines is total number of fields

I is still not all that clear..

Perhaps this would suffice, give it a try:

awk -F\| '$1==67890 && $2>4{print $7}' file

Or would you need to have the actual number of fields counted in the first section of the input file?

awk -F"|" '
r && NF {r++}
r && NF && $1==str {c=(r - 1)}
$1 ~ /START/ {r=1}
c && $1==val {print $(2 + c)}
' str="field5" val="67890" test.txt