Replace exact word by perl


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Replace exact word by perl
# 1  
Old 01-21-2016
Replace exact word by perl

my inputfile:

This is a test and only a test for production - prod.

Code:
echo "This is a test and only a test for production - prod" | perl -pi -e 's/prod/test/g'

outputfile:
Code:
This is a test and only a test for testuction - test

I only want to replace prod, not production.

I also tried to use sed:

Code:
echo "This is a test and only a test for production - prod" | sed -e 's/prod/test/g'
This is a test and only a test for testuction - test

Thank you in advance for helping.

Moderator's Comments:
Mod Comment Pleas use code tags for you code and data, nothing else... Thanks

Last edited by vbe; 01-21-2016 at 09:38 AM..
# 2  
Old 01-21-2016
This highly depends on e.g. the sed version you are running. With mine(sed (GNU sed) 4.2.2), this works:
Code:
echo "This is a test and only a test for production - prod" | sed -e 's/\<prod\>/test/g'
This is a test and only a test for production - test

This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-21-2016
I found this on unix.com
Code:
sed 's/\<apple\>/XXX/g' filename

and solved my sed

I am digging for perl. Again, thank you.

Last edited by vbe; 01-21-2016 at 09:41 AM.. Reason: code tags
# 4  
Old 01-21-2016
You can try:
Code:
perl -pi -e 's/\bprod\b/test/g'

This User Gave Thanks to Scrutinizer 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

How to take exact word from output.?

Hi i am writing and i want to take very first word "A924A5FC"from the below o/p A924A5FC 0910055313 P S SYSPROC SOFTWARE PROGRAM ABNORMALLY TERMINATED A924A5FC 0908091913 P S SYSPROC SOFTWARE PROGRAM ABNORMALLY TERMINATED A924A5FC 0906090313 P S SYSPROC SOFTWARE... (4 Replies)
Discussion started by: scriptor
4 Replies

2. Shell Programming and Scripting

How to get the exact word in awk?

Hi, i have a file that contains the following: ARTPRD01_app = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 11.222.3.4)(PORT = 1540)) (CONNECT_DATA = (SERVICE_NAME = artprd01.com) ARTPRD01 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 11.223.3.1)(PORT =... (2 Replies)
Discussion started by: reignangel2003
2 Replies

3. UNIX for Advanced & Expert Users

Perl command to replace word in file...

Hi, I want to search for a specific word in file and replace whole line with new text. e.g. 1) I have file with below lines APP=ABCD 12/12/2012 DB=DDB 01/01/2013 I need perl command which will check for APP=$VAL and replace whole line with APP=$NEWVAL $NEWDT Simlarly need a... (2 Replies)
Discussion started by: mgpatil31
2 Replies

4. Shell Programming and Scripting

Grep Exact word

This may be stupid question but not able to solve it. How to grep exact word and line along with it. TEST:/u00/app/oracle/product/10.2.0/TEST:N TEST2:/u00/app/oracle/product/10.2.0/ODS:N TEST3:/u00/app/oracle/product/10.2.0/TEST:N TEST4:/u00/app/oracle/product/10.2.0/ODS:N... (4 Replies)
Discussion started by: tapia
4 Replies

5. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

6. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

7. Shell Programming and Scripting

perl: replace multiple word on a line

Hi All, If I have a line as following: ( MA "vertical" ) How can I convert it to as below: ( BC "horizontal" ) Thanks, --Michael (6 Replies)
Discussion started by: mxn731
6 Replies

8. Shell Programming and Scripting

Replace exact word with blank

i have a file with the below content file1.txt ALERTADMIN.FIELD ALERTADMIN.TX_ALERTS_LOG i have another file file2 ALERTADMIN.FIELD ALERTADMIN.FIELD_WS ALERTADMIN.SECTION_FIELD_WS ALERTADMIN.TX_ACCT_PROCESSING_WORK_TABLE ALERTADMIN.TX_ACCT_REVIEW_EXEC_METRICS... (2 Replies)
Discussion started by: lavnayas
2 Replies

9. Shell Programming and Scripting

Exact Word Match

I'm trying to find a exact word match but couldn't do it. ABC ABC_NE Searching for ABC_NE tried grep -w </ABC_NE/> grep "^ABC_NE$" but didn't worked , any awk variants would also help. ---------- Post updated at 08:40 AM ---------- Previous update was at 06:48 AM ---------- I... (2 Replies)
Discussion started by: dinjo_jo
2 Replies

10. Shell Programming and Scripting

Replace aword in a.The replaced word should not be overwitten in perl(details inside)

Hi i am trying to write a perl program where i have to open a 1)directory "unit" 2) rename the files in the dir say file1.txt;file2.txt...file5.txt to file1_a.txt;file2_a.txt,....file5_a.txt ;file1_x.txt ;file2_x.txt 3) open these renamed files and replace the words lets say file1_a.txt... (7 Replies)
Discussion started by: madhul2002
7 Replies
Login or Register to Ask a Question