Serach pattern in one field and replace in another


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Serach pattern in one field and replace in another
# 1  
Old 07-26-2012
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 item, say "common/bin/my/home" and if there is a match i want to replace the 6th item "Replace Me" by my own text like "happy Home"

Additionally my regexp will be a shell variable, so I need a command which accepts regexp as variable

The items may have spaces or backslashes and the file is Tab separated.

Is there a simple one liner using sed or awk which can perform this over entire file in one command ?


Thanks in advance Smilie
# 2  
Old 07-26-2012
Try...
Code:
awk 'BEGIN{FS=OFS="\t"}$3~s{$6=r}1' s='common/bin/my/home' r='happy Home' sample.rpt

This User Gave Thanks to Ygor For This Post:
# 3  
Old 07-26-2012
Thanks for soln. I figured out one way to d it myself:

awk -F "\t" -v drv=$mod_pat '$3 ~ drv { OFS= "\t"; $6 = " happy_home"; print;}' sample

---------- Post updated at 06:34 PM ---------- Previous update was at 06:19 PM ----------

I got caught in the else part of condition:

Below I am checking if field 3 matches the pattern specified by drv do whatever next in braces {}

awk -F "\t" -v drv=$mod_pat '$3 ~ drv { OFS= "\t"; $6 = " happy_home"; print;}' sample


The problem now is I want to just print the line untouched if the pattern is not matched. Is this possible in the same command ?
I tried:

awk -F "\t" -v drv=$mod_pat ' if ($3 ~ drv) { OFS= "\t"; $6 = " happy_home"; print;} else {print;}' sample.rpt


But its throwing parse error. Probably some problem with awk syntax.


Any help will be appreciated.

Thanks

---------- Post updated at 09:00 PM ---------- Previous update was at 06:34 PM ----------

Anyhelp ? is this thread closed ?? Smilie

---------- Post updated at 09:39 PM ---------- Previous update was at 09:00 PM ----------

Thanx anyways, got it:

This works:

awk -F "\t" -v drv=$mod_pat '{ if ($3 ~ drv) { OFS= "\t"; $6 = " happy_home"; print;} else {print;} }' sample.rpt

Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

Script to Serach pattern and give number of occurrences

Hi, I want a script which search for a pattern "good" in a huge file and provide me number of occurences of such pattern in a file. lets say i have a file test.txt contents as below good is good but good is sometime bad and sometime good you are very good and good is always good ... (7 Replies)
Discussion started by: sv0081493
7 Replies

4. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

5. 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

6. UNIX for Dummies Questions & Answers

Serach a pattern

Hi, I am trying to find a particular patter in multiple UNIX files (also contain system files,hidden files and normal files) i am now using CMD: egrep -ali 'pattern' * i am not getting the required result, i just need files path and finename Naveen (3 Replies)
Discussion started by: Naveen_5960
3 Replies

7. Shell Programming and Scripting

pattern serach using grep

Frds I have to search for a string which is starting with brighton which will be first in the line of a text file test1.txt. The contents of test1.txt are file names like ----- brighton brighton_gt hst_brighton gst_brighton -------so many files------ --------- i have retrieve only... (3 Replies)
Discussion started by: KiranKumarKarre
3 Replies

8. UNIX for Dummies Questions & Answers

Pattern Matching - serach and replace script

My requirement is to replace a a particular pattren in a script from A to B. I am not sure if this can be done through sed command or through awk . The file sv.inc is window DialogBox AddConnection tag "~ActiveApp/Add Connection - Provider Type?URL" I would wnat the file to be... (10 Replies)
Discussion started by: bsandeep_80
10 Replies

9. Shell Programming and Scripting

serach and replace a specific pattern or value in a xml file

can some one help me with a perl command i have to search and replace a version from a xml-file so i use in a ksh script a command like this ssh $GLB_ACC@$GLB_HOST "/usr/contrib/bin/perl -pi -e "s/$curVersion/$new_Version/g" $Dest_dir/epi.xml" this command worked so far, but the problem... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies

10. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: sergiioo
3 Replies
Login or Register to Ask a Question