1

i need help. I have file vm.list:

VM-NAME1|WEEKDAY|2| VM-NAME2|WEEKDAY|4| VM-NAME3|WEEKDAY|3| VM-NAME4|WEEKDAY|4| VM-NAME5|WEEKDAY|4| VM-NAME6|WEEKDAY|1| VM-NAME7|WEEKDAY|1| VM-NAME8|WEEKDAY|4| VM-NAME9|WEEKDAY|2| VM-NAME10|WEEKDAY|4| VM-NAME11|WEEKDAY|4| 

I need list divide into new lists depending of 3 value and action run:

LIST1: VM-NAME6 VM-NAME7 LIST2: VM-NAME1 VM-NAME9 LIST3: VM-NAME3 LIST4: VM-NAME2 VM-NAME4 VM-NAME5 VM-NAME8 VM-NAME10 VM-NAME11 

Just about it

for i in $(awk -F "|" '{print $3}' today.list | sort | uniq) do echo $i awk -F "|" '{ if ($3 == '$i') print $1 }' today.list done 

i understand what it incorrect, but i don't have ideas

3 Answers 3

4

give this awk one-liner a try:

awk -F'|' '{a[$3]=a[$3]RS$1}END{for(x in a)print "List"x":" a[x]}' file 
Sign up to request clarification or add additional context in comments.

2 Comments

Cool :) This won't sort the output though.
@Nikita : In fact,it can't be any better than this. :)
0

A version that expects the source file being in order by cols 3 and 1 (handled by sort in process substitution).

$ awk -F\| '{ print q ($3!=p?ORS "LIST" $3 ":":"") }{ p=$3; q=$1 }' <(sort -t\| -k3 -k1 file) LIST1: VM-NAME6 VM-NAME7 LIST2: VM-NAME1 VM-NAME9 LIST3: VM-NAME3 LIST4: VM-NAME10 VM-NAME11 VM-NAME2 VM-NAME4 VM-NAME5 

It won't store the whole record set in memory.

Comments

0

If you want the output in separate files, named List{1..4}, you can use this:

$ awk -F'|' '{print $1 > "List" $3}' vm.list $ head -n100 List* # just a quick hack to print them with a header... ==> List1 <== VM-NAME6 VM-NAME7 ==> List2 <== VM-NAME1 VM-NAME9 ==> List3 <== VM-NAME3 ==> List4 <== VM-NAME2 VM-NAME4 VM-NAME5 VM-NAME8 VM-NAME10 VM-NAME11 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.