Search and Replace a text if the line contains a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and Replace a text if the line contains a pattern
# 1  
Old 08-23-2013
Search and Replace a text if the line contains a pattern

My text file looks like below

Code:
.
.
.
abcdefghi
jklmnop

$Bad_ptrq_GTS=rcrd_ip.txt
$Bad_abcd_REJ=rcrd_op.txt

ghijklm
$Bad_abcd_TYHS=rcrd_op.txt
abcgd

abcdefghi
jklmnop

$Bad_ptrq_GTS=rcrd_ip.txt
$Bad_abcd_JER=rcrd_op.txt

qrstuv
$Bad_abcd_TYHS=rcrd_op.txt
wxyz
.
.
.

I have replace .txt with .log only if the line contains $Bad*GTS or $Bad*JER

Output:
Code:
.
.
.
abcdefghi
jklmnop

$Bad_ptrq_GTS=rcrd_ip.log
$Bad_abcd_REJ=rcrd_op.log

ghijklm
qrstuv
$Bad_abcd_TYHS=rcrd_op.txt
abcgd

abcdefghi
jklmnop

$Bad_ptrq_GTS=rcrd_ip.log
$Bad_abcd_JER=rcrd_op.log

qrstuv
qrstuv
$Bad_abcd_TYHS=rcrd_op.txt
wxyz
.
.
.

Smilie
# 2  
Old 08-23-2013
Code:
awk '/\$Bad/&&(/GTS/||/JER/||/REJ/)&&/txt/ {gsub(/.txt/, ".log")} 1' infile

This User Gave Thanks to krishmaths For This Post:
# 3  
Old 08-23-2013
Quote:
$Bad_abcd_REJ=rcrd_op.txt
Is this a typo?
Shorten the solution krishmaths posted.
(no need to test for txt, since txt is what we replace)

Code:
awk '/\$Bad.*(GTS|JER)/ {gsub(/.txt/, ".log")} 1' file
abcdefghi
jklmnop

$Bad_ptrq_GTS=rcrd_ip.log
$Bad_abcd_JER=rcrd_op.log

ghijklm
$Bad_abcd_TYHS=rcrd_op.txt
abcgd

abcdefghi
jklmnop

$Bad_ptrq_GTS=rcrd_ip.log
$Bad_abcd_JER=rcrd_op.log

qrstuv
$Bad_abcd_TYHS=rcrd_op.txt
wxyz

This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

2. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

3. UNIX for Dummies Questions & Answers

VIM search and replace with line breaks in both the target and replacement text

Hi, Ive spent ages trying to find an explanation for how to do this on the web, but now feel like I'm :wall: I would like to change each occurence (there are many within my script) of the following: to in Vim. I know how to search and replace when it is just single lines... (2 Replies)
Discussion started by: blueade7
2 Replies

4. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

5. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

8. Shell Programming and Scripting

search a pattern and replace the whole line

Hi All, I have a requirement where I have to find a pattern in a file and comment the whole line containing the search pattern. Any ideas in shell is welcome. Thanks in advance, Regards, Arun (3 Replies)
Discussion started by: arun_maffy
3 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

Search and replace multi-line text in files

Hello I need to search for a mult-line text in a file exfile1 and replace that text with another text. The text to search for is in exfile2 and the replacement text is in exfile3. I work with kornshell under AIX and need to do this with a lot of files. (the file type is postscript and they need... (10 Replies)
Discussion started by: marz
10 Replies
Login or Register to Ask a Question