如何在 linux 的文件中连接两行不同模式的行?
How can I join two lines with different patterns in file in linux?
我想加入一个文件中的两行。例如,如下所示,我想加入第一行和第二行,分别有谚语和作者姓名。我想加入文件中的所有类似事件。
我可以使用 Shift+J 手动加入它们,但是有将近 10000 行,而且很难做到。
- ,119., 120., 是也出现在行中的原始源代码行号。
所以我想搜索并加入所有行,这些行在行首有一个数字,然后是一个点,然后是一个 space,然后是文本..^[0-9]*。 (118. ) 和下一行在行首没有数字。所以加入他们。
我到处搜索并尝试实现它但没有用。
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
应该这样做:
awk '/^[0-9]+\./ { if (last) print last; last = [=10=]; next }
{ print last, [=10=]; last = "" }'
给定的数据文件:
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
产生输出:
118. People don't care how much you know until they know how much they care. John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm. Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. - Martin Luther King, Jr.
该代码假定只有一个续行。如果您可以有多个续行,那么您需要一个更复杂的脚本。
$ cat new.data
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
123. More than one line of data causes trouble for the basic script.
A more complex script can deal with those too. -
Jonathan Leffler
$ awk '/^[0-9]+\./ { if (last) print last; last = [=13=]; next }
> { last = last " " [=13=] }
> END { if (last) print last }' new.data
118. People don't care how much you know until they know how much they care. John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm. Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. - Martin Luther King, Jr.
123. More than one line of data causes trouble for the basic script. A more complex script can deal with those too. - Jonathan Leffler
$
:%s/\n\%(\d\+\. \)\@!
应该在 vim 完成。此命令适用于多个连续行,但仅删除换行符;它不会插入空格或任何东西。
在 awk 中,"no empty lines and **first line** is present in file",前导 space 也期望:
$ awk '{printf "%s%s", ( ~ /^ *\*\*/? (NR>1?ORS:"") : OFS), [=10=]} END {printf ORS}' file
.
{ # finish previous with ORS if current starts with a number and output it
printf "%s%s", ( ~ /^ *\*\*/? (NR>1?ORS:"") : OFS), [=11=]
}
END {printf ORS} # closing ORS
这可能适合您 (GNU sed):
sed 'N;/\n[0-9]/!s/\n//;P;D' file
读取两行,如果第二行不是以数字开头,则删除换行符。
另一种方式:
sed 'N;s/\n\([^0-9]\)//;P;D' file
我想加入一个文件中的两行。例如,如下所示,我想加入第一行和第二行,分别有谚语和作者姓名。我想加入文件中的所有类似事件。
我可以使用 Shift+J 手动加入它们,但是有将近 10000 行,而且很难做到。
- ,119., 120., 是也出现在行中的原始源代码行号。
所以我想搜索并加入所有行,这些行在行首有一个数字,然后是一个点,然后是一个 space,然后是文本..^[0-9]*。 (118. ) 和下一行在行首没有数字。所以加入他们。
我到处搜索并尝试实现它但没有用。
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
应该这样做:
awk '/^[0-9]+\./ { if (last) print last; last = [=10=]; next }
{ print last, [=10=]; last = "" }'
给定的数据文件:
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
产生输出:
118. People don't care how much you know until they know how much they care. John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm. Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. - Martin Luther King, Jr.
该代码假定只有一个续行。如果您可以有多个续行,那么您需要一个更复杂的脚本。
$ cat new.data
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
123. More than one line of data causes trouble for the basic script.
A more complex script can deal with those too. -
Jonathan Leffler
$ awk '/^[0-9]+\./ { if (last) print last; last = [=13=]; next }
> { last = last " " [=13=] }
> END { if (last) print last }' new.data
118. People don't care how much you know until they know how much they care. John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm. Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. - Martin Luther King, Jr.
123. More than one line of data causes trouble for the basic script. A more complex script can deal with those too. - Jonathan Leffler
$
:%s/\n\%(\d\+\. \)\@!
应该在 vim 完成。此命令适用于多个连续行,但仅删除换行符;它不会插入空格或任何东西。
在 awk 中,"no empty lines and **first line** is present in file",前导 space 也期望:
$ awk '{printf "%s%s", ( ~ /^ *\*\*/? (NR>1?ORS:"") : OFS), [=10=]} END {printf ORS}' file
.
{ # finish previous with ORS if current starts with a number and output it
printf "%s%s", ( ~ /^ *\*\*/? (NR>1?ORS:"") : OFS), [=11=]
}
END {printf ORS} # closing ORS
这可能适合您 (GNU sed):
sed 'N;/\n[0-9]/!s/\n//;P;D' file
读取两行,如果第二行不是以数字开头,则删除换行符。
另一种方式:
sed 'N;s/\n\([^0-9]\)//;P;D' file