Unix:根据匹配的字符串将文件拆分为两个

时间:2014-03-14 01:59:11

标签: bash unix sed awk grep

我想将文件拆分为两个,但找不到办法。

Master.txt
Happy Birthday to you!  [[#HAPPY]]
Stop it.  [[#COMMAND]]
Make a U-turn. [[#COMMAND]]

我想拆分成两个文件,第二个文件在匹配正则表达式模式时开始[[#

Output1.txt
Happy Birthday to you!
Stop it.
Make a U-turn.

Output2.txt
[[#HAPPY]]
[[#COMMAND]]
[[#COMMAND]]

我尝试过使用awk:

awk -v RS="[[#*" '{ print $0 > "temp" NR }'

但它没有给出我想要的输出 - 任何帮助都将不胜感激!

7 个答案:

答案 0 :(得分:4)

以下是GNU awk的一种方式:

awk -v RS='\\[\\[#|\n' 'NR%2{print $0>"Output1.txt";next}{print "[[#"$0>"Output2.txt"}' master

测试:

$ ls
master

$ cat master 
Happy Birthday to you!  [[#HAPPY]]
Stop it.  [[#COMMAND]]
Make a U-turn. [[#COMMAND]]

$ awk -v RS='\\[\\[#|\n' 'NR%2{print $0>"Output1.txt";next}{print "[[#"$0>"Output2.txt"}' master

$ ls
master  Output1.txt  Output2.txt

$ head Out*
==> Output1.txt <==
Happy Birthday to you!  
Stop it.  
Make a U-turn. 

==> Output2.txt <==
[[#HAPPY]]
[[#COMMAND]]
[[#COMMAND]]

答案 1 :(得分:1)

bash解决方案可能会慢一些,但可读性很高:

while read line; do
    [[ $line =~ (.*)(\[\[#.*]]) ]]
    printf "%s" "${BASH_REMATCH[1]}" >&3
    printf "%s" "${BASH_REMATCH[2]}" >&4
done 3> output1.txt 4> output2.txt

答案 2 :(得分:0)

you can write small script like this…
#!/bin/ksh

sed -i -e 's/ \[\[#/,\[\[#/' $1

cut -d, -f1 $1 > $1.part1
cut -d, -f2 $1 > $1.part2    
---------------------------------------------

或...使用多命令行

# sed -i -e 's/ \[\[#/,\[\[#/' Master.txt ; cut -d, -f1 Master.txt > output1.txt ; cut -d, -f1 Master.txt > output.txt

答案 3 :(得分:0)

sed更简单,恕我直言:

$ sed 's/^\([^[]*\).*/\1/' Master.txt > Output1.txt
$ sed 's/^[^[]*//'         Master.txt > Output2.txt

答案 4 :(得分:0)

不需要gnu awk,这适用于任何awk

awk -F'\\[\\[#' '{print $1>"Output1.txt";print "[[#"$2>"Output2.txt"}' Master.txt

cat Output1.txt
Happy Birthday to you!
Stop it.
Make a U-turn.

cat Output2.txt
[[#HAPPY]]
[[#COMMAND]]
[[#COMMAND]]

答案 5 :(得分:0)

sed -n 's/\[\[#/\
&/;P
/\n/ {s/.*\n//;H;}
$ {x;s/\n//;w Output2.txt
  }' YourFile > Output1.txt

1 sed但是awk更适合这项任务

答案 6 :(得分:0)

这可能适合你(GNU sed):

sed -n 's/\[\[#/\n&/;P;s/.*\n//w file3' file1 >file2