Replacing specific entry using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing specific entry using sed
# 1  
Old 01-11-2013
Replacing specific entry using sed

Hi guys, I'm new to bash programming, so please pardon me.

I'm trying to replace an entry's text in Books.txt

This code works perfectly:

Code:
sed -i "s/$BookTitle/$NewBookTitle/g" Books.txt

But problem is, if there are double entries, it will also replace that entry. For example:

Books.txt
Fondue:Grass <-- intended text to replace (Fondue)
Fondue:Hello

After that code, it becomes:
Elephant:Grass
Elephant:Hello <-- not supposed to change this entry

I only want to change Fondue:Grass to Elephant:Grass. Please help!
# 2  
Old 01-11-2013
Please remove 'g' at the end of your sed command.
g means globally. The change will effect to all entries.
If you remove g , it will change first occurence.
This User Gave Thanks to karumudi7 For This Post:
# 3  
Old 01-11-2013
try:
Code:
sed -i '0,/Fondue/s//Elephant/' Books.txt

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 01-11-2013
Thanks karumudi7 and rdrtx1!

---------- Post updated at 08:36 AM ---------- Previous update was at 07:54 AM ----------

Oh, actually, I want to use sed to find the specific line that fulfills the requirement of $BookTitle:$BookAuthor. So if I were to update the value of $BookAuthor, it must update according to the $BookTitle.

E.g.
The Book Author is J.K. Rowling
There are various books by J.K. Rowling:
Philosopher's Stone:J.K. Rowling
Chamber of Secrets:J.K. Rowling
Prisoner of Azkaban:J.K. Rowling

The code below only takes the first entry "Philosopher's Stone:J.K. Rowling" and replaces it with the new author.

Code:
sed -i '0,/$BookAuthor/s//$NewBookAuthor/' Books.txt

So it changes Philosopher's Stone:J.K. Rowling to Philosopher's Stone:Candace

However, I want to be able to update $BookAuthor based on the value of $BookTitle.

For example, I want to be able to change Chamber of Secrets:J.K. Rowling to Chamber of Secrets:Candace.
# 5  
Old 01-11-2013
The example below will allow you to change the "BookAuthor" to the "NewBookAuthor" based on the Book Title:
Code:
$ cat Books.txt
Philosopher's Stone:J.K. Rowling
Chamber of Secrets:J.K. Rowling
Prisoner of Azkaban:J.K. Rowling

$ BookTitle="Chamber of Secrets"

$ NewBookAuthor=Candace

$ sed "s/\($BookTitle:\)\(.*\)$/\1$NewBookAuthor/g" Books.txt
Philosopher's Stone:J.K. Rowling
Chamber of Secrets:Candace
Prisoner of Azkaban:J.K. Rowling

This User Gave Thanks to spacebar For This Post:
# 6  
Old 01-12-2013
Use sed's address capability to find the line and then replace old author with new:
Code:
$ sed "/$BookTitle/s:$BookAuthor:$NewBookAuthor:" Books.txt

@karumudi7: g means all occurrencies in that respective line, NOT in entire file!
This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-12-2013
Okay, I think I should specify my question further.

I need to be able to update the values of a specific value in my .txt file.

For example: [fruit]:[brand]:[price]:[qty]
apple:greenwood:0.70:50
pear:sunshine:0.95:60
dragonfruit:moonshine:1.50:50

If I do a search for apple and greenwood, I should be able to update the fruit, brand, price, and qty of that entry. So lets say I did a search (fruit & brand) on pear & sunshine. I'll be able to update the value of pear, sunshine, price, and quantity.

Code:
echo -n "Fruit : "
	read Fruit
	echo ""
	echo -n "Brand : "
	read Brand
	echo ""
	if grep -q "$Fruit:$Brand" "Fruit.txt"; then
		echo "Fruit found!"
		echo ""
		selection=""
		until [ "$selection" == "e" ]; do
			echo "a) Update Fruit Name"
			echo "b) Update Brand"
			echo "c) Update Price"
			echo "d) Update Qty"
			echo "e) Back to main menu"
			echo ""
			echo -n "Please enter your choice: "
			read selection
			echo ""
			case $selection in
				a)
					echo -n "New Fruit Name : "
					read NewFruit
					echo ""
					sed -i "0,/$Fruit/s//$NewFruit/" Fruit.txt
					echo "Fruit Name has been changed from $Fruit to $NewFruit."
					Fruit="$NewFruit"
					echo ""
					;;

This is what I've done so far, but as per my problem above, I'm unable to update specific values in a specific entry.

Fruit & Brand variables will remain as constants to match with the .txt file.

I'm figuring there will be 4 different codes for the updating of 1) fruit, 2) brand, 3) price and 4) qty. Sed is confusing me with all the pattern flags and command line options.

P.S.: I tried using grep from my .txt source, and pipe it into a do-while loop, but I'm still unable to update the specific values.

Last edited by todaealas; 01-12-2013 at 10:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace multiple "&nbsp;" entry with in <td> tag into single entry using sed?

I have the input file like this. Input file: 12.txt 1) There are one or more than one <tr> tags in same line. 2) Some tr tags may have one <td> or more tna one <td> tags within it. 3) Few <td> tags having "<td> &nbsp; </td>". Few having more than one "&nbsp;" entry in it. <tr> some td... (4 Replies)
Discussion started by: thomasraj87
4 Replies

2. Programming

PYTHON COPY Contents of file1 into a specific entry in file2

file1 cat dog fish file2 This is a bunch of lines <!-- INSERT ANIMALS HERE --> horse cheetah post results file2 This is a bunch of lines <!-- INSERT ANIMALS HERE --> cat dog fish horse (1 Reply)
Discussion started by: TY718
1 Replies

3. UNIX for Dummies Questions & Answers

Check for entry of specific pattern

Hey Guys and Gals, I am having trouble with what I thought shouldn't be hard.. In a script I am working on there is a need to enter a MAC address. MAC addresses are formatted ; XX:XX:XX:XX:XX:XX where X can be 0-9, a-f or A-F So in the sample script the query is something... (4 Replies)
Discussion started by: TAPE
4 Replies

4. Shell Programming and Scripting

Replacing specific characters using sed

Hi, I have a text file which is output from a server and it lists all the files in a specific volume. However, the volume name appears as volume_name:. I would like to replace this with \\volume_name\volume_name. This is not a problem in itself as I can use sed to globally look for the... (8 Replies)
Discussion started by: vnayak
8 Replies

5. Shell Programming and Scripting

how to change specific value for a entry in the file

Hello All, can someone please suggest me a one line command to change a specific value that is associated to an entry in the file. for example #more schedulefile quartz.job.manual.bonus.schedule=0 0 9 ? * * # it should be changed to #more schedulefile... (5 Replies)
Discussion started by: bobby320
5 Replies

6. Shell Programming and Scripting

Notification of the modification of a specific entry in a file

I need to get notification via email when the line containing a pattern is changed in a file. Not during the time any changes to file occurs. Ie if we reset a user password say example, demouser the hash in the line containing the word demouser in the file /etc/group changes. So when this change... (1 Reply)
Discussion started by: anil510
1 Replies

7. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

8. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

9. Shell Programming and Scripting

SED Replacing all but one regex match on a line or specific matches

Hi, I'm attempting to rename some files that have spaces in them. Without linking sed commands together is it possible to replace the first three "." to " ". File.name.is.long.ext -> File name is long.ext I can get the desired effect with echo "File.name.is.long.ext" | sed 's/\./ /g;s/... (5 Replies)
Discussion started by: vectox
5 Replies

10. Shell Programming and Scripting

replacing specific lines in a file

Hi there I have a file which has the lines # Serial number for hostid EXP_SERIAL_="" These lines could be anywhere in the file as far as line numbers go, I would like replace these two lines with # Serial number for hostid $var1 EXP_SERIAL_$var1="$var2" Is there a quick and simple... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question