i already have a .csv file with this structure:

2223178555, Joshep, 22.36

and i would like to add this entry taken from a .dat file that looks like this:

PN: 2221471162
size: 14.22

so the .csv looks like this:

2223178555, Joshep, 22.36
2221471162, Cesar, 14.22

Could you please help me with this?


using the marked solution this is what I've got:

2223178555, Joshep, 22.36

I've tried placing "\t" before printf's

awk 'NR==1{serial_number=$2}NR==2{device_id=$0}NR==3{print serial_number",\t"device_id",\t"  $2}' Disptelnet.dat >>dispositivoss.csv

and this is what i get:

2223178555, Joshep, 22.36
2221471162,   Cesar,    14.22

I've also tried with \t, \s \k

  • is there just one entry in the .dat file? if not, how are they separated? – Jeff Schaller Apr 5 '17 at 17:46
  • the .dat file only contains one entry, the .csv could contain 0 to n entries. both files are exactly as in the post. line number 2 in .dat file does not has a "name label" but name is always there. – Cesar Villegas Apr 5 '17 at 17:53

One simplistic approach, using awk

awk 'NR==1{phone=$2}NR==2{name=$0}NR==3{print phone","name","$2}' cesar.dat >>phones.csv
| improve this answer | |
  • this worked perfectly, but i would like to keep the spaces before name and size, i just update the question in order to be clear – Cesar Villegas Apr 6 '17 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.