开发者

Combine 2 lines in large text file using sed

How do I combine two specific adjacent lines in a large text file into one line using sed? For example I want to combine line numbered 2001891 and 2001892 into one line, and write it back to the file at the same line location (meaning line 2001893 and below will get shifted up)?

I've googled around for tutorials and manuals on sed but all are too cryptic to me (those '2,3p;N;s/ja开发者_如何学运维m.l,21/.,lk;dczA[12]blahblah' means what?)


I am not a sed guru, but I think you want:

sed -i '2001891N;s/\n//' filename

(Try it without -i on a test file before running it on important data, of course.)

$ cat a.dat
1
2
3
4
$ sed '2N;s/\n//' a.dat 
1
23
4

I tried the simpler sed '2001891s/\n//', but it doesn't work when the pattern is \n. For any other pattern (sed '2001891s/a//' for example), it seems to work. It's too late in the night for me to think about it more, but I hope one of the sed gurus will explain what's going on with my simpler sed command.


I am sure you can do some simple magic using cat, file redirection, and nl command.


$ awk 'NR==2001891{printf $0;getline;print;next}1' file > temp;mv temp file

OR

$ awk '{printf (NR==2001891)?$0:$0"\n"}' file  > temp;mv temp file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜