akang
akang

Reputation: 651

awk/sed solution for printing only next line after it matches a pattern

I have multiple files in a folder. This is how a file look like File1.txt

ghfgh gfghh
  dffd  kjkjoliukjkj
  sdf ffghf
  sf 898575
  sfkj utiith

## 
my data to be extracted 

I want to extract the line immediately below "##" pattern from all the files and write them to an output file. I want the file name to be appended too in the output file. Desired output

>File1
My data to be extracted
>File2
My data to be extracted
>File3
My data to be extracted 

This is what i tried 
awk '/##/{getline; print FILENAME; print ">"; print}' *.txt > output.txt

Upvotes: 1

Views: 98

Answers (4)

dawg
dawg

Reputation: 103844

POSIX or GNU sed:

$ sed -n '/^##/{n;p;}' file
my data to be extracted 

grep and sed:

$ grep -A 1 '##' file | sed '1d'
my data to be extracted 

Upvotes: 0

Kent
Kent

Reputation: 195079

a quick way with grep:

grep -A1 '##' *.txt|grep -v '##' > output.txt

Upvotes: 1

choroba
choroba

Reputation: 241898

Perl to the rescue!

perl -ne 'print ">$ARGV\n", scalar <> if /^##/' -- *.txt > output.txt
  • -n reads the input line by line
  • $ARGV contains the current input file name
  • scalar <> reads one line from the input

Upvotes: 2

karakfa
karakfa

Reputation: 67507

assumes one extract per file (otherwise filename header will be repeated)

$ awk '/##/{f=1; next} f{print ">"FILENAME; print; f=0}' *.txt > output.txt

Upvotes: 4

Related Questions