AWK解析的文件名,并添加结果到每一行的末尾 [英] awk parse filename and add result to the end of each line
本文介绍了AWK解析的文件名,并添加结果到每一行的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个有像
相似名称的文件数 DWH_Export_AUSTA_20120701_20120731_v1_1.csv.397.dat.2012-10-02 04-01-46.out
DWH_Export_AUSTA_20120701_20120731_v1_2.csv.397.dat.2012-10-02 04-03-12.out
DWH_Export_AUSTA_20120801_20120831_v1_1.csv.397.dat.2012-10-02 04-04-16.out
等。
我需要的.csv之前得到数字(1或2)从文件名,并将其放入每行结束文件,TAB分隔符。
我写了这个code,它发现我需要的数字,但我不知道如何把这个数字到文件中。有文件名的空间,我的剧本打破了的缘故吧。
另外,我不知道,如何发送到文件的脚本列表。现在我有一个文件唯一的工作。
我的code:
#!/ bin / sh的
字符串=DWH_Export_AUSTA_20120701_20120731_v1_1.csv.397.dat.2012-10-02 04-01-46.out
出= $(回声$字符串| awk的'BEGIN {FS =_} {打印SUBSTR(7,0,1 $)}')
AWK'{$打印0\\ T $出}'$字符串
解决方案
在文件*
做
SFX = $(回声$文件| sed的'。:S /.*_ \\(* \\)CSV * / \\ 1 /')
SED -iS / $ / \\ T $ SFX /$文件
DONE
I have number of files which have similar names like
DWH_Export_AUSTA_20120701_20120731_v1_1.csv.397.dat.2012-10-02 04-01-46.out
DWH_Export_AUSTA_20120701_20120731_v1_2.csv.397.dat.2012-10-02 04-03-12.out
DWH_Export_AUSTA_20120801_20120831_v1_1.csv.397.dat.2012-10-02 04-04-16.out
etc.
I need to get number before .csv(1 or 2) from the file name and put it into end of every line in file with TAB separator.
I have written this code, it finds number that I need, but i do not know how to put this number into file. There is space in the filename, my script breaks because of it.
Also I am not sure, how to send to script list of files. Now I am working only with one file.
My code:
#!/bin/sh
string="DWH_Export_AUSTA_20120701_20120731_v1_1.csv.397.dat.2012-10-02 04-01-46.out"
out=$(echo $string | awk 'BEGIN {FS="_"};{print substr ($7,0,1)}')
awk ' { print $0"\t$out" } ' $string
解决方案
for file in *
do
sfx=$(echo "$file" | sed 's/.*_\(.*\).csv.*/\1/')
sed -i "s/$/\t$sfx/" "$file"
done
这篇关于AWK解析的文件名,并添加结果到每一行的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文