Reputation: 651
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
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
Reputation: 195079
a quick way with grep:
grep -A1 '##' *.txt|grep -v '##' > output.txt
Upvotes: 1
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 namescalar <>
reads one line from the inputUpvotes: 2
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