linux - 使用 sed 的 Shell 脚本句柄字符串

标签 linux shell unix sed

我有一个文本文件,每一行是一个或多个用空格分隔的文件路径,所有文件都有后缀dl,例如

/some/path/file.dl
/some/other/path/file2.dl /some/other/path2/file3.dl
/some/other/path3/file4.dl /some/other/path4/file5.dl ...
...

现在我需要将上述文件转换为另一个文本文件。只需要将每行第一个文件改为/out/P{fileName}.h:{fileName}为原文件名,不带目录和后缀。例如

/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path2/file5.dl ...
...

那么如何编写linux shell脚本呢?

最佳答案

试试这个命令:

$ sed -r 's@^\S*/(\S*)\.dl@/out/P\1.h:@' input 
/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path4/file5.dl

关于linux - 使用 sed 的 Shell 脚本句柄字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557268/

相关文章:

linux - 如何使用 BASH 脚本在文本文件上创建动态 header

linux - 查找并更新文件中的电子邮件

java - 我可以在没有 DNS 配置的情况下从其 .jar 文件使用 Rundeck 吗?

c - 在 C 中使用#define 进行字符串连接

linux - Tar 当前目录中的 100 个目录

linux - fgrep 和egrep ?

regex - 如何在 linux shell 中使用正则表达式从文件中提取 IP 地址?

linux - 从 perl 运行时将 bash 脚本的输出记录到日志文件和标准输出中

linux - 与 Xmonad 一起使用时 Xmobar 不可见

linux - 用于从文本文件重命名文件夹的 Shell 或 Perl 脚本