简体   繁体   中英

How can I join two lines with different patterns in file in linux?

I want to join lines in a file which have been written on two lines. for example, as shown below, I want to join first and second line which have a proverb and name of author respectively. All similar occurrences I want to join in a file.

I can join them manually, by using Shift+J but there are almost 10000 lines and It has become very difficult to do it.

  1. ,119., 120., are the original source line numbers that are also present in line.

So I want to search and join all lines which have got a number at starting of the line and then a dot and then a space and then text.. ^[0-9]*. (118. ) and next line having no number at the starting of line. So join them.

I have searched everywhere and tried to implement it but of no use.

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.

This should do it:

awk '/^[0-9]+\./ { if (last) print last; last = $0; next }
                 { print last, $0; last = "" }'

Given data file:

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.

That produces the output:

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.

The code does assume there's only ever one continuation line. If you can have multiple continuation lines, then you need a more complex script.

$ 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 = $0; next }
>                  { last = last " " $0 }
>      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\\+\\. \\)\\@! should do it in vim. This command works with multiple continuation lines but only removes newlines; it doesn't insert spaces or anything.

In awk, "no empty lines and **first line** is present in file", leading space also expected:

$ awk '{printf "%s%s", ($1 ~ /^ *\*\*/? (NR>1?ORS:"") : OFS), $0} END {printf ORS}' file

.

{   # finish previous with ORS if current starts with a number and output it
    printf "%s%s", ($1 ~ /^ *\*\*/? (NR>1?ORS:"") : OFS), $0
} 
END {printf ORS} # closing ORS

This might work for you (GNU sed):

sed 'N;/\n[0-9]/!s/\n//;P;D' file

Read two lines and if the second line does not start with a number, remove the newline.

Another way:

sed 'N;s/\n\([^0-9]\)/\1/;P;D' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM