Join lines based on pattern

蹲街弑〆低调 提交于 2019-12-01 23:40:53

sed 'N;s/\n//' file.txt

This should give the desired output when the content is in file.txt

paste -d "" - - < filename

This takes consecutive lines and pastes them together delimited by the empty string.

awk '{printf("%s", $0);} !(NR%2){printf("\n");}' file.txt


EDIT: I just noticed that your question requires the use of cat and grep. Both of those programs are unnecessary to achieve your stated aims. If you have some reason for including them that you haven't mentioned, try this (uselessly inefficient) version of the line I wrote immediately above:
cat file.txt | grep '^' | awk '{printf("%s", $0);} !(NR%2){printf("\n");}'

It is possible that this command uses features not present in the original awk program. You may need to invoke the new awk program, nawk instead.

If your input file is always 1 number then 1 string, and you only want the strings, all you have to do is take every other line.

If you only want the odd lines, you can do awk 'NR % 2' file.txt

If you want the evens, this becomes awk 'NR % 2==0' data

Here is the answer: cat file.txt | awk 'BEGIN { lno = 0 } { val=$0; if (lno % 2 == 1) {printf "%s\n", $0} else {printf "%s", $0}; ++lno}'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!