find pattern and replace another field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find pattern and replace another field
# 1  
Old 04-10-2007
Data find pattern and replace another field

HI all

I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example:

100099C01101C00000000059394200701CREoperadora_TX
100099C01201C00000000000099786137OPERADORA_TX2

in the example above I need to change the first field from 1 to 2 only if the line has the CRE pattern. so the lines will be:

200099C01101C00000000059394200701CREoperadora_TX
100099C01201C00000000000099786137OPERADORA_TX2

Could you help me, how to do this with perl or with awk

Regads

Marcelo
# 2  
Old 04-11-2007
Hi,
Check this code
echo $filename | awk '{if(index($0,"CRE") != 0)
{
if($0 ~ /^1/)
{
printf("2%s\n",substr($0,2,length($0)));
}
else
{
print $0;
}
}
else{print $0}}'

Thanks
Raghuram
# 3  
Old 04-11-2007
Code:
sed "/^1.*CRE/s/^1/2/" file

# 4  
Old 04-11-2007
Quote:
Originally Posted by sergiioo
HI all

I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example:

100099C01101C00000000059394200701CREoperadora_TX
100099C01201C00000000000099786137OPERADORA_TX2

in the example above I need to change the first field from 1 to 2 only if the line has the CRE pattern. so the lines will be:

200099C01101C00000000059394200701CREoperadora_TX
100099C01201C00000000000099786137OPERADORA_TX2

Could you help me, how to do this with perl or with awk

Code:
awk '/^1.*CRE/ { $1 = "2" substr($1,2) }
                   { print }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find pattern in first field of file

Hello all I have two files. 1. Pattern.txt - It contains patterns to be matched. It has large number of patterns to be matched. Cat Pattern.txt Ram Shyam Mohan Jhon I have another file which has actual data and records are delimted by single or multiple spaces. 2.... (8 Replies)
Discussion started by: krsnadasa
8 Replies

2. Shell Programming and Scripting

Replace pattern from nth field from a file

I have posted this again as old post is closed and I am not able to reopen. so please consider this new post Input File : 1,A,Completed,06.02_19.36,Jun 30 20:00 2,BBB,Failed,07.04_05.12,Jul 21 19:06 3,CCCCC,New,07.21_03.03,Jul 26 12:57 4,DDDDD,Pending,, I wast output file as: ... (7 Replies)
Discussion started by: Amit Joshi
7 Replies

3. Shell Programming and Scripting

Replace pattern from nth field from a file

$ cat /cygdrive/d/Final2.txt 1,A ,Completed, 07.03_23.01 ,Jun 30 20:00 2,BBB,Pending,, 3,CCCCC,Pending,, 4,DDDDD,Pending,, 5,E,Pending,, 6,FFFF,Pending,, 7,G,Pending,, In the above file 4th field is date which is in MM.DD_HH.MIN format and I need to convert it to as it is there in 5th... (1 Reply)
Discussion started by: Amit Joshi
1 Replies

4. Shell Programming and Scripting

To find char field and replace null

hi, i having a file with | seperated in which i need to search char in 3rd column and replace with null. i need to replace only the coulmn where character occurs in 3rd field for eg: file1.txt xx|yy|xx|12 output file: xx|yy||12 (5 Replies)
Discussion started by: rohit_shinez
5 Replies

5. UNIX for Dummies Questions & Answers

Serach pattern in one field and replace in another

Hi all, I have a TAB separated file like this: sample.rpt: 54 67 common/bin/my/home {{bla bla bla}} {bla bla} Replace Me 89 75 bad/rainy/day/out {{ some bla} } {some bla} Dontreplace Me ...... ...... I wish to do a regexp match on the 3rd... (2 Replies)
Discussion started by: newboy
2 Replies

6. Shell Programming and Scripting

Replace a data in a field if that does not contain a particular pattern

Hi, I have a date/time field in my file. I have to search in all the records and append a timestamp to it, if the timestamp is missing in that field. Is there a possible awk solution for this? Field date format File1 ==== 1|vamu|payer|2007-12-02 02:01:30|bcbs|... (5 Replies)
Discussion started by: machomaddy
5 Replies

7. Shell Programming and Scripting

Find field count and replace

Hello All, I have a file with contents like apple|ball|charlie|David| England|France|Germany| Ireland|Japan|King|London| Man|Nancy|Orange| here the column delimiter is | so if any of the lines/rows in the file has 3 only records (last field is empty), i want to place a | at the end of... (4 Replies)
Discussion started by: vinredmac
4 Replies

8. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

9. Shell Programming and Scripting

Find pattern, and then last field from subsequent lines

I've got a log file, of the format Name: network1 Dropped packets: 15618 Dropped packets for IPv6: 27 Dropped packets: 74 Dropped packets for IPv6: 0 Failed RADIUS Authentication procedures: 0 Failed RADIUS Accounting procedures: 0 Name: network2 Dropped packets: 1117 ... (18 Replies)
Discussion started by: Yorkie99
18 Replies

10. UNIX for Dummies Questions & Answers

Find and replace a field in the last line

I have a file 'test.out' with contents: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| 4|1|10|10|I|hgfj| 34|0|10|10|I|sdg| I want to modify the fifth column with value 'I' to 'A' for only the last line. Below is what I expect to see: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| ... (3 Replies)
Discussion started by: ChicagoBlues
3 Replies
Login or Register to Ask a Question