Replacing specific entry using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing specific entry using sed
# 8  
Old 01-12-2013
From pottery to green stuff - interesting. However, including solutions to others of your threads, try
Code:
$ F=apple
$ B=greenwood
$ OP=0.70
$ NP=1.20
$ sed "/$F.*$B/ s/$OP/$NP/" file
apple:greenwood:1.20:50
pear:sunshine:0.95:60
dragonfruit:moonshine:1.50:50

sed is extremely powerful, so taking your time sitting back and learning it is invaluable.
# 9  
Old 01-12-2013
If you want to replace specific field values then I recommend using awk instead of sed:
Code:
awk -F: -v oldfruit="apple" -v newfruit="orange" '$1==oldfruit{$1=newfruit}1' Fruits.txt

In above program I am using colon : as field separator and changing the value of specific field, 1st field $1
# 10  
Old 01-12-2013
Be careful - this will replace ALL apples with oranges, not only greenwood's!
# 11  
Old 01-12-2013
Oh, here is the modified program:
Code:
awk -F: -v oldfruit="apple" -v newfruit="orange" -v brand="greenwood" '$1==oldfruit&&$2==brand{$1=newfruit}1' OFS=: Fruits.txt

RudiC, thanks for pointing that out.
# 12  
Old 01-19-2013
Linux sed fid and replace example

Hello Guys ,

I am new to shell scripting.

Below is what i have for you guys

file name employee.txt where I have a ^M at the end of the file , i can remove ^M usind sed

Solution 1Example 1 :
Code:
sed 's/ ^M/ /gi' employee.txt

Solution 2 Example 2 : :
Code:
1,$s/ ^M/ /g

But here is a catch on 1 thing ,where we need to apply the same concept to a entire folder where we have more than 50 file having the same problem of ^M to be removed , how can we do that ?

Problem :
Code:
ls -lrt /root/sunny1/
                              
                               
-rw-r--r-- 1 root root  3325 Dec  7  2011 install.log.syslog
-rw-r--r-- 1 root root 26725 Dec  7  2011 install.log
-rw------- 1 root root   949 Dec  7  2011 anaconda-ks.cfg
-rw-r--r-- 1 root root  1071 Oct 30 20:13 emplst.sh
drwxr-xr-x 2 root root  4096 Dec 10 20:51 Desktop
-rw-r--r-- 1 root root  1669 Jan 10 17:53 natasha_access.sh
-rw-r--r-- 1 root root   108 Jan 10 17:54 natasha_others
-rw-r--r-- 1 root root   109 Jan 10 17:56 last_sasha.sh
-rw-r--r-- 1 root root   140 Jan 10 18:02 emplsts_1_4.sh
drwxr-xr-x 2 root root  4096 Jan 10 18:33 redhat
-rw-r--r-- 1 root root   812 Jan 10 18:37 emplsts.sh
-rw-r--r-- 1 root root    59 Jan 14 18:52 emp1.sh
-rw-r--r-- 1 root root   549 Jan 19 12:55 employee.txt
-rw-r--r-- 1 root root   549 Jan 19 13:14 n_employee.txt
-rw-r--r-- 1 root root   507 Jan 19 13:20 f_employee.txt
[root@localhost ~]#


Last edited by Scrutinizer; 01-19-2013 at 05:18 AM.. Reason: code tags
# 13  
Old 01-19-2013
@sunnyd1: Please start a new thread with your question..
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