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
SED Search Pattern and Replace with the Pattern racbern Shell Programming and Scripting 4 03-15-2008 05:59 AM
awk: find and replace in certain field only, help needed jkl_jkl Shell Programming and Scripting 5 02-21-2008 10:52 AM
Find a pattern and replace using sed. maridhasan Shell Programming and Scripting 5 09-18-2007 01:43 AM
find and replace a pattern in a file krishnamaraju Shell Programming and Scripting 1 08-29-2006 10:02 AM
find pattern in FILES and replace it ?? tamer UNIX for Dummies Questions & Answers 4 03-03-2001 10:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-10-2007
sergiioo sergiioo is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 1
Unhappy find pattern and replace another field

HI all

I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example:

100099C01101C00000000059394200701CREoperadora_TX
100099C01201C00000000000099786137OPERADORA_TX2

in the example above I need to change the first field from 1 to 2 only if the line has the CRE pattern. so the lines will be:

200099C01101C00000000059394200701CREoperadora_TX
100099C01201C00000000000099786137OPERADORA_TX2

Could you help me, how to do this with perl or with awk

Regads

Marcelo
  #2 (permalink)  
Old 04-10-2007
Raghuram.P Raghuram.P is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 44
Hi,
Check this code
echo $filename | awk '{if(index($0,"CRE") != 0)
{
if($0 ~ /^1/)
{
printf("2%s\n",substr($0,2,length($0)));
}
else
{
print $0;
}
}
else{print $0}}'

Thanks
Raghuram
  #3 (permalink)  
Old 04-10-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed "/^1.*CRE/s/^1/2/" file
  #4 (permalink)  
Old 04-11-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by sergiioo
HI all

I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example:

100099C01101C00000000059394200701CREoperadora_TX
100099C01201C00000000000099786137OPERADORA_TX2

in the example above I need to change the first field from 1 to 2 only if the line has the CRE pattern. so the lines will be:

200099C01101C00000000059394200701CREoperadora_TX
100099C01201C00000000000099786137OPERADORA_TX2

Could you help me, how to do this with perl or with awk

Code:
awk '/^1.*CRE/ { $1 = "2" substr($1,2) }
                   { print }'
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 03:30 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