sed to find first appearance and append string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to find first appearance and append string
# 8  
Old 02-14-2011
No change pravin...

@ctsgnb: How using awk???
# 9  
Old 02-14-2011
I updated my previous post :
Use something like :

Code:
nawk '/^GROUP A/&&!f{sub(/$/," vkk",$0);f=1}1' $i > $i.new

(or gawk or mawk or just awk depending on what is available and what works on your machine)
# 10  
Old 02-14-2011
Excellent!!! That worked...Thanks for your effort Smilie
# 11  
Old 02-14-2011
A bit variation in technique:
Code:
awk -v c=0 '/^GROUP/&&c==0 { print $0" vvk";c=1;next}1' input_File

# 12  
Old 02-14-2011
Quote:
Originally Posted by ctsgnb
Should use awk instead of sed.
Hello, ctsgnb: That's just crazy talk. This situation clearly calls for [s]ed Smilie
Code:
 printf '/^GROUP A/s/$/ vkk/\nw\nq\n' | ed -s file

Code:
ed -s file <<EOED
/^GROUP A/s/$/ vkk/
w
q
EOED

Code:
sed '
/^GROUP A/ {
    s/$/ vkk/
    :loop
    n
    b loop
}' file

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 13  
Old 02-15-2011
@Alister :

Yes , i am crazy, that's not a scoop Smilie

And you're true, i should have written "Could use awk"
and not "Should use awk instead of sed"

I just don't masterise [s]ed as well as you do

Thanks for your lights ! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gnu tool; sed awk echo etc to prepend or append string to a file

Looking to add text to a file, example File example; nodegroups: check-hosts: L@host.domain.com,host2.domain.com,host3.domain.com I need to take a file with a one line list of hosts separated by commas host.domain.com,host2.domain.com,host3.domain.comand prepend the string " ... (6 Replies)
Discussion started by: bash_in_my_head
6 Replies

2. Shell Programming and Scripting

Using sed to find and append or insert on SAME line

Hi, $ cat f1 My name is Bruce and my surname is I want to use SED to find “Bruce” and then append “ Lee” to the end of the line in which “Bruce” is found Then a more tricky one…. I want to INSERT ….a string… in to a line in which I find sometihng. So example $ cat f2 My name is... (9 Replies)
Discussion started by: Imre
9 Replies

3. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

4. Shell Programming and Scripting

Using sed to Find first string, then next string

I know that if file is now is the time for all good men house boat car for sed -n -e '/is/,/for/p' file will print (3 Replies)
Discussion started by: sumguy
3 Replies

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

6. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: vchee
8 Replies

7. Shell Programming and Scripting

Find string in a file and append character

Hi Experts, Is there a way to find a string in a file then append a character to that string then save the file or save to another file. Here is an example. >cat test.txt NULL NULL NULL 9,800.00 NULL 1,234,567.01 I want to find all NON NULL String and add a dollar sign to those... (9 Replies)
Discussion started by: brichigo
9 Replies

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

9. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

10. Shell Programming and Scripting

how to find the line number of a pattern of first appearance??

Hi, I am doin a project in shell script please answer the above question..... waiting........ (2 Replies)
Discussion started by: shivarajM
2 Replies
Login or Register to Ask a Question