bash 如何从命令行将每两行合并为一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9605232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge every two lines into one from the command line?
提问by shantanuo
I have a text file with the following format. The first line is the "KEY" and the second line is the "VALUE".
我有一个具有以下格式的文本文件。第一行是“KEY”,第二行是“VALUE”。
KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1
I need the value in the same line as of the key. So the output should look like this...
我需要与键在同一行中的值。所以输出应该是这样的......
KEY 4048:1736 string 3
KEY 0:1772 string 1
KEY 4192:1349 string 1
KEY 7329:2407 string 2
KEY 0:1774 string 1
It will be better if I could use some delimiter like $
or ,
:
如果我可以使用像$
或这样的分隔符会更好,
:
KEY 4048:1736 string , 3
How do I merge two lines into one?
如何将两行合二为一?
回答by glenn Hymanman
paste
is good for this job:
paste
适合这份工作:
paste -d " " - - < filename
回答by Kent
awk:
awk:
awk 'NR%2{printf "%s ",sed 'N;s/\n/ /' yourFile
;next;}1' yourFile
note, there is an empty line at the end of output.
请注意,输出末尾有一个空行。
sed:
sed:
xargs -n2 -d'\n'
回答by nnog
Alternative to sed, awk, grep:
sed、awk、grep 的替代:
awk '{key=while read line1; do read line2; echo "$line1, $line2"; done < data.txt
; getline; print key ", " sed -n '/KEY/{
N
s/\n/ /p
}' somefile.txt
;}'
This is best when you want to join N lines and you only need space delimited output.
当您想要连接 N 行并且您只需要空格分隔的输出时,这是最好的。
My original answer was xargs -n2
which separates on words rather than lines. -d
can be used to split the input by any single character.
我最初的答案是xargs -n2
用单词而不是行分隔。-d
可用于按任何单个字符拆分输入。
回答by ghoti
There are more ways to kill a dog than hanging. [1]
杀死狗的方法比绞死的要多。[1]
awk 'ORS=NR%2?FS:RS' file
Put whatever delimiter you like inside the quotes.
将您喜欢的任何分隔符放在引号内。
References:
参考:
- Originally "Plenty of ways to skin the cat", reverted to an older, potentially originating expression that also has nothing to do with pets.
- 最初是“给猫剥皮的多种方法”,后来恢复为一个较旧的、可能起源的表达,也与宠物无关。
回答by Hai Vu
Here is my solution in bash:
这是我在 bash 中的解决方案:
$ cat file
KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1
回答by J.D.
Although it seems the previous solutions would work, if a single anomaly occurs in the document the output would go to pieces. Below is a bit safer.
尽管以前的解决方案似乎可行,但如果文档中出现单个异常,输出将变得碎片化。下面稍微安全一点。
$ awk 'ORS=NR%2?FS:RS' file
KEY 4048:1736 string 3
KEY 0:1772 string 1
KEY 4192:1349 string 1
KEY 7329:2407 string 2
KEY 0:1774 string 1
回答by jaypal singh
Here is another way with awk
:
这是另一种方式awk
:
awk '{ ORS = (NR%2 ? FS : RS) } 1' file
awk '{ ORS = (NR%2 ? "," : RS) } 1' file
ex -c "%g/KEY/j" -c "wq" data.txt
As indicated by Ed Mortonin the comments, it is better to add braces for safety and parens for portability.
正如Ed Morton在评论中指出的那样,最好添加括号以确保安全并添加括号以提高便携性。
awk '{ if (NR%2 != 0) line=perl -0pe 's/(.*)\n(.*)\n/ \n/g' file.txt
; else {printf("%s %s\n", line, ##代码##); line="";} } \
END {if (length(line)) print line;}' flle
ORS
stands for Output Record Separator. What we are doing here is testing a condition using the NR
which stores the line number. If the modulo of NR
is a true value (>0) then we set the Output Field Separator to the value of FS
(Field Separator) which by default is space, else we assign the value of RS
(Record Separator) which is newline.
ORS
代表输出记录分隔符。我们在这里所做的是使用NR
存储行号的来测试条件。如果模数为NR
真值 (>0),那么我们将输出字段分隔符设置为FS
(Field Separator)的值,默认情况下为空格,否则我们分配RS
(Record Separator)的值,即换行符。
If you wish to add ,
as the separator then use the following:
如果您希望添加,
为分隔符,请使用以下内容:
回答by Justin
"ex" is a scriptable line editor that is in the same family as sed, awk, grep, etc. I think it might be what you are looking for. Many modern vi clone/successors also have a vi mode.
“ex”是一个可编写脚本的行编辑器,与 sed、awk、grep 等属于同一家族。我认为它可能正是您要找的。许多现代 vi 克隆/后继者也有 vi 模式。
##代码##This says for each line, if it matches "KEY" perform a join of the following line. After that command completes (against all lines), issue a write and quit.
这说的每一行,如果匹配“KEY”执行ĴOIN以下行的。该命令完成(对所有线)后,发出W¯¯仪式和qUIT。
回答by anubhava
You can use awk like this to combine ever 2 pair of lines:
您可以像这样使用 awk 来组合两对线:
##代码##回答by andrefs
If Perl is an option, you can try:
如果 Perl 是一个选项,您可以尝试:
##代码##