sed从多个文本文件中提取文本

时间:2016-03-30 18:06:04

标签: unix awk sed grep

可以说,我有两个输入文件,fileA.txtfileB.txt。文件是这样的:

FileA.txt
Color
yellow
orange
black
Fruit
apple
banana
Weekday
monday
sunday

FileB.txt
Color
blue
green
Fruit
melon
Weekday
saturday
tuesday
thursday

我想在所有文件中提取ColorFruitWeekday下面的信息,并为每个字段创建一个新信息。所以,我的输出是3个文件,Color.txtFruit.txtWeekday.txt就像这样。

Color.txt
Color_A
yellow
orange
black
Color_B
blue
green

Fruit.txt
Fruit_A
apple
banana
Fruit_B
melon

Weekday.txt

类似

我希望现在更清楚了,谢谢。

2 个答案:

答案 0 :(得分:1)

awk救援!

$ awk '{suffix=substr(FILENAME,5,1)}
         /Color|Fruit|Weekday/{file=$1".txt"; $1=$1"_"suffix}
             {print > file}' FileA.txt FileB.txt                   


$ head {Fruit,Color,Weekday}.txt          

==> Fruit.txt <==                                                                                                     
Fruit_A
apple
banana
Fruit_B
melon

==> Color.txt <==
Color_A
yellow
orange
black
Color_B
blue
green

==> Weekday.txt <==
Weekday_A
monday
sunday
Weekday_B
saturday
tuesday
thursday

答案 1 :(得分:0)

在POSIX shell脚本中(即bash,dash等):

rm Color.txt Fruit.txt Weekday.txt  # old files will spoil this.
for f in A B
do
    while read x
    do 
        L=${x%${x#?}}
        l=$(echo $L | tr '[:upper:]' '[:lower:]')
        if [ $L != $l ]
        then
            F=$x.txt
            echo ${x}_$f >> $F
        else
            echo $x >> $F
        fi
    done < File$f.txt
done

输出:

head {Fruit,Color,Weekday}.txt
==> Fruit.txt <==
Fruit_A
apple
banana
Fruit_B
melon

==> Color.txt <==
Color_A
yellow
orange
black
Color_B
blue
green

==> Weekday.txt <==
Weekday_A
monday
sunday
Weekday_B
saturday
tuesday
thursday

工作原理:暂停(我担心这是一个学业上的问题,如果OP公布上述shell代码可能会更好,然后根据需要解释或批评。)