Join multiple lines into One (.cap file) CentOS

46 views Asked by At

Single entry has multiple lines. Each entry is separated by two blank lines. Each entry has to be made into a single line followed by a delimiter(;).

Sample Input:

Name:Sid
ID:123


Name:Jai
ID:234


Name:Arun
ID:12

Tried replacing the blank lines with cat test.cap | tr -s [:space:] ';'

Output:

Name:Sid;ID:123;Name:Jai;ID:234;Name:Arun;ID:12;

Expected Output:

Name:SidID:123;Name:JaiID:234;Name:ArunID:12;

Same is the case with Xargs.

I've used sed command as well but it only joined two lines into one. Where as I've 132 lines as one entry and 1000 such entries in one file.

3

There are 3 answers

1
Wiktor Stribiżew On BEST ANSWER

You may use

cat file | awk  'BEGIN { FS = "\n"; RS = "\n\n"; ORS=";" } { gsub(/\n/, "", $0); print }' | sed 's/;;*$//' > output.file

Output:

Name:SidID:123;Name:JaiID:234;Name:ArunID:12

Notes:

  • FS = "\n" will set field separators to a newline`
  • RS = "\n\n" will set your record separators to double newline
  • gsub(/\n/, "", $0) will remove all newlines from a found record
  • sed 's/;;*$//' will remove the trailing ; added by awk

See the online demo

5
RavinderSingh13 On

Could you please try following.

awk 'NF{val=(val?$0~/^ID/?val $0";":val $0:$0)} END{print val}' Input_file

Output will be as follows.

Name:SidID:123;Name:JaiID:234;Name:ArunID:12;

Explanation: Adding explanation of above code too now.

awk '                                    ##Starting awk program here.
NF{                                      ##Checking condition if a LINE is NOT NULL and having some value in it.
  val=(val?$0~/^ID/?val $0";":val $0:$0) ##Creating a variable val here whose value is concatenating its own value along with check if a line starts with string ID then add a semi colon at last else no need to add it then.
}
END{                                     ##Starting END section of awk here.
  print val                              ##Printing value of variable val here.
}
'  Input_file                            ##Mentioning Input_file name here.
0
potong On

This might work for you (GNU sed):

sed -r '/./{N;s/\n//;H};$!d;x;s/.//;s/\n|$/;/g' file

If it is not a blank line, append the following line and remove the newline between them. Append the result to the hold space and if it is not the end of the file, delete the current line. At the end of the file, swap to the hold space, remove the first character (which will be a newline) and then replace all newlines (append an extra semi-colon for the last line only) with semi-colons.