Replace particular words in file based on if finds another words in that line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace particular words in file based on if finds another words in that line
# 1  
Old 10-06-2017
Replace particular words in file based on if finds another words in that line

Hi All,

I need one help to replace particular words in file based on if finds another words in that file .

i.e.

Code:
my self is peter@king.
i am staying at north sydney.
we all are peter@king.

How to replace peter to sham if it finds @king in any line of that file.

Please help me out.Smilie

Last edited by Don Cragun; 11-21-2017 at 01:31 AM.. Reason: Add missing CODE and ICODE tags.
# 2  
Old 10-06-2017
Hello Rajib Podder,

Welcome to forums, please use CODE TAGS as per forum rules for commands/codes/Inputs you are posting into your post. Coming to your question now, could you please try following and let me know if this helps you.
Code:
awk '{gsub("peter@king","sham@king")} 1'  Input_file

Let me know if you have any queries on same.

Thanks,
R. Singh
# 3  
Old 10-06-2017
Hi R,

Thanks for your help.

But if it like below format

my self is peter.<variable>@king then how to change only peter to sham based on @king.
i.e.

Code:
myself is peter.r1@king
myself is peter.r2@king
myself is peter.r3@king
myself is peter.r4

ouput should be :

Code:
myself is sham.r1@king
myself is sham.r2@king
myself is sham.r3@king
myself is peter.r4

is it possible

Last edited by Scott; 10-06-2017 at 09:58 AM.. Reason: Added code tags
# 4  
Old 10-06-2017
Hello Rajib Podder,

To thank any person in forums, you could HIT THANKS button at left corner of each post. Also please use CODE TAGS for your shown sample Inputs. Could you please try following and let me know if this helps you.
Code:
awk '/peter.*@king/{sub("peter","sham")} 1'  Input_file

Output will be as follows.
Code:
myself is sham.r1@king
myself is sham.r2@king
myself is sham.r3@king
myself is peter.r4

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-06-2017 at 02:03 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 10-06-2017
Hi Ravi,

But the output should include sham.r1 also etc.It is not visible

---------- Post updated 10-06-17 at 12:05 AM ---------- Previous update was 10-05-17 at 11:59 PM ----------

Hi Ravi,

I think you got my point.

Regards,
Rajib
# 6  
Old 10-06-2017
Hello Rajib Podder,

You could try following sed command too for same.
Code:
sed  '/peter.*@king/s/peter/sham/g'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 10-06-2017
Hi Ravi,

It is working perfectly now.One more question,can we add multiple condition and multiple replacement on same command.

Regards,
Rajib
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 replace some specific words from file?

I have the file like this. cat 123.txt <p> <table border='1' width='90%' align='center' summary='Script output'> <tr><td>text </td> </tr> </table> </p> I want to replace some tags and want the output like below. I tried with awk & sed commands. But no luck. Could someone help me on this? ... (4 Replies)
Discussion started by: thomasraj87
4 Replies

2. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

How to replace words in file?

Hi Guys, I have a text where we used Ram in 10 times now I want replace all Ram words by Shyam word then how to do it. (6 Replies)
Discussion started by: aaditya321
6 Replies

4. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

5. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

6. UNIX for Advanced & Expert Users

cut words based on the word count of a line

I would like to cut words based on the word count of a line. This over here inspired me with some ideas but I wasn't able to get what I needed. https://www.unix.com/shell-programming-scripting/105841-count-words-each-line-file-using-xargs.html If the line has 6 words I would like to use this.... (8 Replies)
Discussion started by: cokedude
8 Replies

7. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. UNIX for Dummies Questions & Answers

To Extract words from File based on Position

Hi Guys, While I was writing one shell script , I just got struck at this point. I need to extract words from a file at some specified position and do some comparison operation and need to replace the extracted word with another word. Eg : I like Orange very much. I need to replace... (19 Replies)
Discussion started by: kuttu123
19 Replies

10. Shell Programming and Scripting

replace words in file based on another file

Hello, Can someone kindy help me solve this problem..I am using SunOS shell script I got a file A with following content: This is my correct document. I wrote 111 This is my incorrect word , 222 This is my wrong statement 333 This is my correct document 444 This is my correct document 555... (9 Replies)
Discussion started by: kinmak
9 Replies
Login or Register to Ask a Question