bash - Awk or Sed to join multiple lines separated with blank line -


i managed create file needs formatted csv import:

here records:

(202) 111-0000 1 full name street address city, state zip  (212) 222-9999 2 full name street address city, state zip  (312) 888-2222 3 full name street address city, state zip 

etc

i looking transpose this:

(202) 111-0000,1,full name,street address,city,state,zip (212) 222-9999,2,full name,street address,city,state,zip (312) 888-2222,3,full name,street address,city,state,zip 

notice how city state , zip have been comma delimited in desired output.

any awk or sed gurus willing me out this?

thanks much.

i assume have access gnu awk , sed.

in order zip part right, let's use sample input:

$ cat file (202) 111-0000 1 full name street address city, state 10023  (212) 222-9999 2 full name street address city, state 10023  (312) 888-2222 3 full name street address city, state 10023 

we can transpose follows:

$ awk -v rs="" -f'\n' -v ofs=, '{$1=$1} 1' file | sed -r 's/ +([[:digit:]-]+)$/, \1/' (202) 111-0000,1,full name,street address,city, state, 10023 (212) 222-9999,2,full name,street address,city, state, 10023 (312) 888-2222,3,full name,street address,city, state, 10023 

eliminating spaces

if spaces above problem, try:

$ awk -v rs="" -f'\n' -v ofs=, '{$1=$1} 1' file | sed -r 's/ +([[:digit:]-]+)$/, \1/; s/, +/,/g' (202) 111-0000,1,full name,street address,city,state,10023 (212) 222-9999,2,full name,street address,city,state,10023 (312) 888-2222,3,full name,street address,city,state,10023 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -