user3040077
user3040077

Reputation: 71

Make lines from a file into rows

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.

Upvotes: 1

Views: 88

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85875

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

Upvotes: 2

shellter
shellter

Reputation: 37298

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

Upvotes: 0

Related Questions