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


Trivia Category: Science: Mathematics
Difficulty: Easy
An isosceles triangle has two sides of equal length as opposed to three.
True or False?

 
Thread Tools Search this Thread
# 1  
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:
Mod Comment 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 12:12 PM..
# 2  
try..

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

# 3  
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  
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;
   }
}

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

awk to change specific string to new value if found in text file

I am trying to use awk to change a specific string in a field, if it is found, to another value. In the tab-delimited file the text in bold in $3 contains the string 23, which is always right before a ., if it is present. I am trying to change that string to X, keeping the formatting and the... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Search a string in a file and change another in the line

I did it myself (0 Replies)
Discussion started by: apenkov
0 Replies

4. Shell Programming and Scripting

Change some string in specific column with space

Hello All of Master Script , i need help to solve my problem before : mount /dev/rdsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /vol/cl123/PURGE1 ufs mount /dev/rdsk/c1t70d0s6 /dev/rdsk/c1t70d0s6 /vol/cl123/PURGE2 ufs expected : mount /dev/dsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /PURGE1 ufs mount ... (3 Replies)
Discussion started by: k0p0nkkk
3 Replies

5. Shell Programming and Scripting

Using sed to change values after a specific string

Hello I have a script that searches a file for a specific string and then changes the nth column after that string. I have searched online for how to do this with sed but have not seemed to find a solution that works for me. I am using bash. Some background info: - Currently I am using awk to... (4 Replies)
Discussion started by: prodigious8
4 Replies

6. UNIX for Dummies Questions & Answers

Search specific string logfile specific date range

Hi, I have logfile like this.. === 2014-02-09 15:46:59,936 INFO RequestContext - URL: '/eyisp/sc/skins/EY/images/pickers/comboBoxPicker_Over.png', User-Agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko': Unsupported with Accept-Encoding header === 2015-02-09... (8 Replies)
Discussion started by: kishk
8 Replies

7. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

8. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

9. Shell Programming and Scripting

Search a specific string

Hi, I have following requirement. Pls suggest. To search a string in a file which is combination of character and number(always 9 digit, but numeric). if found then caputure the exit return code as 0 else 1 , if 0 then next job can be triggerd. If exit code is 1, should return a failure... (10 Replies)
Discussion started by: zooby
10 Replies

10. UNIX for Dummies Questions & Answers

Search all files for specific string

Hi Friends, How can I search all files in all slices on a unix system for a particular string within the file. e.g search string 'oracle' Thanks (4 Replies)
Discussion started by: sureshy
4 Replies

Featured Tech Videos