Home > OS >  Print all lines between line containing a string and first blank line, starting with the line contai
Print all lines between line containing a string and first blank line, starting with the line contai

Time:04-13

I've tried awk:

awk -v RS="zuzu_mumu" '{print RS $0}' input_file > output_file

The obtained file is the exact input_file but now the first line in file is zuzu_mumu. How could be corrected my command?

CodePudding user response:

Print all lines between line containing a string and first blank line, starting with the line containing that string

I would use GNU AWK for this task. Let file.txt content be

Able
Baker
Charlie

Dog
Easy
Fox

then

awk 'index($0,"aker"){p=1}p{if(/^$/){exit};print}' file.txt

output

Baker
Charlie

Explanation: use index String function which gives either position of aker in whole line ($0) or 0 and treat this as condition, so this is used like is aker inside line? Note that using index rather than regular expression means we do not have to care about characters with special meaning, like for example .. If it does set p value to 1. If p then if it is empty line (it matches start of line followed by end of line) terminate processing (exit); print whole line as is.

(tested in gawk 4.2.1)

CodePudding user response:

If you don't want to match the same line again, you can record all lines in an array and print the valid lines in the END block.

awk '
f && /zuzu_mumu/ {              # If already found and found again
  delete ary; entries=1; next;  # Delete the array, reset entries and go to the next record
}
f || /zuzu_mumu/ {              # If already found or match the word or interest
  if(/^[[:blank:]]*$/){exit}    # If only spaces, exit
  f=1                           # Mark as found
  ary[entries  ]=$0             # Add the current line to the array and increment the entry number
}
END {                             
  for (j=1; j<entries; j  )     # Loop and print the array values
      print ary[j]
}
' file
  • Related