How to concatenate multiple lines of output to one line?

前端 未结 11 1382
失恋的感觉
失恋的感觉 2020-12-04 07:08

If I run the command cat file | grep pattern, I get many lines of output. How do you concatenate all lines into one line, effectively replacing each \"\\n

11条回答
  •  生来不讨喜
    2020-12-04 07:56

    Here is another simple method using awk:

    # cat > file.txt
    a
    b
    c
    
    # cat file.txt | awk '{ printf("%s ", $0) }'
    a b c
    

    Also, if your file has columns, this gives an easy way to concatenate only certain columns:

    # cat > cols.txt
    a b c
    d e f
    
    # cat cols.txt | awk '{ printf("%s ", $2) }'
    b e
    

提交回复
热议问题