将目录中每个文件的标记之间的文件内容提取到新位置

时间:2013-11-21 23:30:45

标签: linux bash sed grep

让我们说我在目录location1 / bar中有一个名为“foo1.txt,v”的文件,其中包含我在开始标记和结束标记中包含的所需文本

@@
text
@<content> 
@

我的目录栏包含“foo1.txt,v”“foo2.txt,v”.......“fooN.txt,v”

我希望从每个文件中获取标签@@ \ ntext \ n @和\ n @ \ n之间的内容,并将其保存在 location2 / bar,文件名为foo1.txt foo2.txt ...... fooN.txt

即。将文件夹结构复制到另一个位置,同时从文件名中删除“,v”,其中新文件仅包含亲爱的文本

3 个答案:

答案 0 :(得分:1)

这个awk单行应该这样做:

 awk '/^@@/{p=1;next}/^@$/{p=0}p{f=FILENAME;sub(/,v$/,"",f);print > "/loc2/bar/"f}' loc1/foo/*

一点测试:

kent@7pLaptop:/tmp/test/foo$ head *
@@text@
xxx
@

==> foo2.txt,v <==
@@text@
yyy
@
kent@7pLaptop:/tmp/test/foo$ ls -l /tmp/test/bar 
total 0
kent@7pLaptop:/tmp/test/foo$ awk '/^@@/{p=1;next}/^@$/{p=0}p{f=FILENAME;sub(/,v$/,"",f);print > "/tmp/test/bar/"f}' *
kent@7pLaptop:/tmp/test/foo$ head /tmp/test/bar/*
==> /tmp/test/bar/foo1.txt <==
xxx

==> /tmp/test/bar/foo2.txt <==
yyy
kent@7pLaptop:/tmp/test/foo$ 

答案 1 :(得分:1)

试试这个脚本:

cd /path/to/location1/..

mkdir -p location2/bar

for file in location1/bar/*.txt,v
do
    name=${file##*/}
    sed -n '/^@@/{:a;n;/^@/!{p;ba};q0}' $file > location2/bar/${name%,v}
done

答案 2 :(得分:0)

没有sed或awk它有点大:

#!/bin/bash

FROM="/location1/foo" ; cd "$FROM"         #goto the originating directory
DEST="/location2/bar" ; mkdir -p "$DEST"   #create a destination directory 

#with the `for` loop we visit each file in the FROM directory.
for FILE in *.txt,v 
do
    # we start reading the FILE here given by the 'for' loop 
    # line by line until there is no more to read. 
    while read LINE; do

        #TAG is a flag we unset if the line we read is "@" (end tag).
        [ "$LINE" = "@"        ] && TAG=""

        #if TAG is set,append the line to the destination file without the ,v
        [ "$TAG"               ] && echo "$LINE" >> "$DEST/${FILE%,v}"

        #Set TAG if the first two characters of the line is "@@" (start tag).
        [ "${LINE:0:2}" = "@@" ] && TAG="X"

    done < "$FILE" #<-- this is where 'while read LINE' gets input
    TAG=""         #<-- unset TAG, a missing end-tag could have left it "set"
done