简体   繁体   English

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

[英]Bash Script- Bash Script - Editing Lines on Text From File

I'm using a bash script to read in data from a text file. 我正在使用bash脚本从文本文件中读取数据。

Data: 数据:

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]

code: 码:

#!/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"

I would like to remove the white space between the two songs, then remove the time at the beginning of the song and the hyperlink/studio name from the end of the file. 我想删除两首歌曲之间的空白,然后删除歌曲开头的时间和文件末尾的超链接/工作室名称。 So my output would be: 所以我的输出是:

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:.*//'

output 产量

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

Not quite what your example output shows, but matches your written requirement remove the time at the beginning of the song and the hyperlink/studio name from the end ... 并非您的示例输出所显示的完全正确,而是符合您的书面要求, 删除了歌曲开头的时间,并删除了结尾处的超链接/工作室名称...

Rather than read each line in a while loop, use sed s built in ability to read each line of a file and process it. 而不是在while循环中读取每一行,而是使用sed内置的功能来读取文件的每一行并对其进行处理。 you can do 你可以做

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

OR if you're using a modern linux environment (and others), use the -i option to overwrite the existing file : 或者,如果您使用的是现代linux环境(和其他环境),请使用-i选项覆盖现有文件:

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

IHTH IHTH

#!/bin/bash

file="/home/nexusfactor/Desktop/inputData(linux).txt"
while read -r date line
do
  [[ $date == "" ]] && continue    # empty line -> next loop
  [[ $date =~ ^\[ ]] && continue   # line starts with "[" -> next loop
  line="${line%(*}"                # remove "(" and everything to the right of it
  line="${line%http*}"             # remove "http" and everything to the right of it
  echo "$line"
done <"$file"

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM