Home > Back-end >  How to use awk to print a line before match and until next blank space after the match
How to use awk to print a line before match and until next blank space after the match

Time:08-02

For example, I have a variable $var

 awk  '/'$var'/ {  }' file.txt

Once awk matches the variable in text file, I wanna to start printing one line before the match until next blank space.

Edit: My File

AAAA
BBBB
SSSS
CCCC
DDDD
LLLL
PPPP
ASAD
BEKK

SSEE
AASS

if $var = SSSS, my ouput should look like:

BBBB
SSSS
CCCC
DDDD
LLLL
PPPP
ASAD
BEKK

Sorry I am new here If my explanation is not very clear

CodePudding user response:

With your shown samples and attempts, please try following GNU grep solution. Written and tested in GNU grep:

grep -ozP '(?:[^\n] \n)?AAAA(?:\n[^\n] )*'  Input_file


Few scenarios Checking above code with shown samples and with 3 different input strings.

1st scenario: Checking with input string SSSS:

grep -ozP '(?:[^\n] \n)?SSSS(?:\n[^\n] )*' Input_file
BBBB
SSSS
CCCC
DDDD
LLLL
PPPP
ASAD
BEKK

2nd scenario: Checking with string AAAA in code:

grep -ozP '(?:[^\n] \n)?AAAA(?:\n[^\n] )*' Input_file
AAAA
BBBB
SSSS
CCCC
DDDD
LLLL
PPPP
ASAD
BEKK

3rd scenario: Checking with input string BEKK:

grep -ozP '(?:[^\n] \n)?BEKK(?:\n[^\n] )*' Input_file
ASAD
BEKK

CodePudding user response:

awk -v tgt="$var" '!f && ($0==tgt){print prev; f=1} f{if (NF) print; else exit} {prev=$0}' file

The above assumes you just want the first such range printed. If that's wrong then change to:

awk -v tgt="$var" '!f && ($0==tgt){print prev; f=1} f{if (NF) print; else f=0} {prev=$0}' file

Both scripts assume you want to do a full-line string match.

CodePudding user response:

You may use this awk solution using match function and empty RS:

awk -v var='SSSS' -v RS= 'match($0, "(^|[^\n] \n[^\n]*)" var) {print substr($0, RSTART)}' file
BBBB
SSSS
CCCC
DDDD
LLLL
PPPP
ASAD
BEKK

# more testing
awk -v var='BEKK' -v RS= 'match($0, "(^|[^\n] \n[^\n]*)" var) {print substr($0, RSTART)}' file
ASAD
BEKK

awk -v var='AAAA' -v RS= 'match($0, "(^|[^\n] \n[^\n]*)" var) {print substr($0, RSTART)}' file
AAAA
BBBB
SSSS
CCCC
DDDD
LLLL
PPPP
ASAD
BEKK
  • Related