sed to update string pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed to update string pattern
# 1  
Old 09-12-2018
sed to update string pattern

Hi
I have to update string pattern using sed. If I find any keyword "post" I have to add a function "yahoo" to that keyword. The keyword "post" may or may not come with some prefix too like "abc.post" or "def.post" etc or only "post". The sed must only change when word is completely matched. Like compost must remain compost only. Only post should change to yahoo(post) or abc.post to yahoo(abc.post).
Below is sample example:
Input:

Code:
Hi this is another abc.post. This post is new. Not compost. Dummy String - dbc.post.

Output Required:

Code:
Hi this is another yahoo(abc.post). This yahoo(post) is new. Not compost. Dummy String - yahoo(dbc.post).

I m trying
sed -i "s/\bpost\b/yahoo(post)/g"

but this does not takes care of abc.post, dbc.post & other similar prefix.

Thanks in advance.
# 2  
Old 09-12-2018
Hello dashing201,

Could you please try following.

Code:
awk '{for(i=1;i<=NF;i++){if($i ~ /\.[Pp][oO][sS][tT]/ || $i ~ /^[Pp][oO][sS][tT]$/){sub(/\.$/,"",$i);$i="yahoo("$i")."}}} 1'  Input_file

Thanks,
R. Singh
# 3  
Old 09-12-2018
Hi Ravindra

Thanks for quick reply.
Actual "post" is also a dummy keyword.
I have to run while loop for 20,000 such keywords.

Please share more generic solution.
# 4  
Old 09-12-2018
Quote:
Originally Posted by dashing201
Hi Ravindra
Thanks for quick reply.
Actual "post" is also a dummy keyword.
I have to run while loop for 20,000 such keywords.
Please share more generic solution.
Sorry Mate, we are here are NOT for free coding service. You have to show your efforts(it could be logical or could be coding) which you have put in order to solve your problem. Then You should mention all details in single shot itself rather than asking us questions each time adding some stuff in there.

Thanks,
R. Singh
# 5  
Old 09-12-2018
thanks anyways.
I have already shared above what I tried.
May be I didn't put things very clearly at first place. But if things are "hardcoded" life becomes so simple. Right ?
# 6  
Old 09-12-2018
Quote:
Originally Posted by dashing201
thanks anyways.
I have already shared above what I tried.
May be I didn't put things very clearly at first place. But if things are "hardcoded" life becomes so simple. Right ?
Hello dashing201,

How come anyone will come to know that you have 20'000 keywords which you have do like this? Can you take my code as a start and post it here what you have tried then?

Thanks,
R. Singh
# 7  
Old 09-12-2018
Hi Ravindra

I have tried your suggestion with some modification

Code:
echo "Hi this is another abc.post. This post is new. Not compost. Dummy String - dbc.post."|awk '{for(i=1;i<=NF;i++){if($i ~ /\.post/ || $i ~ /^post$/){sub(/\.$/,"",$i);$i="yahoo("$i")."}}} 1'

Only thing is output is having "." after post keyword.

Code:
Hi this is another yahoo(abc.post). This yahoo(post). is new. Not compost. Dummy String - yahoo(dbc.post).

I don't need that extra "." in output - This yahoo(post). is new.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

8. Shell Programming and Scripting

Delete string between 3rd tab and first pattern with SED

Hello, I have this sentence :Pattern1 Pattern2 Pattern3 Pattern4-which-contains-HELLO-string-and-other-stuff-and-second-HELLO-and-third-HELLO I want to delete everything between the 3rd tab (\t) and the FIRST pattern "HELLO" of the line. Result expected is : Pattern1 ... (7 Replies)
Discussion started by: theclem35
7 Replies

9. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

10. Shell Programming and Scripting

extract a string after a pattern using sed

I have a very large file and each line has a pattern and it is not position specific. I need to extract the string after the pattern ****MI* is the pattern in the red color 12 digit number is the sting value in the green color and it ends with ~ e.g. ... (1 Reply)
Discussion started by: gunaah
1 Replies
Login or Register to Ask a Question