如何在 bash 中解析文件名?

发布于 2024-07-05 01:24:39 字数 125 浏览 6 评论 0原文

我有一个格式如下的文件名:

系统源-yyyymmdd.dat

我希望能够使用“-”作为分隔符来解析文件名的不同位。

I have a filename in a format like:

system-source-yyyymmdd.dat

I'd like to be able to parse out the different bits of the filename using the "-" as a delimiter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一百个冬季 2024-07-12 01:24:39

最简单(也是 IMO 最好的方法)就是使用 read

$ IFS=-. read system source date ext << EOF
> foo-bar-yyyymmdd.dat
> EOF
$ echo $system
foo
$ echo $source $date $ext
bar yyyymmdd dat

该主题有很多变体,其中许多都依赖于 shell:

bash$ IFS=-。 读取系统源日期 ext <<< foo-bar-yyyymmdd.dat

echo "$name" | { IFS=-. read system source date ext
   echo In all shells, the variables are set here...; }
echo but only in some shells do they retain their value here

The simplest (and IMO best way) to do this is simply to use read:

$ IFS=-. read system source date ext << EOF
> foo-bar-yyyymmdd.dat
> EOF
$ echo $system
foo
$ echo $source $date $ext
bar yyyymmdd dat

There are many variations on that theme, many of which are shell dependent:

bash$ IFS=-. read system source date ext <<< foo-bar-yyyymmdd.dat

echo "$name" | { IFS=-. read system source date ext
   echo In all shells, the variables are set here...; }
echo but only in some shells do they retain their value here
自由如风 2024-07-12 01:24:39

使用剪切命令。

例如

echo "system-source-yyyymmdd.dat" | cut -f1 -d'-'

将提取第一位。

更改 -f 参数的值以获得适当的部分。

以下是有关 Cut 命令的指南。

Use the cut command.

e.g.

echo "system-source-yyyymmdd.dat" | cut -f1 -d'-'

will extract the first bit.

Change the value of the -f parameter to get the appropriate parts.

Here's a guide on the Cut command.

往昔成烟 2024-07-12 01:24:39

另一种方法是使用shell的内部解析工具,这样可以避免创建子进程的成本:

oIFS=$IFS
IFS=-
file="system-source-yyyymmdd.dat"
set $file
IFS=$oIFS
echo "Source is $2"

Another method is to use the shell's internal parsing tools, which avoids the cost of creating child processes:

oIFS=$IFS
IFS=-
file="system-source-yyyymmdd.dat"
set $file
IFS=$oIFS
echo "Source is $2"
烟火散人牵绊 2024-07-12 01:24:39

根据您的需要,awk 比 cut 更灵活。 第一个预告片:

# echo "system-source-yyyymmdd.dat" \
    |awk -F- '{printf "System: %s\nSource: %s\nYear: %s\nMonth: %s\nDay: %s\n",
              $1,$2,substr($3,1,4),substr($3,5,2),substr($3,7,2)}'
System: system
Source: source
Year: yyyy
Month: mm
Day: dd

问题在于,将 awk 描述为“更灵活”肯定就像称 iPhone 为增强型手机一样;-)

Depending on your needs, awk is more flexible than cut. A first teaser:

# echo "system-source-yyyymmdd.dat" \
    |awk -F- '{printf "System: %s\nSource: %s\nYear: %s\nMonth: %s\nDay: %s\n",
              $1,$2,substr($3,1,4),substr($3,5,2),substr($3,7,2)}'
System: system
Source: source
Year: yyyy
Month: mm
Day: dd

Problem is that describing awk as 'more flexible' is certainly like calling the iPhone an enhanced cell phone ;-)

海拔太高太耀眼 2024-07-12 01:24:39

您可以使用 cut 命令 来获取 3 个“字段”中的每一个,例如:

$ echo "system-source-yyyymmdd.dat" | cut -d'-' -f2
source

“-d”指定分隔符,“-f”指定你需要的字段号

You can use the cut command to get at each of the 3 'fields', e.g.:

$ echo "system-source-yyyymmdd.dat" | cut -d'-' -f2
source

"-d" specifies the delimiter, "-f" specifies the number of the field you require

影子是时光的心 2024-07-12 01:24:39

一个漂亮而优雅的(在我看来:-)仅使用内置函数是将其放入数组中

var='system-source-yyyymmdd.dat'
parts=(${var//-/ })

然后,您可以在数组中找到部分...

echo ${parts[0]}  ==> system
echo ${parts[1]}  ==> source
echo ${parts[2]}  ==> yyyymmdd.dat

警告:如果文件名包含“奇怪”字符,这将不起作用比如空格,或者,天堂不允许,引号,反引号......

A nice and elegant (in my mind :-) using only built-ins is to put it into an array

var='system-source-yyyymmdd.dat'
parts=(${var//-/ })

Then, you can find the parts in the array...

echo ${parts[0]}  ==> system
echo ${parts[1]}  ==> source
echo ${parts[2]}  ==> yyyymmdd.dat

Caveat: this will not work if the filename contains "strange" characters such as space, or, heaven forbids, quotes, backquotes...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文