0

If sample.txt has following data

Employee_id 101
Dept_id 10
Salary 2000

Employee_id 102
Dept_id 20
Salary 3000

Employee_id 103
Dept_id 30
Salary 3000

I would like to see the output as following

Employee_id 101 Dept_id 10 Salary 2000
Employee_id 102 Dept_id 20 Salary 3000
Employee_id 103 Dept_id 30 Salary 3000
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
redtape2015
  • 85
  • 2
  • 8

3 Answers3

6

A simple way using awk:

$ awk -v RS= '{$1=$1}1' file
Employee_id 101 Dept_id 10 Salary 2000
Employee_id 102 Dept_id 20 Salary 3000
Employee_id 103 Dept_id 30 Salary 3000

The record separator RS is unset, so that each block is treated as a record. $1=$1 causes awk to touch each record, so that it is reformatted into fields separated by spaces. The 1 at the end is a shorthand for {print}.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • You're welcome. If your problem is solved, don't forget to accept an answer by clicking on the tick mark next to it. – Tom Fenech May 22 '15 at 08:05
0

You could use sed ':a;N;$!ba;s/\n/ /g' yourfile as suggested here: How can I replace a newline (\n) using sed?

Community
  • 1
  • 1
WilliamSF
  • 284
  • 1
  • 8
0
awk 'NF' file | paste - - -

will do it. The AWK command removes the blank lines, and the paste command separates the 3 strings with a tab character. To have the 3 bits separated with a space, use

paste -d ' ' - - -
user2138595
  • 187
  • 7