The UNIX and Linux Forums  


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
Search and replace sanyerra Shell Programming and Scripting 8 09-16-2008 02:55 AM
search and replace coolkid Shell Programming and Scripting 3 08-12-2008 06:15 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
Search/Replace with Sed mvalonso UNIX for Dummies Questions & Answers 5 05-08-2006 08:30 PM
search and Replace mukeshannamalai UNIX for Advanced & Expert Users 4 09-14-2001 07:21 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 02-12-2009
sdosanjh sdosanjh is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 27
Search and replace using awk

Dear All,

I want to search and replace the text in file using awk. but facing hard luck in that.
Please help me out!!!!

> grep Abc.De.ync.rate /tmp/sdosanjh.txt
Abc.De.ync.rate 6 write


Now, I want to replace the "6" with value say "2".

I tried the follwoing using awk.... but its not working

% chgline=`cat /tmp/sdosanjh.txt | grep Abc.De.ync.rate | awk '{print ($2)}'`

% echo $chgline
6

% awk 'BEGIN{OFS=FS="Abc.De.ync.rate"}$2==$chgline{$2=5} {print}' /tmp/sdosanjh.txt
  #2 (permalink)  
Old 02-12-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,330
Something like this should work:


Code:
awk '/Abc.De.ync.rate/{$2=val}{print}' val=2 /tmp/sdosanjh.txt

Regards
  #3 (permalink)  
Old 02-12-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
nawk '/Abc.De.ync.rate/{if ($2==6) $2=2; print}' /tmp/sdosanjh.txt

  #4 (permalink)  
Old 02-12-2009
sdosanjh sdosanjh is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 27
Gr8 franklin it worked.
But its not preserving the file structure. Its trimming the tab spaces here as well in my reply... imagine there are two tab spaces after each field
if before edit the file was

Abc.De.ync.rate 6 write

Now it is like below
Abc.De.ync.rate 2 write

Any idea how it can be preserved???
  #5 (permalink)  
Old 02-12-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by sdosanjh View Post
Gr8 franklin it worked.
But its not preserving the file structure. Its trimming the tab spaces here as well in my reply... imagine there are two tab spaces after each field
if before edit the file was

Abc.De.ync.rate 6 write

Now it is like below
Abc.De.ync.rate 2 write

Any idea how it can be preserved???
If you ALWAYS have 2 tabs between fields....

Code:
awk '/Abc.De.ync.rate/{$2=val}{print}' val=2 OFS='\t\t' /tmp/sdosanjh.txt

  #6 (permalink)  
Old 02-12-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,330
Quote:
Originally Posted by sdosanjh View Post
Gr8 franklin it worked.
But its not preserving the file structure. Its trimming the tab spaces here as well in my reply... imagine there are two tab spaces after each field
if before edit the file was

Abc.De.ync.rate 6 write

Now it is like below
Abc.De.ync.rate 2 write

Any idea how it can be preserved???
Try this:


Code:
awk '/Abc.De.ync.rate/{$2=val}{print $1 "\t\t" val "\t\t" $3;next}{print}' val=2 /tmp/sdosanjh.txt

Regards
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 11:31 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