我有一个如下格式的文本文件。第一行是“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

我需要这个值和键在同一行。所以输出应该是这样的…

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

如果我可以使用一些分隔符,如$或:

KEY 4048:1736 string , 3

我如何将两条线合并成一条?


当前回答

试试下面这句话:

while read line1; do read line2; echo "$line1 $line2"; done <old.txt>new_file

在中间放置分隔符

"$line1 $line2";

例如,如果分隔符是|,那么:

"$line1|$line2";

其他回答

更通用的解决方案(允许连接多个后续行)是shell脚本。这在每一个之间增加了一条线,因为我需要可见性,但这很容易补救。在这个例子中,“key”行以:结尾,而其他行没有。

#!/bin/bash
#
# join "The rest of the story" when the first line of each   story
# matches $PATTERN
# Nice for looking for specific changes in bart output
#

PATTERN='*:';
LINEOUT=""
while read line; do
    case $line in
        $PATTERN)
                echo ""
                echo $LINEOUT
                LINEOUT="$line"
                        ;;
        "")
                LINEOUT=""
                echo ""
                ;;

        *)      LINEOUT="$LINEOUT $line"
                ;;
    esac        
done

杀死一只狗的方法不止绞刑。[1]

awk '{key=$0; getline; print key ", " $0;}'

在引号内放入您喜欢的任何分隔符。


引用:

原本是“Plenty of ways to skin the cat”,后来变成了一个古老的、可能起源于宠物的表达,也和宠物无关。

下面是另一种使用awk的方法:

awk 'ORS=NR%2?FS:RS' file

$ 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 $ 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 As indicated by Ed Morton in the comments, it is better to add braces for safety and parens for portability. awk '{ ORS = (NR%2 ? FS : RS) } 1' file 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. If you wish to add , as the separator then use the following: awk '{ ORS = (NR%2 ? "," : RS) } 1' file

"ex"是一个可脚本化的行编辑器,与sed、awk、grep等属于同一家族。我觉得这可能就是你要找的东西。许多现代vi克隆/后继者也有一个vi模式。

 ex -c "%g/KEY/j" -c "wq" data.txt

这就是说,对于每一行,如果它匹配"KEY"执行下一行的j。该命令完成后(针对所有行),发出一个w rite和q uit。

perl -0pE 's{^KEY.*?\K\s+(\d+)$}{ $1}msg;' data.txt > data_merged-lines.txt

-0读取整个文件,而不是逐行读取; pE用循环包装代码并打印输出,详见http://perldoc.perl.org/perlrun.html; ^KEY匹配行首的“KEY”,后面是序列前任何(.*?)的非贪婪匹配

一个或多个空格\s+,包括换行符; 一个或多个数字(\d+),我们捕获并重新插入为$1;

后跟$的末尾。

\K方便地从替换中排除左边的所有内容,因此{$1}只替换1-2个序列,参见http://perldoc.perl.org/perlre.html。