Append a searched string with another string using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append a searched string with another string using sed
# 1  
Old 10-01-2012
Append a searched string with another string using sed

Hi,

I need to replace and append a string in a text if grep is true. For eg:

grep ABC test.txt | grep -v '\.$' | awk {'print $4'} | sed "s/ ?

How do I replace all instances of "print $4" using sed with another sring? Eg of the string returned will be,
lx123
web222
xyz

Want to replace all the above to become
lx123.abc.com.
web222.abc.com.
xyz.abc.com.

Pls. help. Thanks.
# 2  
Old 10-01-2012
Please post the contents of test.txt and mention what you need to do to get the final/required output along with the required output. This way you might get a better solution than the unnecessary pipeline.
# 3  
Old 10-01-2012
Quote:
Originally Posted by elixir_sinari
Please post the contents of test.txt and mention what you need to do to get the final/required output along with the required output. This way you might get a better solution than the unnecessary pipeline.
The Text is as follows,
Code:
mars            IN      A       202.27.17.78
sentosa         IN      A       202.27.17.100
www             IN      CNAME   mars
majordomo       IN      CNAME   mars
smtp2           IN      CNAME   sentosa
smtp3           IN      CNAME   venus.test.com.

Script suppose to grep for CNAME without the "." at the end and append them with .test.com. so that the above will become,
Code:
mars            IN      A       202.27.17.78
sentosa         IN      A       202.27.17.100
www             IN      CNAME   mars.test.com.
majordomo       IN      CNAME   mars.test.com.
smtp2           IN      CNAME   sentosa.test.com.
smtp3           IN      CNAME   venus.test.com.

Thanks.
# 4  
Old 10-01-2012
Code:
awk -v OFS="\t" '!/\.$/&&$NF!~/^[0-9]/{$NF=$NF".test.com."}1' input.txt

# 5  
Old 10-01-2012
Code:
awk -v VM=".test.com." '/CNAME/{if($NF !~ VM){$NF=$NF""VM}}1' OFS="\t" file

# 6  
Old 10-01-2012
Quote:
Originally Posted by itkamaraj
Code:
awk -v OFS="\t" '!/\.$/&&$NF!~/^[0-9]/{$NF=$NF".test.com."}1' input.txt

How do I change it to apply to only lines with "CNAME"? Will grep work? Thanks.
# 7  
Old 10-01-2012
Quote:
Originally Posted by vchee
How do I change it to apply to only lines with "CNAME"?
see my post above..Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

2. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

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

4. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

5. Shell Programming and Scripting

sed to find first appearance and append string

I have a file like below #GROUP A belongs to Asia GROUP A jojh hans local admin GROUP A gege fans michel jing jong #GROUP U belongs to USA GROUP U jeff goal hello world My requirement is to grep for first apperence of GROUP A which is not commented and append my name to end of file.... (12 Replies)
Discussion started by: vkk
12 Replies

6. Shell Programming and Scripting

sed append to string

I am trying to replace in multiple files every instance of text that begins with http and add hyperlink characters to it. I can get it to work with the following:sed -e "s/http*.*/<a href=\"&\">&<\/a>/g" * as long as the http text is at the end of the file. I need it to stop at the end of the... (2 Replies)
Discussion started by: numele
2 Replies

7. Shell Programming and Scripting

greping last occurrence of the searched string

Hello, I have active log file i want to grep the last occurrence of the word in that log file the log file gets on increasing and increasing i want to fetch it from live file. Please guide me, Thanks in advance (4 Replies)
Discussion started by: vidurmittal
4 Replies

8. Shell Programming and Scripting

put a string before a searched string

hi all! i have a working that looks like this... file1 MALE JOHN MALE ANJO FEMALE ANNE MALE JAMES FEMALE HONEY FEMALE IZA what i want to do is insert "M" when the first string is "MALE" and insert "F" when the first string is "FEMALE". file1 M MALE ... (10 Replies)
Discussion started by: kingpeejay
10 Replies

9. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

10. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies
Login or Register to Ask a Question