Double search and replace?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Double search and replace?
# 8  
Old 03-09-2009
Quote:
Originally Posted by ripat
This version seems to return the required output file. The sub() function will only replace the first occurrence of the string to be replaced.

Code:
 awk 'BEGIN {RS=ORS="\n\n"} /(XYZ|\/\/)\n(FAL|COD)/ {sub(/99X/, "98X", $0); sub(/99K/, "98A", $0)}1' file

I am running on a linux box and this worked for the given example, however I need to change the value only if it is 2 lines after the value "COD" or "FAL". This is changing it even if the first occurrence is on a different line.

Plus we have a mix of Solaris and HP-linux servers, so I need to rethink how I do this since I need something that will work on either box.
# 9  
Old 03-09-2009
OK, give this a try ...

Code:
awk '/XYZ|\/\// { n=NR } NR==n+1 && /COD|FAL/ { n=NR } NR==n+2 && n { sub("99X","98X"); sub("99K","98A") }1' file

On Solaris use nawk or /usr/xpg4/bin/awk.
# 10  
Old 03-09-2009
That's working perfectly. This will get me going so I can finish the script! Thanks for the help. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

2. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

3. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

4. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

5. Shell Programming and Scripting

Replace double double quotes using AWK/SED

Hi, I have data as "01/22/97-"aaaaaaaaaaaaaaaaa""aaa""aabbbbbbbbcccccc""zbcd""dddddddddeeeeeeeeefffffff" I want to remove only the Consequitive double quotes and not the one which occurs single. My O/P must be ... (2 Replies)
Discussion started by: Bhuvaneswari
2 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

Search and replace

Hi, I have a variable which names the file path, like: ./home/folder1/a/b/c/file.txt the names of the folders, and the number of upper level folders will be different, I want it just to return the name of the first upper directory with "_" then the folder name, with something added... (5 Replies)
Discussion started by: a27wang
5 Replies

8. Shell Programming and Scripting

awk - use double NOT in search

Hi folks, Can someone guide me on how to use two "not" in an awk search function? What would be equivalent to grep ABCD file | grep TO | grep -v "#" | grep -v "DR" in awk (my idea is to use TWO && and TWO !) Thanks a bunch in advance. (7 Replies)
Discussion started by: gowri_g_s
7 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question