The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
TO know whether file is updating or not raj333 Shell Programming and Scripting 2 11-05-2007 05:29 PM
Updating file in loop braindrain Shell Programming and Scripting 4 06-20-2007 01:37 PM
Why my sar is not updating the output file. skneeli UNIX for Advanced & Expert Users 1 11-12-2006 08:29 PM
Updating a File in a Zip Archive dbridle AIX 6 09-27-2006 03:29 PM
sending and updating a file over network bhakti High Level Programming 1 09-21-2005 02:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-24-2008
bisla.yogender bisla.yogender is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 30
Exclamation 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 09:27 AM..
  #2 (permalink)  
Old 03-24-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
Is this what you're looking for?

Code:
sed "/DNGGF10/s/None/$1/" test > test.chk
Regards
  #3 (permalink)  
Old 03-24-2008
bisla.yogender bisla.yogender is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 30
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 (permalink)  
Old 03-24-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
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 (permalink)  
Old 03-24-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Code:
sed '/DNGGF10/s/\(.*\) \(.*\) \(.*\)/\1 :\1 \3/' test >test.chk
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0