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
# 1  
Old 02-14-2011
sed to find first appearance and append string

I have a file like below

Code:
#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.
I tried the below...but it is appending to each line where GROUP A is there.

Code:
sed '/GROUP A/s/$/ vkk/' $i > $i.new


Last edited by pludi; 02-14-2011 at 08:38 AM.. Reason: please use code tags
# 2  
Old 02-14-2011
Code:
sed '/^GROUP A/!d;s/$/ vkk/;q' $i >$i.new

# 3  
Old 02-14-2011
Thanks. But that is deleting all my other lines and displaying only that line alone. I need the file as such. Smilie

Is that because of "!d" in the command??
# 4  
Old 02-14-2011
Pls post the output you intend
# 5  
Old 02-14-2011
Please find the output below i want my name to be there without deleting any line of the file.

#GROUP A belongs to Asia
GROUP A jojh hans local admin vkk
GROUP A gege fans michel jing jong

#GROUP U belongs to USA
GROUP U jeff goal hello world
# 6  
Old 02-14-2011
Try this,
Code:
sed  '0,/^\(GROUP A\)\(.*\)/s//\1\2 vkk/' infile

# 7  
Old 02-14-2011
Should use awk instead of sed.

smthg like

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


Last edited by ctsgnb; 02-14-2011 at 09:12 AM..
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