Double search and replace?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Double search and replace?
# 1  
Old 03-06-2009
Double search and replace?

I need to search for a line containing only 'XYY' or '//'. Then if the next line is either 'COD' or 'FAL' I need to replace positions 3-5 of the line 2 lines after that depending on its value.
So my incoming file is like this:
ABC
XYZ
COD
AS/12/1436/02MAR09
K 99X C4347 N6450SDQ
AAAAAAAAAAAAAAA
BBBBBBBBB10
F 99X SSSSSS R09001

//
FAL
FF/12/1436/02MAR09
K 99K C4347 N6450SDQ
AAAAAAAAAAAAAAA
BBBBBBBBB10
F 99K SSSSSS R09001


I want to replace the 99X on 5th line with 98X, but not the one on line 8. Then replace the 99K on line 13 with 98A. If the value after XYZ or // is not COD or FAL, I don't change anything in that set of lines.

Give me some clues on where to start. There can me up to 99 sets of these lines in a file, but not all need changes.
# 2  
Old 03-06-2009
something along these lines.

assuming myFile:
Code:
ABC
XYZ
COD
AS/12/1436/02MAR09
K 99X C4347 N6450SDQ
AAAAAAAAAAAAAAA
BBBBBBBBB10
F 99X SSSSSS R09001

//
FAL
FF/12/1436/02MAR09
K 99K C4347 N6450SDQ
AAAAAAAAAAAAAAA
BBBBBBBBB10
F 99K SSSSSS R09001

nawk -f prism.awk myFile

prism.awk:
Code:
BEGIN {
  FS=RS=""
  OFS="\n"
  PAT1search="(XYZ)|(//)"
  PAT2search="(COD)|(FAL)"
}
{
  for(i=1;i<=NF;i++)
    if( $i ~ PAT1search && ($(i+1) ~ PAT2search) ) {
       n=split($(i+3), a, " ")
       a[2]=($(i+1) == "COD") ? "98X" : "98A"
       for(j=1; j<=n; j++)
          $(i+3) = (j==1) ? a[j] : $(i+3) " " a[j]
       break
  }
  print $0 "\n"
}


Last edited by vgersh99; 03-06-2009 at 05:36 PM.. Reason: minor fixes
# 3  
Old 03-06-2009
I tried your code, but it was processing one character at a time, so I tried to set the field sep to a new line FS="\n" and was able to get a line at a time, but it is not keeping the lines and it never matches any value to i+1.

I'll keep playing with it, but I am not familiar with awk.
# 4  
Old 03-06-2009
Quote:
Originally Posted by prismtx
I tried your code, but it was processing one character at a time, so I tried to set the field sep to a new line FS="\n" and was able to get a line at a time, but it is not keeping the lines and it never matches any value to i+1.

I'll keep playing with it, but I am not familiar with awk.
Please re-read my assumptions at the beginning of the post.
Given your sample file (quoted in my response) I get:

nawk -f prism.awk prism.txt
Code:
ABC
XYZ
COD
AS/12/1436/02MAR09
K 98X C4347 N6450SDQ
AAAAAAAAAAAAAAA
BBBBBBBBB10
F 99X SSSSSS R09001

//
FAL
FF/12/1436/02MAR09
K 98A C4347 N6450SDQ
AAAAAAAAAAAAAAA
BBBBBBBBB10
F 99K SSSSSS R09001

# 5  
Old 03-06-2009
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

# 6  
Old 03-06-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

not all awk's support more than 1 character for RS - Solaris' 'nawk' does not.
Does not return the right 'stuff' under Solaris' /usr/xpg4/bin/awk either.
# 7  
Old 03-07-2009
Quote:
Originally Posted by vgersh99
not all awk's support more than 1 character for RS - Solaris' 'nawk' does not.
Does not return the right 'stuff' under Solaris' /usr/xpg4/bin/awk either.
Quite right. Only tested on a linux box with:

gawk 3.1.6
gawk in traditional mode (in which gawk should behave identically to UNIX awk)
mawk (from man: new awk Posix 1003.2 meaning it implements the AWK language as defined in Aho, Kernighan and Weinberger, AWK book)

Does indeed not work on the real original awk. I should have specified on which version I did my tests.
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