user4350786
user4350786

Reputation:

Bash Script- Bash Script - Editing Lines on Text From File

I'm using a bash script to read in data from a text file.

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

Upvotes: 1

Views: 160

Answers (2)

Cyrus
Cyrus

Reputation: 88979

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

Upvotes: 2

shellter
shellter

Reputation: 37318

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 seds built in ability to read each line of a file and process it. 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 :

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

IHTH

Upvotes: 3

Related Questions