2

I have a text file containing some records. Each record is splitted in 4 rows (not always 4), like the example:

----
row1
row2
row3
row4
----
row1
etc...

Each row is ended with the Line Feed character (LF). Ok, I need to obtain the record in only one line, replacing the LF character with a Space, like in example:

---- row1 row2 row3 row4
---- row1 row2 ...etcetera

Any help or suggestion for the solution? Thanks in advance.

Possa
  • 2,067
  • 7
  • 20
  • 22

6 Answers6

8

maybe this can work ?

cat FILE | xargs | sed "s/ ---- /\n---- /g"
ajreal
  • 46,720
  • 11
  • 89
  • 119
5
tr  "\n" " "  <file | awk '{gsub(/--+/,"\n&");print}'

or all in one awk

awk '/--/{print s;printf $0;s=""}!/--/{s=s" "$0}END{print s}' file
kurumi
  • 25,121
  • 5
  • 44
  • 52
2

And a much simpler approach would be this

cat text_file | tr '\n' ' ' | sed 's/ ---/\n---/g'
fancyPants
  • 50,732
  • 33
  • 89
  • 96
0

You need to know what exactly is the separator between the records. In your example it looks like it's '----', but you also said that there is a variable number of records.

Anyway, things like that are best done using code like this:

cat source | (
  acc=""
  while read -r line; do
  if test "$line" = "----" -a -n "$acc"; then
    echo "$acc"
    acc="$line"
  else
    test -n "$acc" && { acc="$acc "; }
    acc="${acc}$line"
  fi
  done
  test -n "$acc" && { echo "$acc"; }
)
Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
0

Use awk for this rather than a shell script

Iterating through a text file and doing different things based on the line contents, is precisely what awk was designed to do.

In the 21st century shell scripts should be kept simple and other tools used for complex logic.

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106
  • Why so complicated? awk is more complicated than sed, sed is more complicated than tr. – fancyPants Feb 18 '11 at 08:56
  • @tombom, on the contrary, awk is much simpler to use than sed. – kurumi Feb 18 '11 at 09:33
  • @kurumi I don't think so. What would be the solution for this with awk? Then consider how much you need to read and learn about awk, assuming you don't know anything about awk and compare the same with sed. sed wins this by far. – fancyPants Feb 18 '11 at 09:52
  • @tombom, there are many other things `sed` cannot do as easily as awk. consider doing maths, parsing fields in a csv file, table lookups, etc. Learning awk is more useful than learning sed. – kurumi Feb 18 '11 at 10:04
  • @kurumi That might be right, I'm not that familiar with awk. But it's about this particular problem here. And certainly this is solved with sed (and tr, see my answer) faster than with awk (again considering how much you have to learn). – fancyPants Feb 18 '11 at 10:42
  • @kurumi Saw your answer right now and I think it proves my point. If I'd see it without knowing the task, I'd have no idea what it would do. If I'd like to understand it quick, I sure would have to google a very lot. – fancyPants Feb 18 '11 at 10:48
  • @tombom, well, if the domain is with this question, then using awk is no more complicated than using sed, since I saw in your answer you are using `tr` besides just `sed`. If you are using just `sed`, then it will be more complicated as well. – kurumi Feb 18 '11 at 10:56
0

awk 'BEGIN {RS="----"; FS="\n"; OFS=" "} FNR==1 {next} {$1=RS $1; print}' input.file

glenn jackman
  • 238,783
  • 38
  • 220
  • 352