English 中文(简体)
一、导 言
原标题:Convert column pattern

我有这种档案:

1 0 1

2 0 3
2 1 2

3 0 3

4 0 1
4 1 1
4 2 1
4 3 1

5 0 1



8 0 1


10 0 1

11 0 1

塞族共和国的分离者是一空一线。

如果存在双重空白线,我们必须代之以如下方式: 1 0<>code>,$1, 即之前增加的“编号”, 编号1 0*

If the separator is empty line + 1 empty line we have to increase the $1 by 1. If the separator is empty line + 2 empty line we have to increase the $1 by 2. ...

and I need to get this output:

1 0 1

2 0 3
2 1 2

3 0 3

4 0 1
4 1 1
4 2 1
4 3 1

5 0 1

6 0 0 

7 0 0

8 0 1

9 0 0

10 0 1

11 0 1

提前感谢!

最佳回答

你可以尝试这样的东西。 <><>>> 这样做的好处是,你的输入文件不需要一个空洞的线。

awk -v RS="" -v ORS="

" -v OFS="
"                     
BEGIN{getline; col=$1;line=$0;print line}
$1==col{print $0;next }
($1==col+1){print $0;col=$1;next}
{x=$1;y=$0; col++; while (col < x) {print col" 0 0";col++};print y;next}  file 

Input File:

[jaypal:~/Temp] cat file
1 0 1

2 0 3
2 1 2

3 0 3

4 0 1
4 1 1
4 2 1
4 3 1

5 0 1



8 0 1


10 0 1

11 0 1

Script Output:

[jaypal:~/Temp] awk -v RS="" -v ORS="

" -v OFS="
"                     
BEGIN{getline; col=$1;line=$0;print line}
$1==col{print $0;next }
($1==col+1){print $0;col=$1;next}
{x=$1;y=$0; col++; while (col < x) {print col" 0 0";col++};print y;next}  file
1 0 1

2 0 3
2 1 2

3 0 3

4 0 1
4 1 1
4 2 1
4 3 1

5 0 1

6 0 0

7 0 0

8 0 1

9 0 0

10 0 1

11 0 1
问题回答
awk  NF{f=0;n=$1;print;next}f{print ++n " 0 0"}{print;f=1}  ./infile

Output

$ awk  NF{f=0;n=$1;print;next}f{print ++n " 0 0"}{print;f=1}  ./infile
1 0 1

2 0 3
2 1 2

3 0 3

4 0 1
4 1 1
4 2 1
4 3 1

5 0 1

6 0 0

7 0 0

8 0 1

9 0 0

10 0 1

11 0 1

Explanation

  • NF{f=0;n=$1;print;next}: if the current line has data, unset flag f, save the number in the first field to n, print the line and skip the rest of the script
  • {print;f=1}: We only reach this action if the current line is blank. If so, print the line and set the flag f
  • f{print ++n " 0 0"}: We only execute this action if the flag f is set which only happens if the previous line was blank. If we enter this action, print the missing fields with an incremented n




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...