how to redirect entire pattern of lines from a file. grep, awk,sed etc.,

时间:2018-07-25 05:12:14

标签: shell scripting

$cat datafile

BlankLine
a Line start with "Cn:"
BlankLine

Want to direct these lines to a new file and remove it from current file. datafile contains plenty of lines.

1 个答案:

答案 0 :(得分:1)

sed可能会完成工作,

$ sed -e '/BlankLine/,/BlankLine/w newfile' -i -e '/BlankLine/,/BlankLine/d' datafile

简要说明,

  1. '/BlankLine/,/BlankLine/w newfile':搜索从'BlankLine'到另一'BlankLine'的行,并将其写入 newfile
  2. -i -e '/BlankLine/,/BlankLine/d':删除在数据文件中写入的行。