LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2014, 05:51 AM   #1
Leeofthedark
LQ Newbie
 
Registered: Apr 2014
Posts: 2

Rep: Reputation: Disabled
Using sed within a script to change the input of a seperate text file


Hello fellow ladies and gentlemen, I haven't been doing this long, and here is my trouble:

First, I have a text file called songinfo, inside is listed song information in this format (one song per line):

song_name:album_name:artist_name:song_length:year

Next, I have a script that is eventually supposed to allow the user to modify any field in a song entry. This is after having isolated the song's line in the file. The next part could so far be summarized like:
_____________________________________________________

echo "Choose which attribute to edit: "
echo "A -- song name"
echo "B -- album name"
echo "C -- artist name"
echo "D -- year"
echo
echo "Edit option: "
read change

case $change in
a|A)
oldsong=`cut -f 1 -d :`
echo -n "Enter new song name: "
read newsong
sed w s/$oldsong/$newsong/ songinfo
;;
b|B)
oldalbum=`cut -f 2 -d :`
echo -n "Enter new album name: "
read newalbum
sed w s/$oldalbum/$newalbum/ songinfo
;;
...and so on
___________________________________________________

My question is how do I properly user sed to replace the field in the songinfo from the old value to the new value? I don't know if I'm misusing sed, or the variables even.

As of now, the output is that there is no visible change after execution, and if I check the songinfo file, the text will now have changed.

Please and thanks in advance
 
Old 04-10-2014, 07:04 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"man sed" - check the "-i" to allow edit in-place (i.e. in the file).
Not that I'd normally recommend that - backups might be an idea...
 
Old 04-10-2014, 07:49 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Leeofthedark View Post
First, I have a text file called songinfo, inside is listed song information in this format (one song per line) ...
This amounts to an inventory application.

You say "I have a text file." Do you mean the file already exists, or you contemplate creating it? The layout of the data will be a factor in the complexity of your program.

Suggestion: consider the merits of having not one file, but four:
File A for song name
File B for album name
File C for artist name
File D for year

Data for each song would occupy the same line in each of the four files. This multi-file approach could be advantageous if you need to extend the content of your inventory. Suppose you wanted to add fields such as "genre" and "copyright owner." That could be easy: just create a new file, equal in length to the four existing files, with something like "-no data-" in each line.

Daniel B. Martin
 
Old 04-10-2014, 08:03 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Firstly, please use [code][/code] tags when displaying code or data to make it easier to read.

As others have already answered the sed question, I thought I might make some suggestions:

1. Have a look at the select function for bash as it already performs your echo / read / case portion of your script.

2. Your questions appear to be singular, "Enter new song name: ", yet your cut does not provide the ability to choose a particular line or song. So how do you know you will change the correct one?

Hope some of that helps or gives you some ideas
 
Old 04-10-2014, 09:35 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what if you have a line like:
Code:
band:name:band:name:band:name:3:55:2015
and you only want to change "B -- album name"

Last edited by schneidz; 04-10-2014 at 07:10 PM.
 
Old 04-10-2014, 07:02 PM   #6
Leeofthedark
LQ Newbie
 
Registered: Apr 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks everyone, I have taken some of the aforementioned advice and it's working out better
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: feeding input to a script from a text file faizlo Programming 32 12-02-2010 09:45 AM
sed to change character in text file only once. pobrika Programming 7 09-09-2009 07:43 AM
[SOLVED] Running a sed script on a text file.. ?? panda12 Linux - General 2 07-29-2009 10:13 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration