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'.

1
  • 2
    Didn't you just ask this exact same question a few days ago? See unix.stackexchange.com/q/537908/133219. Or is this homework for a class you and some other people are taking? Commented Sep 2, 2019 at 18:52

2 Answers 2

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.

2
  • Great, would you explain the code? Commented Sep 2, 2019 at 18:24
  • Well done, but you need to put explanations in for credit. It's the way it works on this site. Commented Sep 2, 2019 at 18:46
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.

2
  • Sorry for addition but it is possible to print particular column instead of whole line. Update will very helpful Commented Sep 3, 2019 at 8:30
  • @KalpeshBhoj please edit your question - or ask another. You will need to be more specific than "print particular column". Commented Sep 3, 2019 at 9:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.