Reputation: 1481
I've got a rather large set of CSV's that I need to parse. Most of it is extremely easy, however I've got some 'group' objects with embedded objects that I need to extract correctly.
The file looks something like this
Test_GroupA,Group,-,-,-,-,NodeA,,-,
,,,,,,NodeB,,,
,,,,,,NodeC,,,
,,,,,,NodeD,,,
,,,,,,NodeE,,,
Test_GroupB,Group,-,-,-,-,NodeA,,-,
,,,,,,NodeB,,,
,,,,,,NodeC,,,
,,,,,,NodeX,,,
,,,,,,NodeE,,,
,,,,,,NodeF,,,
So, as you can see, I need something along the lines of:
awk -F"[,|]" '{if ($2=="Group")
then - pseudo code->
print "create group",$1
print "add member in $7 to group found in $1 of first row"
continue until you reach next $2=="Group"), then loop
This is perplexing me greatly :)
Edit:: It seems a lot of the values are somewhat bogus and contain '-' when they're blank instead of just being ,,
Something like
sed 's/\,\-\,/\,\,/g'
should replace them I'd think, however I think I need a leading wildcard.
New example:
grp-ext-test-test,Group,-,-,-,-,Net_10.10.10.10,,-,
,,,,,,Net_10.101.10.10,,,
,,,,,,ws-ext-test-10.102,,,
,,,,,,ws-ext-test-10.103,,,
,,,,,,ws-ext-test-10.104,,,
,,,,,,ws-ext-test-10.105,,,
,,,,,,ws-ext-test-10.106,,,
,,,,,,ws-ext-test-10.107,,,
,,,,,,ws-ext-test-10.108,,,
,,,,,,ws-ext-test-10.108,,,
Running the new string on it only produces:
create group grp-ext-test-test
Upvotes: 0
Views: 106
Reputation: 9936
You could try something like this and adapt as required..
awk -F, '$2=="Group"{g=$1; print "create group",g}{print "add " $7 " to " g}' file
Output:
create group Test_GroupA
add NodeA to Test_GroupA
add NodeB to Test_GroupA
add NodeC to Test_GroupA
add NodeD to Test_GroupA
add NodeE to Test_GroupA
create group Test_GroupB
add NodeA to Test_GroupB
add NodeB to Test_GroupB
add NodeC to Test_GroupB
add NodeX to Test_GroupB
add NodeE to Test_GroupB
add NodeF to Test_GroupB
---edit--- To check if the contents of $7 are valid you could try something like:
awk -F, '$2=="Group"{ g=$1; print "create group",g } $7!="-"{print "add " $7 " to " g}' file
Upvotes: 1