我在一个文件夹中有多个文件。这是一个文件的样子 File1.txt
ghfgh gfghh
dffd kjkjoliukjkj
sdf ffghf
sf 898575
sfkj utiith
##
my data to be extracted
我想从所有文件中提取“ ##”模式正下方的行,并将其写入输出文件。我也希望文件名也附加在输出文件中。 所需的输出
>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
答案 0 :(得分:4)
假设每个文件有一个摘录(否则将重复文件名头)
UPDATE wpp_posts join wp_term_relationships on (wp_posts.ID = wp_term_relationships.object_id)
SET post_content = ''
WHERE wp_term_relationships.term_taxonomy_id = 410
答案 1 :(得分:2)
抢救Perl!
Camera
perl -ne 'print ">$ARGV\n", scalar <> if /^##/' -- *.txt > output.txt
逐行读取输入-n
包含当前输入文件名$ARGV
从输入中读取一行答案 2 :(得分:1)
使用grep的快速方法:
grep -A1 '##' *.txt|grep -v '##' > output.txt
答案 3 :(得分:0)
POSIX或GNU sed:
$ sed -n '/^##/{n;p;}' file
my data to be extracted
grep和sed:
$ grep -A 1 '##' file | sed '1d'
my data to be extracted