使用bash命令删除nextline,space

时间:2014-04-29 08:32:41

标签: linux bash unix sed

sed命令是否能够删除空格,nextline?

我希望得到以下输出(请参阅下面的输出)

TESTING     The quick brown fox jumps over the lazy dog

            The quick brown fox jumps over the lazy dog


TESTING02   The quick brown fox jumps over the lazy dog

            The quick brown fox jumps over the lazy dog

TESTING03   The quick brown fox jumps over the lazy dog


            The quick brown fox jumps over the lazy dog


            The quick brown fox jumps over the lazy dog

输出应为以下

TESTING     The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

TESTING02   The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

TESTING03   The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

3 个答案:

答案 0 :(得分:4)

如果您想尝试awk,可以这样做:

awk '/TESTING/ && NR>1 {print ""} NF' file
TESTING     The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

TESTING02   The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

TESTING03   The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

NF阻止其打印空行。
/TESTING/ && NR>1 {print ""}TESTING行之前添加一个空行,但第一行除外

答案 1 :(得分:3)

为你的结构

sed '/^[[:space:]]*$/d
1!{
   /^TESTING/ s//\
&/
  }' YourFile

删除“空格行”并在TESTING条目之前添加新行(但不是第1行)

答案 2 :(得分:1)

这可能适合你(GNU sed):

sed '/\S/!d;1b;/TESTING/i\\' file

删除所有空行并在包含TESTING的行之前插入一行,除非它是第一行。