0

I have same question asked here in latest,but i have something different problem. Ex:

Input file

******************
.WER
+ aaa bbb ccc
+ ddd eee 
******************
.SDF
+ zzz xxx yyy 
+ iii  
+ kkk lll
******************
.XCV
+ uuu vvv ggg 
+ hhh qqq
******************

Desired Output:

******************
.WER aaa bbb ccc ddd eee
******************
.SDF zzz xxx yyy iii kkk lll
******************
.XCV uuu vvv ggg hhh qqq
******************

So as per above i have file where no of line is no certain. It can be 2,3 or 4 and every record separated with ****************** whether it is possible to convert all lines of each record in single line? For exact no of lines i have succesfully used paste -s -d ' \n'.

Freddy
  • 25,565
  • 2
    Didn't you just ask this exact same question a few days ago? See https://unix.stackexchange.com/q/537908/133219. Or is this homework for a class you and some other people are taking? – Ed Morton Sep 02 '19 at 18:52

2 Answers2

3
$ sed -e :a -e '$!N;s/ *\n+ / /;ta' -e 'P;D' testfile
******************
.WER aaa bbb ccc ddd eee 
******************
.SDF zzz xxx yyy iii kkk lll
******************
.XCV uuu vvv ggg hhh qqq
******************

http://sed.sourceforge.net/sed1line.txt holds a similar example.

  • :a create the label 'a'

  • $!N append the next line (and a newline) to the pattern space, IF it is not the last line ($!)

  • s/ *\n+ / / Replace trailing spaces, the newline, the + and the space after it with a single space

  • ta jump back to label a

  • Print the pattern space. This will produce double output, because we didn't use the -n-option to sed, so now, we need to

  • Delete the superfluous output.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
markgraf
  • 2,860
0

If you really want to use awk, then at least with GNU awk you could do something like

$ gawk -vRS='\n[*]+' -F'[ ]*\n[+][ ]*' '{NF+=0; ORS=RT} 1' file
******************
.WER aaa bbb ccc ddd eee 
******************
.SDF zzz xxx yyy iii kkk lll
******************
.XCV uuu vvv ggg hhh qqq
******************

which simply sets appropriate record and field separators, and forces re-assembly of the record with default (single space) field separators.

steeldriver
  • 81,074
  • Sorry for addition but it is possible to print particular column instead of whole line. Update will very helpful – Kalpesh Bhoj Sep 03 '19 at 08:30
  • @KalpeshBhoj please edit your question - or ask another. You will need to be more specific than "print particular column". – steeldriver Sep 03 '19 at 09:58