开发者

Awk displays file name twice (?!)

Greetings.

I have files that contains 开发者_如何学运维eleven fields in the file name. Each file has a specific meaning. My job is to list each file name and load the info into a database table for reporting.

My awk code does this (files have VER suffix):

find . -name "*.VER" -exec ls '{}' ';' -printf %f\\t | awk -F"~" '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$0}' > somefile.tab

It works fine except that it does something weird with the file name ($0): it repeats it like so (the 2 last fields from the right):

RRR1    PRE DTV_PREP    PREP05  JGM15453.   26  P   H23-600 029416589165    20110216    RRR1~PRE~DTV_PREP~PREP05~JGM15453.~26~P~H23-600~029416589165~20110216~090353.VER    ./RRR1~PRE~DTV_PREP~PREP05~JGMDTV269~33~P~H21-200~029384120357~20110216~091829.VER

If I remove "%f" switch from the printf statement, it seems to work but if I leave it alone and remove $0, file name never gets displayed. What am missing here? Any ideas?

Thanks for your input.


awk isn't printing it out twice, find is due to the ls and the -printf. I don't see the reason for the -exec ls '{}' ';' anyway. You should be able to simply remove it like so:

find . -name "*.VER" -printf "%f" | awk -F"~" '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$11"\t"$0}'

By the way, you can greatly simplify you code like so:

find . -name "*.VER" -printf "%f" | awk -F'~' '{for(i=1;i<=NF;i++)printf("%s\t",$i)}1'

Note that you were missing the 11th field $11 in your code. Both my answers will print this out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜