linux - Bash 脚本 - Bash 脚本 - 编辑文件中文本的行

标签 linux bash shell

我正在使用 bash 脚本从文本文件中读取数据。

数据:

04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q 
[ARMADA MUSIC]

07:46 Fabio XB & Liuck feat. Christina Novelli - Back To You (Wach Remix)http://goo.gl  /yGxqRX 
[DIGITAL SOCIETY RECORDINGS]

代码:

#!/bin/bash


file="/home/nexusfactor/Desktop/inputData(linux).txt"
while IFS= read -r line
do
       # display $line or do somthing with $line
       echo "$line"
done <"$file"

我想删除两首歌之间的空白,然后删除歌曲开头的时间和文件末尾的超链接/工作室名称。所以我的输出是:

Alex M.O.R.P.H. & Natalie Gioia - My Heaven
Fabio XB & Liuck feat. Christina Novelli

最佳答案

echo " 04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q [ARMADA MUSIC]

07:46 Fabio XB & Liuck feat. Christina Novelli - Back To You (Wach Remix) http://goo.gl/yGxqRX [DIGITAL SOCIETY RECORDINGS]" \
| sed '/^[ \t]*$/d;s/^[0-9][0-9]:[0-9][0-9] //;s/http:.*//'

输出

Alex M.O.R.P.H. & Natalie Gioia - My Heaven
Fabio XB & Liuck feat. Christina Novelli - Back To You (Wach Remix)
# ---------------------------------------^----- ???? 

与您的示例输出显示的不完全相同,但符合您的书面要求 删除歌曲开头的时间和结尾的超链接/工作室名称 ...

与其在 while 循环中读取每一行,不如使用 sed 的内置功能来读取文件的每一行并进行处理。你可以做到

sed '/^[ \t]*$/d;s/^[0-9][0-9]:[0-9][0-9] //;s/http:.*//' file > newFile && /bin/mv newFile file

或者如果您使用的是现代 linux 环境(和其他),请使用 -i 选项覆盖现有文件:

sed -i '/^[ \t]*$/d;s/^[0-9][0-9]:[0-9][0-9] //;s/http:.*//' file

健康教育

关于linux - Bash 脚本 - Bash 脚本 - 编辑文件中文本的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27969303/

相关文章:

c - 如何修复 mupdf 中的 "cannot find ExtGState dictionary"?

bash - 此处文档给出 'unexpected end of file' 错误

linux - shell如何使用行尾的值重复文本中的行

linux - 在 grep 搜索后添加 echo

ruby - 单列 rpm 命令输出

Linux 组权限

linux - 尝试将管道命令放入 if 语句

linux - bash 脚本 - 根据用户输入生成建议

linux - shell程序无法运行

linux - 文件的最后两行相同表示触发电子邮件