1

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 https://www.facebook.com/alexmorph 
[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
2
  • 2
    Read up on sed and awk. Those command will be useful here. Commented Jan 15, 2015 at 17:26
  • Crossposting: askubuntu.com/questions/574137/… Commented Jan 15, 2015 at 17:54

2 Answers 2

3
echo " 04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven https://www.facebook.com/alexmorph [ARMADA MUSIC]

07:46 Fabio XB & Liuck feat. Christina Novelli - Back To You (Wach Remix) http://www.beatport.com/release/back-to-you/1245297 [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

Sign up to request clarification or add additional context in comments.

3 Comments

My example output is me just typing what I wanted, but your output is what I wanted anyway :D. Now wait, how would I apply it to my while loop so that as it reads each line, it edits it?
Thanks, Now I'm using Lubuntu 14.10, I'm going to try the last example.
@CharlesWhitfield with the -i option you can also keep a backup of the original file if you want sed -i .bak '/^[..etc..' file. See man sed.
2
#!/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 

5 Comments

Thanks, but I get an error: line 10: syntax error near unexpected token done' line 10: done <"$file'
Thanks, so much for this, however, one more requirement, is it possible to remove the studio name in the [] after the link? Completely remove it, including the braces?
I've added output of my script. The output contains already no [studio] because these brackets are behind a ( or http.
Thanks. The mistake was on my end. If you bare with me, let me correct my input data. The studio was initially on a new line.
I edited my post. The [studio] names were on a line by themselves. How would I remove it so that I get the output you've shown?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.