Match a Pattern & Replace The value Using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match a Pattern & Replace The value Using AWK
# 1  
Old 12-17-2011
Match a Pattern & Replace The value Using AWK

I have a csv file in which i have to search a particular string and replace the data in any column with something else. How do i do it using awk.

Code:
file
------
2001,John,USA,MN,20101001,29091.50,M,Active,Y
2002,Mike,USA,NY,20090130,342.00,M,Pending,N
2003,Steffi,USA,MN,20070101,23222.89,F,Active,Y

Now if i want to search this file for data in column 4, i can say:
Code:
awk -F, '$4 == "MN" {print $1}' file

This would give me the first column from all rows in the file that have MN in 4th column.

But how do i replace the data in say 8th column? e.g Change it to N if 4th column has MN in it.

Thanks.
# 2  
Old 12-17-2011
Does it have to be awk?
Code:
perl -F"," -lane '$F[8]="N" if ($F[3]=="MN"); print join (",",@F)' file.txt

---------- Post updated at 15:03 ---------- Previous update was at 14:58 ----------

My first attempt at awk:
Code:
awk 'BEGIN{FS=",";OFS=","} ($4 == "MN") {$9="N"}1' file.txt


Last edited by balajesuri; 12-17-2011 at 08:04 AM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-17-2011
great job..it worked Smilie
# 4  
Old 12-18-2011
Code:
awk '$4=="MN" {$NF="N"}1' FS=, OFS=, infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If pattern match, replace it with #

This command is not working for me. awk '{if ($1 == server) {$1 = #server} }' /etc/ntp.conf # grep server /etc/ntp.conf # Use public servers from the pool.ntp.org project. server 0.rhel.pool.ntp.org iburst server 1.rhel.pool.ntp.org iburst server 2.rhel.pool.ntp.org iburst server... (5 Replies)
Discussion started by: kenshinhimura
5 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

Pattern Match & Extract from a string

Hi, I have long string in 2nd field, as shown below: REF1 | CLESCLJSCSHSCSMSCSNSCSRSCUDSCUFSCU7SCV1SCWPSCXGPDBACAPA0DHDPDMESED6 REF2 | SBR4PCBFPCDRSCSCG3SCHEBSCKNSCKPSCLLSCMCZXTNPCVFPCV6P4KL0DMDSDSASEWG I have a group of fixed patterns which can occur in these long strings & only... (11 Replies)
Discussion started by: karumudi7
11 Replies

4. Shell Programming and Scripting

How to replace with pattern match using Perl

I have to replace a line, if it has a pattern for example Suppose file.out contains: <tr><td class="cB">Hello</td><td class="cB">1245</td><td class="cB">958</td><td class="cRB">1.34</td><td class="cRB">1.36</td></tr> <tr><td class="cB">world</td><td class="cB">3256</td><td... (8 Replies)
Discussion started by: sol_nov
8 Replies

5. Shell Programming and Scripting

Help with Pattern match and replace

I have a file containing a multiple lines of the format sddfdsf_gaf/ywrtrtwrt_gaf ghfghfgh_ert/xcvxcvcv_ert werwerwwerw_adf/jkhjkhjkjhkjhk_adf I am interested in only the first 3 letters following the "_" character and make those 3 letters uppercase after extraction. So would like to convert... (5 Replies)
Discussion started by: inditopgun
5 Replies

6. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

7. Shell Programming and Scripting

replace pattern after the first pattern match

I need this. aaa OOOOO bbb ccc OOOOO ddd fff ggg OOOOO iii OOOOO I need all OOOOO replaced with PPPPP, but only change after the pattern ggg. So the first two OOOOO should not be changed. OUTPUT should be :- aaa (2 Replies)
Discussion started by: anilcliff
2 Replies

8. Shell Programming and Scripting

Match pattern and replace with string

hi guys, insert into /*<new>*/abc_db.tbl_name this is should be replaced to insert into /*<new>*/${new}.tbl_name it should use '.' as delimiter and replace is there any way to do it using sed (6 Replies)
Discussion started by: sol_nov
6 Replies

9. Shell Programming and Scripting

Match pattern and replace

Hi All, I am new to unix shell scripting, I need your help guys in coming up with some thing for the following scenario: file1 ABC_BASE ${base} ABC_ACC ${acc} ABC_TEST ${test} 01-01-2006 ${from_dt} 01-15-2006 ${to_dt} file 2 I have an file2.sql file which contains: ####This... (4 Replies)
Discussion started by: sol_nov
4 Replies

10. Shell Programming and Scripting

Replace 1 word after pattern match

Hi, Here is my pattern CREATE USER LZ IDENTIFIED BY VALUES 'A0144280ESD70' DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP1 PROFILE DEVELOPER_D_1 ACCOUNT UNLOCK / The Sed command must look for the Line that contains TEMPORARY TABLESPACE and replace the immediate word... (4 Replies)
Discussion started by: rajan_san
4 Replies
Login or Register to Ask a Question