How to convert rows to columns in unix

前端 未结 5 1025
离开以前
离开以前 2021-01-03 12:50

I want my Unix file output, which has each value output on a new line, to be converted into grouped rows as shown below.

Say my output file in Unix looks like this:

相关标签:
5条回答
  • 2021-01-03 13:02

    Kind of ugly, but doesn't require a shell script:

    cat outputfile | tr "\n" " " | sed -e 's/\W*\(\w*\) \(\w*\) \(\w*\) \(\w*\)\W*/\1 \2 \3 \4\n/g'
    
    0 讨论(0)
  • 2021-01-03 13:04

    This is a minimal awk solution:

    awk 'ORS=NR%4?" ":"\n"' input.txt 
    

    output

    jobname userid starttime endtime
    jobname2 userid starttime endtime
    

    (If you want to align the fields, pipe to column -t)

    0 讨论(0)
  • 2021-01-03 13:11

    If you are looking for a shell script, you can do this as the number of lines to be printed in the output seems to have to fixed length:

    while read line1; do
      read line2
      read line3
      read line4
      echo $line1 $line2 $line3 $line4 >>output
    done <file
    
    0 讨论(0)
  • 2021-01-03 13:15

    This will paste each four consecutive lines as four tab-delimited fields:

    cat source_file | paste - - - - > destination_file
    
    0 讨论(0)
  • 2021-01-03 13:17

    With GNU sed:

    tr '\n' ' ' < file | sed -r 's/(\w+ +){4}/&\n/g'
    
    0 讨论(0)
提交回复
热议问题