Merge lines with Awk

/ Published in: Awk
Save to your folder(s)

For use in bash script. First two lines are an example of usage. This is one of several solutions to this problem. Works well for cleaning database output. Takes a regex and two files as arguments - first one is raw db output - second is merged output.This assumes unwanted line breaks. You can also get the same results working with the newline characters -- will post that one as well. The --re-interval option is only necessary if you want to enable regex interval operators like {n} or {n,m} ....etc. If using a current version of GNU Awk, might as well leave it in there.

Copy this code and paste it in your HTML
  1. REGEX="/^[^|]/"
  2. func_merge_lines "$REGEX" $TMP1 $TMP2
  4. function func_merge_lines
  5. {
  6. awk --re-interval "NR==1 \
  7. { s=\$0;next } \
  8. "$1"{ s=s\$0;next } \
  9. { print s;s=\$0 } END {if(s)print s}" $2 > $3 # merge lines.
  10. # Looks for lines not starting with pipe
  11. # then merges line below to end.
  12. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.