How does awk convert a file to libsvm format?
I have a file whose first two lines look like this:
"price" "spec.long" "spec.short" "com.long" "com.short" "small.long" "small.short"
"1" 10.8 10270 -4069 57894 -76045 58818 -46868
I'd like to eliminate the first line, eliminate the entire column within quotes, eliminate the quotes and have th开发者_JAVA技巧e result appear as follows:
10.8 1:10270 2:-4069 3:57894 4:-76045 5:58818 6:-46868
I'm considering if it's worth learning awk to do tasks like this and similar ones. Or if my time is better spent solving this in perl, ruby or python.
No need to hardcode the indexes:
awk 'NR>1 {
printf("%s ",$2)
for (i=3; i<=NF; i++) printf("%d:%s ", i-2, $i)
print ""
}' filename
The simplest way to get rid of the first line is just grep it out. Then the rest can be hardcoded, assuming the whole file looks like this.
grep -v price file | awk '{print $2 " 1:" $3 " 2:" $4 " 3:" $5 "4:" $6 " 5:" $7 " 6:" $8}'
精彩评论