English 中文(简体)
Awk - print next record following matched record
原标题:
  • 时间:2009-11-19 06:47:05
  •  标签:
  • awk

I m trying to get a next field after matching field using awk.

Is there an option to do that or do I need to scan the record into array then check each field in array and print the one after that?

What I have so far:

The file format is:

<FIELD><separator "1"><VALUE><separator "1"><FIELD><separator "1"><VALUE> 

... and so on, field|value pairs are repeated, will be at least one pair in line or multiple pairs <10 per line

dat.txt:

FIELDA01VALUEA01FIELDA21VALUEA21FIELDA31VALUEA3
FIELDB01VALUEB01FIELDB21VALUEB21FIELDB31VALUEB3
FIELDC01VALUEC01FIELDC21VALUEC21FIELDC31VALUEC3
FIELDD01VALUED01FIELDD21VALUED21FIELDD31VALUED3
FIELDE01VALUEE01FIELDE21VALUEE21FIELDE31VALUEE3

With simple awk script which prints the second field on a line matching FIELDB2:

#!/bin/awk -f

BEGIN { FS = "1" }
/FIELDB2/ { print $2 }

Running the above:

> ./scrpt.awk dat.txt

Gives me:

VALUEB0

This is because the line that match:

FIELDB01VALUEB01FIELDB21VALUEB21FIELDB31VALUEB3

When split into records looks:

FIELDB0 VALUEB0 FIELDB2 VALUEB2 FIELDB3 VALUEB3

From which the second field is VALUEB0

Now I don t know which FIELDXX will match but I would like to print the next record on the line after FIELDXX that matched, in this specific example when FIELDB2 matches I need to print VALUEB2.

Any suggestions?

最佳回答

You can match the line, then loop over the fields for a match, and print the next record:

awk -F1  /FIELDB2/ { for (x=1;x<=NF;x++) if ($x~"FIELDB2") print $(x+1) }  
问题回答

No need for slowing things down using a loop :)

awk -F1  /FIELDB2/ {f=NR} f&&NR-1==f  RS="1" file
VALUEB2




相关问题
awk save command ouput to variable

I need to execute a command per line of some file. For example: file1.txt 100 4 file2.txt 19 8 So my awk script need to execute something like command $1 $2 $3 and save the output of command $1 $2 ...

awk and bash script

I have a tgz file which contains a version file, version.txt. This file has only one line "version=1.0.0". This tgz file is present in two different directories and has the same name. My requirement ...

awk - how to specify field separator as binary value 0x1

Is it possible to specify the separator field FS in binary for awk? I have data file with ascii data fields but separated by binary delimiter 0x1. If it was character 1 it would look like this: ...

Awk - print next record following matched record

I m trying to get a next field after matching field using awk. Is there an option to do that or do I need to scan the record into array then check each field in array and print the one after that? ...

Using Unix Tools to Extract String Values

I wrote a small Perl script to extract all the values from a JSON formatted string for a given key name (shown below). So, if I set a command line switch for the Perl script to id, then it would ...

how to use sed, awk, or gawk to print only what is matched?

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk. But in my case, I have a regular expression that I want to run against a text file to extract ...

Bulk Insert Code Before </body> Tag in 100 Files

I d like to insert <?php include_once( google_analytics.php ); ?> before the closing body tag of about 100 php files. Unfortunately the person who made the site didn t make a header or ...

热门标签