Pattern search and modify the values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern search and modify the values
# 1  
Old 11-02-2015
Pattern search and modify the values

I have one file and the file may contain 500 to 15,000 records. I need to search pattern ^F509= and then increment the corresponding value by one and print the entire line.
Please note that Its not a fixed length file. Can anyone please help?



ex:
Code:
^F509=204656
     ^F509=204656
     ^F509=204656

The value should be
Code:
^F509=204656
                              ^F509=204657
                              ^F509=204658

Please refer the attached input and output file.

Last edited by Scrutinizer; 11-02-2015 at 02:27 PM.. Reason: code tags not icode
# 2  
Old 11-02-2015
What if that field's numbers are NOT equal?
Code:
^F509=204656
^F509=209996
^F509=146569

# 3  
Old 11-02-2015
Try something like:
Code:
awk '$2~/^F509=/{split($2,F,/=/); if(n=="")n=F[2]; $2=F[1]"="n++}1' FS=^ OFS=^ file


Last edited by Scrutinizer; 11-02-2015 at 02:50 PM..
# 4  
Old 11-02-2015
Quote:
Originally Posted by RudiC
What if that field's numbers are NOT equal?
Code:
^F509=204656
^F509=209996
^F509=146569

The field numbers are always equal.

---------- Post updated at 03:12 PM ---------- Previous update was at 03:10 PM ----------

Quote:
Originally Posted by Scrutinizer
Try something like:
Code:
awk '$2~/^F509=/{split($2,F,/=/); if(n=="")n=F[2]; $2=F[1]"="n++}1' FS=^ OFS=^ file

I'm not sure whether this logic will work because ^F509 can come anywhere in the file (not $2) .
# 5  
Old 11-02-2015
Try
Code:
awk 'match ($0, /\^F509[^^]*/) && !L {L=1; m=n=substr ($0, RSTART+6, RLENGTH-6)} {sub (m, n++)}1' /tmp/inputfile.txt
DATAk(jdkfjk)09335"mk"8789eetet8766^F509=204657^F321=khfjfd^F870=76547854
DATAk(jdkfjk)09335"mk"8789rtrtr87f6^F509=204658^F321=khfdfdf^F870=76547854
DATAk(jdkfjk)09335"mk"878dg98fdd766^F509=204659^F321=khddjfd^F870=76547854

This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-03-2015
Quote:
Originally Posted by vinus
The field numbers are always equal.

---------- Post updated at 03:12 PM ---------- Previous update was at 03:10 PM ----------



I'm not sure whether this logic will work because ^F509 can come anywhere in the file (not $2) .
Ok, adjusted to any field:
Code:
awk '{for(i=1; i<=NF; i++) if($i~/^F509=/){split($i,F,/=/); if(n=="")n=F[2]; $i=F[1]"="n++}}1' FS=^ OFS=^ file

---
Quote:
Originally Posted by RudiC
Try
Code:
awk 'match ($0, /\^F509[^^]*/) && !L {L=1; m=n=substr ($0, RSTART+6, RLENGTH-6)} {sub (m, n++)}1' /tmp/inputfile.txt
[ .. ]

This will fail if there is a line that happens to have the same sequence of numbers before the F509 field. Then sub (m, n++) will substitute those numbers instead of those in the F509 field...

Last edited by Scrutinizer; 11-03-2015 at 12:19 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-03-2015
Hello Forum!

Back after a long absence. Awk is fun and I needed to flex my brain cells. Here is mine:

Code:
awk '$2~/^F509/{i++; split($2,a,"="); $2=a[1]"="a[2]+i}1' FS=^ OFS=^ file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Modify existing values in a file

Hi, I am trying to write a shell script which will modify existing values and take backup of old values. file trying to modify is : /etc/sysctl.conf script name: modify.sh execute ./modify.sh enter ref no: 123 add below values in file sysctl.cnf kernel.shmall = 4194304 ... (7 Replies)
Discussion started by: darling
7 Replies

3. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

4. Shell Programming and Scripting

modify a file by inserting a conditional values

Hi, input file CCCC 1204 215764.85 9405410.40 1189 DDDD 4498 1503 4617 1507 4723 1517 4829 1528 4996 1540 DDDD 5199 1556 5278 1567 5529 1603 5674 1614 6076 1915 DDDD 6605 2371 7004 2779 CCCC ... (4 Replies)
Discussion started by: Indra2011
4 Replies

5. Shell Programming and Scripting

How to use sed to modify a line above or below matching pattern?

I couldn't figure out how to use sed or any other shell to do the following. Can anyone help? Thanks. If seeing a string (e.g., TODAY) in the line, replace a string in the line above (e.g, replace "Raining" with "Sunny") and replace a string in the line below (e.g., replace "Reading" with... (7 Replies)
Discussion started by: sprinner
7 Replies

6. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

7. Shell Programming and Scripting

Help with search and delete/add/modify script

Gurus, I need to run a script on multiple XML files in different directories and do the following (the output can be redirected to create a new file) 1. Search a pattern like "abc.mno.xyz" in an XML file, once detected, the script should delete one line above and 3 lines below (including the... (2 Replies)
Discussion started by: inditopgun
2 Replies

8. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies
Login or Register to Ask a Question