Common methods of merging lines in linux

Linux merges two lines into one line with spaces
sed -n '{N;s/\n/ /p}' aa.txt
aa aa
bb bb
awk 'BEGIN{FS="\n";ORS=""};{if ($0 ~ /^10/ && NR>1){print "\n"$0;}else{print}}'
Method 1:

sed -n 'p;n' test.txt #odd line
sed -n 'n;p' test.txt #even line
method 2:

sed -n '1~2p' test.txt #odd line
sed -n '2~2p' test.txt #even line
[james@zat2 scripts]$ sed '1~2N;s/\n/\t/' jishu.txt
bb dd
aa cc
[james@zat2 scripts]$ cat jishu.txt
bb
dd
aa
cc
[james@zat2 scripts]$ sed ':z ;N;s/\n/ / ;tz ;' jishu.txt
bb dd aa cc
sed only processes by line by default, N can make it read into the next line, and then replace \n, so that two lines can be combined into one line. But how to combine all the lines into one line? You can use sed's jump function. :a Set a marker a at the beginning of the code, and use the jump command ta to jump to the label a again when the code is executed to the end, and re-execute the code, so that all lines can be recursively merged into one line.
[james@zat2 scripts]$ awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' jishu.
txt bb dd aa cc
Description: awk sets the record separator (record separator or RS) to \n by default, and this line of code sets RS to EOF (end of file), that is, treats the file as a record, and then passes the gsub function. Replace \n with spaces, and finally output.
[james@zat2 scripts]$ clear
[james@zat2 scripts]$ cat jishu.txt | xargs
bb dd aa cc

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679522&siteId=291194637