Paste a string after another string in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Paste a string after another string in file
# 1  
Old 02-20-2012
Paste a string after another string in file

Hello,
how can I paste a string1 after a particular string2 in a file?
# 2  
Old 02-20-2012
Hi,

can you provide sample input and expected output?
can you elabrate your requirement ?

Cheers,
RangaSmilie
# 3  
Old 02-20-2012
I have a file in the form:
Code:
title1:
title2:
title3:
title4:

I want to paste the string "name" after title2. That will be:
Code:
title1
title2: name
title3
title4

# 4  
Old 02-20-2012
Code:
sed "s/title2:/& name/" inputfile

# 5  
Old 02-20-2012
paste

where can i get the name, is it constant one or else it may be dynamic value ?

I assume that name is a constant one that wont get anywhere from the file.

Code:
 
1. sed 's/^\(title2:\)/\1 name/g' Input_File
2. awk '{if( $0 ~ /^title2:/ ){print $0,"name";}else{print $0;}}' Input_File

Cheers,
RangaSmilie
# 6  
Old 02-20-2012
Thank you all for your replies.

rangarasan, you are right, "name" is a dynamic value. In fact I have multiple 'title' files and a file with a list of 'names'. My actual code would be to read each line from the 'name' list and insert this after "title2" in each 'title' file.
# 7  
Old 02-20-2012
paste

okay Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Grep string in a file and paste next line in a specific way

Hello, I know there are many questions and replies regarding grep command. What I would like to do is a bit different. File A: hello world welcome to my page this is my test site how are you I am fine, thank you where have you been I was in hospital really hope you are fine now Thanks,... (10 Replies)
Discussion started by: baris35
10 Replies

3. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

4. Shell Programming and Scripting

Cut, replace and Paste a String from one file to another

I need to cut all the Strings in one file and Paste it in the another file in the Specific line by replacing the specific String. For Example Step 1: From the newfile.txt, i need to copy all the strings newfile.txt How are you, I am fine, How is your work newfle2.txt Hello david,... (2 Replies)
Discussion started by: Padmanabhan
2 Replies

5. Shell Programming and Scripting

How to paste string yanked into an edit in cmd mode?

I have 2 files open in Vim at the same time. I yank from one with 16yw and get this INSERT INTO ALIS_PART_MANU_PART_TAB (SEQUENCE_NO,PART_NO,MANU_NO,MANU_PART_NO,ROWVERSION) VALUES ( Now I go to the second file and type the command to add this to the beginning of every... (3 Replies)
Discussion started by: djehresmann
3 Replies

6. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

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

8. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

9. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

10. Shell Programming and Scripting

Copy-paste string automatically

Hi i'm not really sure if it's possible or not in bash. Basically I want to convert a ssh key created with ssh-keygen to putty format. The problem is that puttygen doesn't have an option for supplying passphrase in batch mode so it must be entered manually. For sskey generated with ssh-keygen i use... (2 Replies)
Discussion started by: ktm
2 Replies
Login or Register to Ask a Question