我设置了(900)具有结构的文件:
XYZ-7, ABC987654321
Some text
[...]
Another Text
1) 8 [...] coordinates: 54°38,177’N, 13°33,573’E; 54°38,094’N, 13°33,237’E
2) 5 [...] coordinates: 54°39, 377’ N, 13°33,940’E, 54°39,242’N, 13°33,632’E
...
23) 2 [...] coordinates: 53°40,207’N, 14°32,522’E; 53°40,207’N, 14°32,557’E
[...]
Some other text我想用awk从他们那里提取数据。输出应该如下所示:
XYZ-7, ABC987654321
1) 8 54°38,177’N, 13°33,573’E; 54°38,094’N, 13°33,237’E
2) 5 54°39, 377’ N, 13°33,940’E, 54°39,242’N, 13°33,632’E
...
23) 2 53°40,207’N, 14°32,522’E; 53°40,207’N, 14°32,557’E我已经找到了如何分别获得第一行awk 'NR==1 {print $0};'和坐标awk '/^[0-9]{1,2}\)/ {print $1 " " $2 " " $5 " " $6 " " $7 " " $8};'的行,但是我不知道如何在一个命令中加入它们。
发布于 2017-06-09 11:14:24
迭代一组文件的awk解决方案:
for f in *; do
awk -v fn="result.txt" 'NR==1{ print >> fn }/coordinates:/{ print $1,$2,$5,$6,$7,$8 >> fn }' "$f";
done生成的文件result.txt内容如下所示:
...
XYZ-7, ABC987654321
1) 8 54°38,177’N, 13°33,573’E; 54°38,094’N, 13°33,237’E
2) 5 54°39, 377’ N, 13°33,940’E,
23) 2 53°40,207’N, 14°32,522’E; 53°40,207’N, 14°32,557’E
...发布于 2017-06-09 11:06:29
你可以试试这个awk
awk 'NR==1{print} {s=$0;n=gsub("^.*coordinates:","",s)} n{print $1,$2,s}' file测试:
$ awk 'NR==1{print} {s=$0;n=gsub("^.*coordinates:","",s)} n{print $1,$2,s}' file
XYZ-7, ABC987654321
1) 8 54°38,177’N, 13°33,573’E; 54°38,094’N, 13°33,237’E
2) 5 54°39, 377’ N, 13°33,940’E, 54°39,242’N, 13°33,632’E
23) 2 53°40,207’N, 14°32,522’E; 53°40,207’N, 14°32,557’Ehttps://stackoverflow.com/questions/44455902
复制相似问题