geeko
September 25, 2007, 1:24am
1
I have a file like this..
Maharastra Mumbai worli
Maharastra Mumbai navy
maharatra Pune
Maharastra Nagpur
Karnataka Bangalore
Karnataka Mysore
Karnataka Mangalore
Punjab Amritsar
punjab Jalandar
my expected outcome should be like this
Maharastra Mumbai worli
---------- Mumbai Navy
---------- pune
---------- Nagpur
Karnataka Bangalore
--------- mysore
--------- Mangalore
Punjab Amritsar
--------- Jalandar
i am not able to find any similar posts,please reply
thanks in advance,
regards,
Geeko
i am also struggling with the same one, can some one have look into it. so that it'll be useful ( need solution for bash shell)
thanks,
vino
September 25, 2007, 3:06am
3
Two users with the same requirement! Is it homework ?
Hi,
Hope this one can help you.
iuput(a):
Maharastra Mumbai worli
Maharastra Mumbai navy
maharatra Pune
Maharastra Nagpur
Karnataka Bangalore
Karnataka Mysore
Karnataka Mangalore
Punjab Amritsar
punjab Jalandar
output:
Maharastra Mumbai worli
-------- Mumbai navy
maharatra Pune
Maharastra Nagpur
Karnataka Bangalore
-------- Mysore
-------- Mangalore
Punjab Amritsar
-------- Jalandar
code:
cat a | nawk '
{
if (cur=="")
{
cur=toupper($1)
print $0
}
else
{
if (toupper($1)==cur)
print "--------"substr($0,index($0," "),length($0)-length($1))
else
{
print $0
cur=toupper($1)
}
}
}'
geeko
September 25, 2007, 4:45am
6
thanks a lot for your code summer cherry, the command nawk didn't work, but with awk command, i met my requirement.
How can I replace a string from a multiple lines. I have two words in my text file and want to replace some text from a particular position in second word.
example :
I have a line :
grep 445170141207
and I want :
grep 44495 *list*
that is from 6th position I want to insert a string *list* in my second word.
Can any one help me in this regard.
Thanks in Advance
nawk '{
if (temp=="")
{
temp=toupper($1)
print
}
else
{
t=""
if(toupper($1)==toupper(temp))
{
for(i=1;i<=length(temp);i++)
t=sprintf("%s%s",t,"-")
$1=t
print
}
else
{
temp=toupper($1)
print
}
}
}' a
How do I replace only if the keyword is at the begining of a line?
Code:
--
a = �This is a print statement�
print a
--
What if I want to replace print by #print only in the second line i.e only if the line starts with that keyword.
Please help me out. I'm new to SED.