Sed Help in Updating something only in one particular file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed Help in Updating something only in one particular file.
# 1  
Old 03-24-2008
Error Sed Help in Updating something only in one particular line.

Hi,

The scenerio is that I want to replace a text in one particular line of a file. But when I am using the sed it's replacing all the occurences of that text.

Like the file is:

>cat test
DNGGF10 :None :Test
DNGGF11 :ABC :Test1
DNGGF12 :None :Test2
DNGGF13 :None : Test3


I have written the following code for this:-
>cat script
name=$1

sed "s/`grep DNGGF10 test|cut -d ':' -f2`/$1/" test >test.chk



But while executing the code as ./script yogi then I am getting O/p in test.chk as:

>cat test.chk
DNGGF10 :Yogi:Test
DNGGF11 :ABC :Test1
DNGGF12 :yogi:Test2
DNGGF13 :Yogi: Test3


But i want to replce second field only for that line which contains DNGGF1O i.e the 1st line.Also space total sapce in second field should also not get altered.

Kindly shed some light on it.

Thanks-
Yogi

Last edited by bisla.yogender; 03-24-2008 at 10:27 AM..
# 2  
Old 03-24-2008
Is this what you're looking for?

Code:
sed "/DNGGF10/s/None/$1/" test > test.chk

Regards
# 3  
Old 03-24-2008
Thanks, I got my hint from here. I changed my code to:

sed "/DNGGF10/s/`grep DNGGF10 test|cut -d ':' -f2`/$1/" test >test.chk

Now I am able to replace text only that paritucular line but the space after None are still getting altered which should not happen.

Line before replacing text:

DNGGF10 :None :Test (4 Spaces after None)

After running the script as ./script abc

DNGGF10 :abc:Test

While there should be five spaces after abc.

Kindly suggest.

Regards,
Yogi
# 4  
Old 03-24-2008
If you want fix columns you can use awk:

Code:
awk -v var=$1 '
BEGIN{FS=OFS=":"}
/DNGGF10/{$2=sprintf("%-8s", var)}1
' test > test.chk

Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 5  
Old 03-24-2008
Code:
sed '/DNGGF10/s/\(.*\) \(.*\) \(.*\)/\1 :\1 \3/' test >test.chk

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Updating in file

hi, i have an csv(which is a month's log file containing userid and log in date ) file which has to be appended to another file say master.log.I need to compare the next month's log data to master.log file .In case, there is new log date for userid it has to get updated in master.log file or i... (2 Replies)
Discussion started by: preema
2 Replies

2. Shell Programming and Scripting

Updating variables using sed or awk

Hi, I have a file(testfile.txt) that contains list of variables as shown below. T $$FirstName=James $$LastName=Fox $$Dateofbirth=1980-02-04 ……and so on there are 50 different variables. I am writing a script(script1.sh) that will update the above three variable one by one with the values... (6 Replies)
Discussion started by: Saanvi1
6 Replies

3. UNIX for Dummies Questions & Answers

Updating a File

Hi, I have a below requirement and need your help in this regard. I have two timestamps in a file i.e. TimeBefore=2014-07-10.18.06.55 TimeAfter_1=2014-07-09.05.45.12 TimeAfter_2=2014-07-09.05.40.12 When I first run the script file, it sets the 'TimeBefore' to todays date. When I run... (5 Replies)
Discussion started by: vrupatel
5 Replies

4. Shell Programming and Scripting

Help updating a file

I can not seem to figure out how to update the attached match.txt column 2 using update.txt. However, only the text before the period in updat.txt appears in match.txt. For example, in update.txt NM_001613.2 matches NM_001613 in match.txt, so is it possible to update the record in match.txt to... (8 Replies)
Discussion started by: cmccabe
8 Replies

5. Shell Programming and Scripting

updating a file with sed -help

I have a file of records all made up of single charactors that need to be updated using their row/column (the dashes represent spaces as the html formatting of this text box when it posts removes the "nonessential" white space between the charactors) file example A 1 c B 2 b C 3 a... (2 Replies)
Discussion started by: dboward
2 Replies

6. Shell Programming and Scripting

Reading from one file and updating other file with constant entries

Hi All, Thaks for the help in my last thread. I have one more question. I have two files with ldap entries in it. One file contains all the user LDAP parameter entries (26 MB) and other file contains only the user id's that have to be inactivated. Unix script has to read from the file that has... (8 Replies)
Discussion started by: Samingla
8 Replies

7. Shell Programming and Scripting

Updating a line in a large csv file, with sed/awk?

I have an extremely large csv file that I need to search the second field, and upon matches update the last field... I can pull the line with awk.. but apparently you cant use awk to directly update the file? So im curious if I can use sed to do this... The good news is the field I want to... (5 Replies)
Discussion started by: trey85stang
5 Replies

8. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

9. Shell Programming and Scripting

sed used for updating firewall allow ftp from DHCP access

Here is my situation. You can make all kinds of comments about how I am doing it and why from the networking standpoint, but I am really only looking for comments on how to make it work. Of course, any other ideas for how to do this would be welcome. I have an iptables firewall that allows only... (1 Reply)
Discussion started by: manouche
1 Replies

10. Shell Programming and Scripting

TO know whether file is updating or not

Hi All, I am new to scripting. my requirement is , I want to know whether the file(i.e., log file) is updating or not. It should search for every 15 min. If it is not updating means it needs to send out a automatic mail. can you please help me in this. (2 Replies)
Discussion started by: raj333
2 Replies
Login or Register to Ask a Question