wolverene13 |
12-19-2011 09:23 PM |
awk - rearrange column data in csv file to match columns
Hi,
I have a csv file that I created from two different device lists. One lists the devices in our trouble ticketing system, the other lists devices in our network monitoring application. In some cases, the device is in the ticketing system but not the network monitoring software, in some cases the device is in our network monitoring software, but not our ticketing system, and sometimes it is in both. I put the data in one csv file and I am trying to line up all of the records by IP address. The desired format when viewed with Open Office Calc (or Excel) is as follows (I changed the actual data to make it generic, but I can provide a snippet of the actual file if needed):
Code:
TS= Ticketing System
MS= Monitoring System
In backup | NodeID-TS | Name-TS | In TS | In TS but not MS | In Both | In MS but not TS | In MS | Name-TS | NodeID-MS
Yes 11111 node1 10.10.10.1 10.10.10.1 10.10.10.1 node_1 11111
No 22222 node2 10.10.10.2 10.10.10.2 10.10.10.2 node_2 22222
Yes 33333 node3 10.10.10.3 10.10.10.3
10.10.10.4 10.10.10.4 node_4 44444
Yes 55555 node5 10.10.10.5 10.10.10.5 10.10.10.5 node_5 55555
10.10.10.6 10.10.10.6 node_6 66666
No 77777 node7 10.10.10.7 10.10.10.7
Yes 88888 node8 10.10.10.8 10.10.10.8
10.10.10.9 10.10.10.9 node_9 99999
However, currently the "In MS" column is not lined up with the rest of the document and it looks like this:
Code:
In backup | NodeID-TS | Name-TS | In TS | In TS but not MS | In Both | In MS but not TS | In MS | Name-TS | NodeID-MS
Yes 11111 node1 10.10.10.1 10.10.10.1 10.10.10.1 node_1 11111
No 22222 node2 10.10.10.2 10.10.10.2 10.10.10.2 node_2 22222
Yes 33333 node3 10.10.10.3 10.10.10.3 10.10.10.4 10.10.10.4 node_4 44444
10.10.10.5 node_5 55555
Yes 55555 node5 10.10.10.5 10.10.10.5 10.10.10.6 10.10.10.6 node_6 66666
10.10.10.9 10.10.10.9 node_9 99999
No 77777 node7 10.10.10.7 10.10.10.7
Yes 88888 node8 10.10.10.8 10.10.10.8
Here's what it looks like as a .csv file in a text editor, for sample purposes:
Code:
In backup,NodeID-TS,Name-TS,In TS,In TS but not MS,In Both,In MS but not TS,In MS,Name-TS,NodeID-MS
"Yes",11111,"node1","10.10.10.1",,"10.10.10.1",,"10.10.10.1","node_1",11111
"No",22222,"node2","10.10.10.2",,"10.10.10.2",,"10.10.10.2","node_2",22222
"Yes",33333,"node3","10.10.10.3","10.10.10.3",,"10.10.10.4","10.10.10.4","node_4",44444
,,,,,,,"10.10.10.5","node_5",55555
"Yes",55555,"node5","10.10.10.5",,"10.10.10.5","10.10.10.6","10.10.10.6","node_6",66666
,,,,,,"10.10.10.9","10.10.10.9","node_9",99999
"No",77777,"node7","10.10.10.7","10.10.10.7",,,,,
"Yes",88888,"node8","10.10.10.8","10.10.10.8",,,,,
Basically I need it so that if a node is in both systems, the line pretty much stays the same. If it is in only one system, that IP and associated record information needs to have its own line. The IP's need to be in order so the records are easier to work with. So far, I've tried this:
Code:
#!/bin/bash
cat mes-SHEET_copy.csv | while read line
do
clmnA=$(echo $line | cut -d"," -f1)
clmnB=$(echo "$line" | cut -d"," -f2)
clmnC=$(echo "$line" | cut -d"," -f3)
clmnD=$(echo "$line" | cut -d"," -f4)
clmnE=$(echo "$line" | cut -d"," -f5)
clmnF=$(echo "$line" | cut -d"," -f6)
clmnG=$(echo "$line" | cut -d"," -f7)
clmnH=$(echo "$line" | cut -d"," -f8)
clmnI=$(echo "$line" | cut -d"," -f9)
clmnJ=$(echo "$line" | cut -d"," -f10)
if [ $clmnE != $clmnF ]; then
echo "$clmnA,$clmnB,$clmnC,$clmnD,$clmnE,$clmnF,,,,,"
elif [ $clmnF != $clmnG ]; then
echo ",,,,,,$clmnF,$clmnG,$clmnH,$clmnI,$clmnJ,"
elif [ -n $clmnF ]; then
echo "$clmnA,$clmnB,$clmnC,$clmnD,$clmnE,$clmnF,$clmnG,$clmnH,$clmnI,$clmnJ"
fi
done
and this:
Code:
#!/bin/bash
cat mes-SHEET_copy.csv | while read line
do
clmnF=$(echo "$line" | awk -F"," '{print $6}')
clmnG=$(echo "$line" | awk -F"," '{print $7}')
clmnH=$(echo "$line" | awk -F"," '{print $8}')
if [ $clmnF=$clmnH ]; then
echo $line
else echo $line | awk -F"," '{print $1,$2,$3,$4,$5,$6}'
fi
done
...but it has occurred to me that none of these will work, because it will not "save" the information that was already in columns 7, 8, and 9 and put it on the next line.
Anyone have any ideas? I really don't want to go through 7,000 lines manually. Thanks in advance.
|