Insert Word After Match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert Word After Match
# 1  
Old 05-31-2017
Insert Word After Match

Dear ALL,

I have sample file :

host.txt

Code:
fullname: FreeBSD
Host: server1
ip: 1.1.1.1
mask: 255.255.255.0

fullname: CentOS
Host: server2
mask: 255.255.255.0

fullname: Fedora
Host: server3
ip: 1.3.1.2
mask: 255.255.255.0

fullname: Asterisk
Host: VoIP1
ip: 1.1.2.4
mask: 255.255.255.0

required output:

Code:
fullname: FreeBSD
Host: server1
ip: 1.1.1.1
mask: 255.255.255.0

fullname: CentOS
Host: server2
ip: no-ip
mask: 255.255.255.0

fullname: Fedora
Host: server3
ip: 1.3.1.2
mask: 255.255.255.0

fullname: Asterisk
Host: VoIP1
ip: 1.1.2.4
mask: 255.255.255.0

Can you help me pls..

Thanks

Last edited by Don Cragun; 05-31-2017 at 01:45 AM.. Reason: Add CODE and ICODE tags again.
# 2  
Old 05-31-2017
Your repeated refusal to properly format your posts has resulted in this account being placed in read-only mode again. (This time for two weeks.) Future posts that are improperly formatted may result in you being permanently banned from using this site.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-31-2017
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Questions forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

If this is not a homework assignment, please explain:
  1. why you need to do this,
  2. what operating system you're using,
  3. what shell you're using, and
  4. show us what you have tried to do to solve this problem on your own.
We want to help you learn how to do things like this on your own, but we are not here to act as your unpaid programming staff and we are not here to do your homework assignments for you.
This User Gave Thanks to Don Cragun 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

Insert text after match in XML file

Having a little trouble getting this to work just right. I have xml files that i want to split some data. I have 2 <name> tags within the file I would like to take only the first tag and split the data. tag example. From this. TAB<Name>smith, john</Name> to TAB<Name>smith,... (8 Replies)
Discussion started by: whegra
8 Replies

2. Shell Programming and Scripting

Insert file after only first match

i'm using the following code to add the entire content of a file (/tmp/resources.txt) to the line directly below the line containing a pattern (wonderful) in the file mainfile.txt: sed '/^wonderful/ r /tmp/resources.txt' mainfile.txt the problem is, it adds the entire content of... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Insert space in a word using sed

Hi all, I have a file that looks like this- ----------------------------- . . ATOM 8 O2' U A 5 135.452 109.687 7.148 1.00 48.99 A16S ATOM 9 C1' U A 5 135.282 111.512 5.641 1.00 48.99 A16S ATOM 10 N1 U A 5 134.647 112.595 ... (2 Replies)
Discussion started by: asmi_g
2 Replies

4. Shell Programming and Scripting

Replacing the first word if the word three match

Dear ALL, I have sample file : IDcentos-forum,bash,linuxCentOS,GNome IEfedora-milis,cli,linuxRedhat,KDE IRfreebsd-milis,aix,unixbsd,pyton required output: centos,bash,linuxCentOS,GNome fedora,cli,linuxRedhat,KDE freebsd,aix,unixbsd,pyton Can you help me pls.. (1 Reply)
Discussion started by: gnulyn
1 Replies

5. Shell Programming and Scripting

Sed; insert text two lines above match

Hi! Considering below text, how would I use sed to insert text right below the v0005-line, using the SEPARATOR-line as a pattern to search for, so two lines above the separator? I can do it right above the separator, but not 2 lines... # v0004 - Some text # v0005 - More text #... (5 Replies)
Discussion started by: indo1144
5 Replies

6. UNIX for Dummies Questions & Answers

Match and insert columns

Hi, I've got two files to match. File one: a1 b c d e a2 b c d e a3 b c d e a4 b c d e and file 2 a1 1 2 3 4 a2 5 6 7 8 a3 9 10 11 12 a4 1 2 3 4 I need to match them by the first column and add the four columns of file 2 to the file 1 so that those added columns go as columns... (5 Replies)
Discussion started by: zajtat
5 Replies

7. Shell Programming and Scripting

How to insert a word into a text file?

Hi, I have a text file with one line having few words separated by space and I need to insert another word on "n"th column/field so that previous word should shift right (to n+1st column). how can I do that? It seems we can do using awk but unable to figure out. Please advise, thanks! ... (11 Replies)
Discussion started by: magnus29
11 Replies

8. Shell Programming and Scripting

Match and insert in a sorted list

I have a sorted list (python) and I want to insert a string if it matches the pattern in list. Example : Sorted List Above list is in sorted order. I need to insert a name in sorted order and also if the name already exist then it should be inserted before the existing... (1 Reply)
Discussion started by: pratapsingh
1 Replies

9. Shell Programming and Scripting

Insert text file only after the first match with SED

Hello, I'm new in Shell scripting but i should write a script, which inserts the license header out of a txt-File into the files in our Projekt. For the Java classes it runs without Problems but for XML files not. At xml-files i have to put the license Header after the xml-Header (?xml... (1 Reply)
Discussion started by: PhoenixONE
1 Replies

10. Shell Programming and Scripting

insert word in each line of a file

can someone tell me how can I insert a word in front of each line in a file. i tried with sed but didn't managed yet. Is there another command or this one(sed) works? 10x anyway. (7 Replies)
Discussion started by: atticus
7 Replies
Login or Register to Ask a Question