如何在gawk中指定文件前缀 [英] How to specify a file prefix in gawk

查看:44
本文介绍了如何在gawk中指定文件前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从从软盘映像中提取的文件名列表中识别文件扩展名.该问题与本示例不同,其中文件已从磁盘映像中提取.我是 gawk 的新手,所以它可能不是正确的工具.

I am trying to identify file extensions from a list of filenames extracted from a floppy disk image. The problem is different from this example where files are already extracted from the disk image. I'm new to gawk so maybe it is not the right tool.

ls Sounddsk2.img -a1 > allfilenames

上面的命令创建如下所示的文件名列表.

The command above creates the list of filenames shown below.

flute.pt
flute.ss
flute.vc
guitar.pt
guitar.ss
guitar.vc

下面的 gawk 命令标识以 .ss

The gawk command below identifies files ending in .ss

cat allfilenames | gawk '/[fluteguitar].ss/' > ssfilenames

当只有几个已知文件名时,这会很好.如何以更通用的形式指定文件前缀?

This would be fine when there are just a few known file names. How do I specify a file prefix in a more generic form?

推荐答案

用你来的正则表达式 /[fluteguitar].ss/,这匹配包含这些字符之一的行 fluegi>、tar(在括号表达式 [...] 中指定,重复字符仅计数一次) 后跟单个未转义的点 . 匹配的任何单个字符(此处的换行符除外),然后在一行的任何位置加倍 ss.

with the regex you come /[fluteguitar].ss/, this matches on lines having one of these characters in it f, l, u, e, g, i, t, a and r (specified within bracket expression [...],duplicated characters count only once) followed by any single character (except newline here) that a single un-escaped dot . matches, then double ss in any place of a line.

需要使用行锚的开始^和结束$,以及使用匹配组来限制匹配.

you need to restrict the matching by using the start ^ and end $ of line anchors, as well as using the group of match.

awk '/^(flute|guitar)\.ss$/' allFilesName> ssFileNames

仅过滤与 flute.ss 和/或 guitar.ss 匹配的两个文件名.组匹配 (...|...) 匹配任何一个由管道分隔的正则表达式,如逻辑 OR.

to filter only two files names matched with flute.ss and/or guitar.ss. The group match (...|...) is matches on any one of regexpes separated by the pipe as saying logical OR.

如果这些只是前缀并且要匹配以这些字符开头并以 .ss 结尾的任何文件,请使用:

if these are just prefixes and to match any files beginning with these characters and end with .ss, use:

awk '/^(flute|guitar).*\.ss$/' allFilesName> ssFileNames

这篇关于如何在gawk中指定文件前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆