1

I have file as below;

roger
"supplier"
anger
"easter"
robin
"badguy"
sweety
"i like you"
goldy
"I hate you"

I need this file to be converted into something like

roger|supplier
anger|easter
robin|badguy
sweety|i like you
goldy|I hate you

Please help, I am thing of putting it thru a awhile loop and read them into 2 different files and then concatenate the files. I know that's not a great idea hence posting for some suggestions.

shellter
  • 36,525
  • 7
  • 83
  • 90
user3040077
  • 71
  • 2
  • 10

2 Answers2

2

One way with awk:

$ awk 'NR%2{a=$0;next}{print a,$2}' FS='"' OFS='|' file
roger|supplier
anger|easter
robin|badguy
sweety|i like you
goldy|I hate you

Or slightly shorter with xargs and sed:

$ xargs -n2 < file | sed 's/ /|/'
roger|supplier
anger|easter
robin|badguy
sweety|i like you
goldy|I hate you

Or just sed:

$ sed '$!N;s/\n/|/;s/"//g' file
roger|supplier
anger|easter
robin|badguy
sweety|i like you
goldy|I hate you
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
0

How's this?

echo 'roger
"supplier"
anger
"easter"
robin
"badguy"
sweety
"i like you"
goldy
"I hate you"' \
| awk '{
   if (NR%2){
     printf("%s", $0)
   }
   else {
     gsub(/"/,"", $0);printf("|%s\n", $0)
   }
 }'

output

roger|supplier
anger|easter
robin|badguy
sweety|i like you
goldy|I hate you
shellter
  • 36,525
  • 7
  • 83
  • 90