Search and Append


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and Append
# 8  
Old 02-07-2011
Code:
awk -v tag="<a clase=\"string-array\">" '$0~tag {++a["tag"]}{print (($0~tag && a["tag"]==1)?$0"\n<string>java</string>":$0)}' urfile


Last edited by yinyuemi; 02-07-2011 at 03:34 PM..
# 9  
Old 02-08-2011
Code:
sed '0,/<a class="string-array">/s//&\n<string>java<\/string>/' inputfile > outfile

This exactly worked for me.

One more hint required,

I set the pattern values into variables and trying to run. But I get below exception, can I know what wrong am doing..

Code:
sed '0,/'$searchenv'/s//&\n'$appendenv'/' config.xml > temp.xml

Error: sed '0,/'$searchenv'/s//&\n'$appendenv'/' config.xml > temp.xml

I tried double/single quote to variable in sed.
# 10  
Old 02-09-2011
You would need to use double quotes..
Code:
searchenv='<a class="string-array">'
appendenv='<string>java<\/string>'

sed "0,/$searchenv/s//&\n$appendenv/"

# 11  
Old 02-09-2011
No exceptions or No Changes in output file on using latest command mentioned
Code:
sed "0,/$searchenv/s//&\n$appendenv/"


Last edited by vino_hymi; 02-09-2011 at 05:12 AM..
# 12  
Old 02-09-2011
can you post your execution line..?
Code:
sed "0,/$searchenv/s//&\n$appendenv/" inputfile > outfile


Last edited by michaelrozar17; 02-09-2011 at 05:47 AM.. Reason: added sample line of execution..
This User Gave Thanks to michaelrozar17 For This Post:
# 13  
Old 02-09-2011
Sorry just I made a mistake in setting the value to variable.

The command
Code:
 sed "0,/$searchenv/s//&\n$appendenv/" inputfile > outfile

worked perfectly.

Thanks once again.
# 14  
Old 02-09-2011
i guessed that would happen and posted the variable values in post# 10 Smilie 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 and Replace+append a text in python

Hello all, I have a verilog file as following (part of it): old.v: bw_r_rf16x32 AUTO_TEMPLATE ( 1957 // .rst_tri_en (mem_write_disable), 1958 .rclk (clk), 1959 .bit_wen (dva_bit_wr_en_e), 1960 .din ... (5 Replies)
Discussion started by: Zam_1234
5 Replies

2. Shell Programming and Scripting

Search id from second file and append in first

Hello, I want to search a string/substring from the second column in file in another file and append the first found record in second file to the end of the record in the first file. Both files are tab delimited. All lines with KOG in col13 do not need to be searched as it will not be... (7 Replies)
Discussion started by: gina.lizar
7 Replies

3. Shell Programming and Scripting

Search for a string, then append character to end of that line only

I have 2 files that I am working with $ cat file1 server1 server3 server5 server6 server8 $ cat file2 server1;Solaris; server2; SLES; server3;Linux; server4; Solaris; server5;SLES; server6;SLES; server7;Solaris; server8;Linux; (1 Reply)
Discussion started by: snoman1
1 Replies

4. Shell Programming and Scripting

Search text and append using SED?

I have file . cat hello.txt Hello World I would like to append a string "Today " so the output is cat hello.txt Hello World Today I dont know which line number does the "Hello World" appears otherwise I could have used the Line number to search and append . (3 Replies)
Discussion started by: gubbu
3 Replies

5. Shell Programming and Scripting

Search for a particular field length and append '0' if less less than 10

Hi, I am new to Unix. Please help me in finding solution for the below scenario. I have a flat file that contains data like 378633410|3013505414|new_378633410|ALBERT|WALLS|378633410|Rew||||||| 351049045|239|new_351049045|JIM|COOK|351049045|Rew|||||||... (6 Replies)
Discussion started by: anandek
6 Replies

6. Shell Programming and Scripting

How to search and append words in a file

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958 only in... (2 Replies)
Discussion started by: gsreeni
2 Replies

7. Shell Programming and Scripting

Trying to search for a string and append text only once

Hi I am trying to search for a particular occurrence of a string in a file, and if found, append another string to the end of that line. Here is my file contents: column1 userlist default nowrite=3 output=4 column2 access default nowrite=3 Here is the code: A="user=1... (1 Reply)
Discussion started by: bludhemn
1 Replies

8. UNIX for Dummies Questions & Answers

Grep search and append

I'm sure glad I found this forum... This is my first post, so please be gentle... ;) I tried searching everywhere, but the terminology is so common that I cannot find a solution to my problem. I'm looking for a GREP statement to do the following... I need a search and append function... (5 Replies)
Discussion started by: rmanke
5 Replies

9. Shell Programming and Scripting

Perl search and append new line

Dear All, I want search two lines and append some string in between these lines. Input file tmp,123 ,10:123 tmp,666 ,50:999 tmp,2:19800 5,3:21. tmp,2:19800 55555555 tmp,2:19800 5,3:21.Output should be tmp,123 ,10:123 tmp,666 ,50:999 tmp,2:19800 (4 Replies)
Discussion started by: arvindng
4 Replies

10. Shell Programming and Scripting

Append text from one file to another based on a search from the end of a document

Hi all, I have output files that are all text files with various different extensions. So, if I submit the input file "job_name.inp", when it finishes I get an output file "job_name.dat". A typical input file looks something like this: $CONTRL SCFTYP=RHF RUNTYP=ENERGY MAXIT=199 MULT=1... (4 Replies)
Discussion started by: marcozd
4 Replies
Login or Register to Ask a Question