vkk
February 14, 2011, 7:35am
1
I have a file like below
#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.
sed '/GROUP A/s/$/ vkk/' $i > $i.new
ctsgnb
February 14, 2011, 7:45am
2
sed '/^GROUP A/!d;s/$/ vkk/;q' $i >$i.new
vkk
February 14, 2011, 7:50am
3
Thanks. But that is deleting all my other lines and displaying only that line alone. I need the file as such.
Is that because of "!d" in the command??
ctsgnb
February 14, 2011, 7:50am
4
Pls post the output you intend
vkk
February 14, 2011, 7:52am
5
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
Try this,
sed '0,/^\(GROUP A\)\(.*\)/s//\1\2 vkk/' infile
ctsgnb
February 14, 2011, 8:05am
7
Should use awk instead of sed.
smthg like
nawk '/^GROUP A/&&!f{sub(/$/," vkk",$0);f=1}1' $i > $i.new
vkk
February 14, 2011, 8:07am
8
No change pravin...
@ctsgnb : How using awk???
ctsgnb
February 14, 2011, 8:15am
9
I updated my previous post :
Use something like :
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)
vkk
February 14, 2011, 8:18am
10
Excellent!!! That worked...Thanks for your effort
panyam
February 14, 2011, 8:27am
11
A bit variation in technique:
awk -v c=0 '/^GROUP/&&c==0 { print $0" vvk";c=1;next}1' input_File
alister
February 14, 2011, 5:15pm
12
Hello, ctsgnb: That's just crazy talk. This situation clearly calls for [s]ed
printf '/^GROUP A/s/$/ vkk/\nw\nq\n' | ed -s file
ed -s file <<EOED
/^GROUP A/s/$/ vkk/
w
q
EOED
sed '
/^GROUP A/ {
s/$/ vkk/
:loop
n
b loop
}' file
Regards,
Alister
1 Like
ctsgnb
February 15, 2011, 5:00am
13
@Alister :
Yes , i am crazy, that's not a scoop
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 !