How can I search and change an specific string in a file | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How can I search and change an specific string in a file

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-20-2012
axel axel is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 20 September 2012, 10:53 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How can I search and change an specific string in a file

Dear All,

New to Linux/Unix OS, my Linux version is 2010 x86_64 x86_64 x86_64 GNU/Linux


As titled, I wonder if you can help to provide a solution to find and change an specific string in a file


The file include a lots of data in following configuration but might be various in different case.



Code:
c 1 0 0 0
c 2 0 0.99999997 0
c 3 0.99999997 0 0
c 4 0.99999997 0.99999997 0
c 5 0 -0.19999999 0
c 6 0.99999997 -0.19999999 0
e 1 2 1 0
e 2 4 2 0
e 3 3 4 0
e 4 5 6 2
e 5 1 5 0
e 6 6 3 0
e 7 3 1 0
r 1 silicon
r 2 oxide
t 1 1 1 3 2 2 -1024 4
t 2 1 3 4 2 -1024 1 -1024
t 3 2 1 5 6 -1022 4 -1024
t 4 2 1 6 3 -1024 1 3

What I want is to find the line which meets following criteria
1. Start with letter 't'
2. Having both string 3 and 4 in the middle three positions
3. Having string -1024 in the rear three positions

Only one line should meet said criteria, e.g.


Code:
t 2 1 3 4 2 -1024 1 -1024

(criteria are highlighted in red font)

I want to find it and change one of the '-1024' into '-1022' e.g.


Code:
t 2 1 3 4 2 -1024 1 -1022

(change are highlighted in blue font)

Thank you and I look forward to your elegant advice.
Moderator's Comments:
Please view this code tag video for how to use code tags when posting code and data.

Last edited by Corona688; 09-20-2012 at 11:12 AM..
Sponsored Links
    #2  
Old 09-20-2012
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 9 April 2014, 1:31 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 Posts
try..


Code:
 
awk '{if($0~/^t/&&$4=="3"&&$5=="4"&&$NF=="-1024"){$NF="-1022";{print $0}}else{print $0}}' filename

Sponsored Links
    #3  
Old 09-20-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 5:10 AM EDT
Location: San Jose, CA, USA
Posts: 4,898
Thanks: 182
Thanked 1,646 Times in 1,397 Posts
Do the lines to be changed always have 9 fields, or do we have to handle any line with an odd number (greater than or equal to 5) fields?
    #4  
Old 09-20-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 September 2014, 12:09 PM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts

Code:
awk -f a.awk infile

where a.awk:

Code:
 /^t/ {
   mf=(NF/2) + .5;
   sub("[.].*", "", mf);
   if ((($(mf-1) == 3 || $(mf) == 3 || $(mf+1) == 3) && ($(mf-1) == 4 || $(mf) == 4 || $(mf+1) == 4)) &&
     ($(NF-2) ~ /^[-]1024/ || $(NF-1) ~ /^[-]1024/ || $(NF) ~ /^[-]1024/)) {
      for (i=NF; i > 0; i-- ) {
         if ($(i) ~ /^[-]1024/ ) {
            sub("-1024", "-1022", $(i));
            break;
         }
      }
      print $0;
   }
}

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search-word-print-specific-string Jassz Shell Programming and Scripting 1 01-28-2011 03:19 AM
search a word and print specific string using awk dragon.1431 Shell Programming and Scripting 7 09-03-2010 07:21 AM
Search a specific string zooby Shell Programming and Scripting 10 11-22-2009 04:14 AM
Search for string between specific lines of code in vi coolavi UNIX for Dummies Questions & Answers 8 07-19-2009 11:17 PM
Search all files for specific string sureshy UNIX for Dummies Questions & Answers 4 03-06-2002 11:28 AM



All times are GMT -4. The time now is 11:28 AM.