Grab data between 2 keywords any do an array operation and write the file intact


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab data between 2 keywords any do an array operation and write the file intact
# 1  
Old 05-25-2012
Grab data between 2 keywords any do an array operation and write the file intact

Hi Unix Gurus,

I need to grep for a block that is between a start and end keyword and then in between I need to find and replace a keyword.

for eg: I need to search between
Code:
Test   = 000;

and
Code:
Test   = 000;

and find
Code:
K9

and replace with
Code:
M9

INPUT FILE

Code:
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}

Require OUTPUT FILE
Code:
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = M9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = M9;
Test      = 000;
Regular  = 00;
}

Appreciate your help
And thanks in advance
# 2  
Old 05-25-2012
Hi

Code:
 sed  '/Test      = 000;/,/Test      = 000;/s/K9/M9/' file

Guru
# 3  
Old 05-25-2012
Hi Guruprasad,

What if I have 100's of such back to back blocks. I guess it will only find the first and last occurrence of test.

Can you suggest something that searches for every pair of Test statements and replaces K9 with M9?

Thanks
# 4  
Old 05-25-2012
Try:
Code:
awk '/Define/{print x}1' infile | awk '{if($8=="000;"){if(p)sub($5,"M9;"); p=1}else p=0}1' RS= ORS='\n\n'

# 5  
Old 05-25-2012
Could you explain what $8 and $5 do? The cmd is not working for me
# 6  
Old 05-25-2012
What OS and version are you using? On Solaris use /usr/xpg4/bin/awk rather than awk

Last edited by Scrutinizer; 05-26-2012 at 05:29 AM..
# 7  
Old 05-26-2012
Quote:
Originally Posted by naveen@
Hi Unix Gurus,

I need to grep for a block that is between a start and end keyword and then in between I need to find and replace a keyword.

for eg: I need to search between
Code:
Test   = 000;

and
Code:
Test   = 000;

and find
Code:
K9

and replace with
Code:
M9

INPUT FILE

Code:
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}

Require OUTPUT FILE
Code:
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = M9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = M9;
Test      = 000;
Regular  = 00;
}

Appreciate your help
And thanks in advance
Code:
# awk 'NR==1{if($0!~/Test/)print;else x=$0;}NR!=1{x=x $0;while(getline){if($0~/Replace/)sub($0,$0 i++);x=x?x"\n"$0:$0;;if(x~/Test.*Test.*$/)break};
if(x~/Test      = 000[0-9]*.*Test      = 000[0-9]*;/){sub("K9;"i-1,"M9;",x)};gsub("K9;[0-9]*","K9;",x);;sub(xx,"",x);print x;
x=gensub(".*(Test.*;)","\\1",x);;xx=x}' file
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = M9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 00;
}
Define {
Replace  = K9;
Test      = 000;
Regular  = 00;
}
Define {
Replace  = M9;
Test      = 000;
Regular  = 00;
}

lets try another same file..
Code:
# cat filesame
Define {
Replace  = K9;
Test      = 0001;
Regular  = 001;
}
Define {
Replace  = K9;
Test      = 0002;
Regular  = 002;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 003;
}
Define {
Replace  = K9;
Test      = 0003;
Regular  = 004;
}
Define {
Replace  = K9;
Test      = 0004;
Regular  = 005;
}
Define {
Replace  = K9;
Test      = 0005;
Regular  = 006;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 007;
}

Code:
# awk ...... [same code ] samefile
Define {
Replace  = K9;
Test      = 0001;
Regular  = 001;
}
Define {
Replace  = M9;
Test      = 0002;
Regular  = 002;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 003;
}
Define {
Replace  = K9;
Test      = 0003;
Regular  = 004;
}
Define {
Replace  = M9;
Test      = 0004;
Regular  = 005;
}
Define {
Replace  = M9;
Test      = 0005;
Regular  = 006;
}
Define {
Replace  = K9;
Test      = 111;
Regular  = 007;
}

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

awk --> math-operation in data-record and joining with second file data

Hi! I have a pretty complex job - at least for me! i have two csv-files with meassurement-data: fileA ...... (2 Replies)
Discussion started by: IMPe
2 Replies

3. Shell Programming and Scripting

How to grab a block of data in a file with repeating pattern?

I need to send email to receipient in each block of data in a file which has the sender address under TO and just send that block of data where it ends as COMPANY. I tried to work this out by getting line numbers of the string HELLO but unable to grab the next block of data to send the next... (5 Replies)
Discussion started by: loggedout
5 Replies

4. Shell Programming and Scripting

Grab data within a table in a long log file.

in my file which is a rather long log file it contains many text and tables and there is one table with 15 columns and I am interested to read in the value in column6 and its corresponding value in column2. Trouble is I do not know how to script it as the line number various between different log... (8 Replies)
Discussion started by: piynik
8 Replies

5. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

6. Shell Programming and Scripting

Grab 2 pieces of data within a file

I am a newbie and what I have is a captured file of content. I want to be able to grab 2 pieces of data, multiple times and print them to the screen. DataFile owner: locke user: fun data size: 60 location: Anaheim owner: david user: work data size: 80 location: Orange my script... (2 Replies)
Discussion started by: greglocke
2 Replies

7. Shell Programming and Scripting

Transferring file write operation to another host

I have a process running on two hosts in Active/Standby mode. Process running on active machine is handling the traffic while process running on Standby machine is sitting idle. We have a debugging mechanism in which logs are generated on local machine. When logging is enabled then process running... (1 Reply)
Discussion started by: akhilonnet
1 Replies

8. Linux

File read/ write operation

Hi, I am creating a progress bar for file upload for which I have CGI script which copies the data and depending on certain bytes it increments the progress bar. Here, I am writing the incremented value to a file which is read by Ajax at server end. However, here I want to ask that, is it... (18 Replies)
Discussion started by: xs2punit
18 Replies

9. Shell Programming and Scripting

Grab terms leaving spacings intact

Hi All, I have an input below. I would want to print the 2nd to 5th term leaving the same number of spaces in between each term to be intact. Here in the example, there are 4 spacings between term " ABC " and " 111 ", and i tried the below awk codes but they don;t work. Can anybody give me... (3 Replies)
Discussion started by: Raynon
3 Replies

10. UNIX for Dummies Questions & Answers

search and grab data from a huge file

folks, In my working directory, there a multiple large files which only contain one line in the file. The line is too long to use "grep", so any help? For example, if I want to find if these files contain a string like "93849", what command I should use? Also, there is oder_id number... (1 Reply)
Discussion started by: ting123
1 Replies
Login or Register to Ask a Question