我陷入了下面的问题。我用while循环完成了这个并且它工作正常但是不能省略每个循环中附加字符串的序列号。
我有文件内容
1 SELECT abc from a ;
2 .IF activi <> 1
3 THEN
4 QUIT;
5 .IF ERROR <> 0 THEN QUIT ERROR;
6 SELECT
7 a,
8 b,
9 c
10 FROM xyz;
11 .IF ERROR <> 0
12 THEN
13 QUIT ERROR;
我想在同一个文件中编辑 使用以下输出
1 SELECT abc from a;
2 .IF activi <> 1 THEN QUIT ;
5 .IF ERROR <> 0 THEN QUIT ERROR;
6 SELECT
7 a,
8 b,
9 c
10 FROM xyz;
11 .IF ERROR <> 0 THEN QUIT ERROR;
答案 0 :(得分:1)
使用sed:
$ sed '/\.IF/{:a;/; *$/!{N;s/ *\n *[0-9]*//;ta}}' file
输出:
1 SELECT abc from a ;
2 .IF activi <> 1 THEN QUIT;
5 .IF ERROR <> 0 THEN QUIT ERROR;
6 SELECT
7 a,
8 b,
9 c
10 FROM xyz;
11 .IF ERROR <> 0 THEN QUIT ERROR;
<强>解释强>
sed '
/\.IF/ { # for lines containing ".IF"
:a; # define a "a" label for upcoming loop
/; *$/! { # if line does not contain ";"
N; # add next line to pattern space
s/ *\n *[0-9]*//; # remove newline and leading digits
ta; # loops to label "a" if ";" is not found
}
}' file