-2

I am trying to solve one competitive question, where I'm stuck at below scenario can some one help me out to understand how the output can be achieved

Explanation : Every group of three consecutive rows should be folded into one

[ Output is shared below ]

Data in file :

Abc 123,
zyxhj pqr
raj
ram:
vilas, 
1234
jkal
yui
gshj

Output :

Abc 123,zyxhj pqr raj
ram:vilas,1234
jkal yui gshj
αғsнιη
  • 41,407

1 Answers1

2

Using xargs:

xargs -d'\n' -n3 <infile

read and print every 3 lines according to the \newline as delimiter


Using paste:

paste -d ' ' - - - <infile

paste data in 3 columns with space delimiter


Using awk:

awk '{ printf "%s%s", $0, (NR%3?OFS:ORS) }' infile

printf every line and then print OFS (Output Field Separator; space by default) if NR (Number of Record) was not module of 3 else ORS (Output Record Separator; newline by default) if it was.

αғsнιη
  • 41,407