Vishal Mahajan Vishal Mahajan - 2 months ago 10
Linux Question

How to join two specific lines with different patterns in file in linux using vi or SED or awk or any other linux command?

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.

**First line** - 118. People don't care how much you know until they know how much they care.

John C. Maxwell

**Third Line** - 119. A life lived in fear is a life half lived. - Proverb

**Fourth Line** - 120. Nothing great was ever achieved without enthusiasm.

Ralph Waldo Emerson

**Sixth Line** - 121. Damn the torpedoes, full speed ahead. - David Farragut

**Seventh line** - 122. Our lives begin to end the day we become silent about things that matter. -

Martin Luther King, Jr.

Answer

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
$