UNIX SED – Convert Vertical List to Horizontal CSV

| |

If you’ve had to take a vertical list to horizontal list before you know how difficult it can be.

You may have a different setup than my example but the code provided below should help in most cases.

==================================================
Origin URL        | http://kittell.net
Action URL        | http://kittell.net/api
User Name Field   | username
Password Field    | password
User Name         | kittell1
Password          | SomethingSecret
Created Time      | 12/9/2015 2:20:52 PM
Password Strength | Very Strong
Password File     | C:\Users\dkittell\database
==================================================

==================================================
Origin URL        | http://kittell.net
Action URL        | http://kittell.net/api
User Name Field   | username
Password Field    | password
User Name         | kittell2
Password          | SomethingSecret
Created Time      | 12/9/2015 2:20:52 PM
Password Strength | Very Strong
Password File     | C:\Users\dkittell\database
==================================================

The code below is still a work in progress but does what I need it to for now, it’s very ugly but it works.

rm -f temp1
head -11 list.txt | sed '/==================================================/d' > temp
sed -i -e 's/,$/\n/' temp

while read line
do
echo $line | cut -f1 -d '|' | sed 's/[ \t]*$//'
#echo $sHeaders
done < temp > temp1

tr -s '\r\n' ',' < temp1 > listheaders.txt

tr -s '\r\n' ',' < list.txt | sed -e 's/,$/\n/' > temp | tr -s '==================================================' '\n' < temp > listd.txt

sed -i -e "s/Origin URL        | //g" listd.txt
sed -i -e "s/Action URL        | //g" listd.txt
sed -i -e "s/User Name Field   | //g" listd.txt
sed -i -e "s/Password Field    | //g" listd.txt
sed -i -e "s/User Name         | //g" listd.txt
sed -i -e "s/Password          | //g" listd.txt
sed -i -e "s/Created Time      | //g" listd.txt
sed -i -e "s/Password Strength | //g" listd.txt
sed -i -e "s/Password File     | //g" listd.txt

cat listheaders.txt listd.txt > listc.txt && rm -f listheaders.txt listd.txt temp temp-e temp1
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.