开发者

Replace Line Feed with a Space in unix shell script

I have a text file containing some records. Each record is splitted in 开发者_StackOverflow社区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.


maybe this can work ?

cat FILE | xargs | sed "s/ ---- /\n---- /g"


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


And a much simpler approach would be this

cat text_file | tr '\n' ' ' | sed 's/ ---/\n---/g'


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"; }
)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜