Hi Guys,
I need to search a string and print the rest of the lines...
input:
8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n
output:
00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n
Actually i don even need the first "00".. any suggestions is appreciated.. i am trying to split it using split . But its throwing an error.
Regards,
Magesh
---------- Post updated at 03:46 PM ---------- Previous update was at 03:44 PM ----------
Got the answer.. as soon as i posted it..
echo "8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n"|awk '{ if($4 == "1"){substr("^ ","");print}}'|awk -F":" '{print $2}'
---------- Post updated at 03:59 PM ---------- Previous update was at 03:46 PM ----------
But Guys, If found one issue with it.. if we have more than one ":", then my logic gets a hit.. can anyone help me in getting the rest of the string starting from the first : .
Thanks..
clx
January 14, 2010, 5:34am
2
so in the 0:00:00, you need to last "00" part?
try:
awk -F"[ :]" '{print $(NF-2),$(NF-1),$NF}'
xoops
January 14, 2010, 5:41am
3
try
$ echo $str
8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n
$ echo $str | sed -e 's/^.*00//g'
/etc/opt/SUNWconn/atm/bin/atmsnmpd -n
actually i am not sure.. what you are saying.. but this is wat i want
I/P
8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhld : 12364
O/P:
/etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhld : 12364
But my logic will give only
00 /etc/opt/SUNWconn/atm/bin/atmsnmpd
Your code was giving me an error
echo "8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n"|awk -F"[ :]" '{print $(NF-2),$(NF-1),$NF}'
awk: trying to access field -1
record number 1
clx
January 14, 2010, 5:52am
5
opps!! I didnt read the requirement properly.
Thought you need that part. but actually you don't.
Apologies.
try:
awk -F"[ :]" '{print $(NF-1),$NF}'
---------- Post updated at 04:20 PM ---------- Previous update was at 04:17 PM ----------
you changed the input line thats why it wont work
dont try this.
---------- Post updated at 04:22 PM ---------- Previous update was at 04:20 PM ----------
try this for the new input line:
awk -F" /" '{print $NF}'
actually anchal again.. it gave me only the ones starting from the last :...
But i need all the characters starting from "first :"..
clx
January 14, 2010, 6:04am
7
I didn't understand. as per the i/p you posted, its working..
/home->echo '8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhld : 12364'| awk -F" /" '{print $NF}'
etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhld : 12364
/home->
if you need "/" also in the beginning,
/home->echo '8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhld : 12364'| awk -F" /" '{print "/"$NF}'
/etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhld : 12364
/home->
tcmsd003:/cust/home/dsdev>echo $a
8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhldv :12346
tcmsd003:/cust/home/dsdev>echo $a|awk -F" /" '{print "/"$NF}'
/:12346
Mine is SunOs..
is that make any difference?
clx
January 14, 2010, 6:18am
9
I am not sure but on solaris, it is always advised to use nawk or /usr/xpg4/bin/awk .
xoops
January 14, 2010, 6:21am
10
$ echo "8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhld : 12364" | sed 's/^.*:00 //g'
/etc/opt/SUNWconn/atm/bin/atmsnmpd : -n adnhld : 12364
@xoops
yes.. but the search string should not be 00.. as you can see.. its the time.. it can change from anything 00 - 59.. that is the problem..
mac4rfree your problem will be solved if you use nawk as per anchal_khare
said earlier in his code:-
nawk -F" /" '{print "/" $NF}'
I have test it.
yes.. that worked like charm..thanks guys.. one more doubt.. my i/p was coming from ps command and combination of awk commands.. after that i pipe the output through this.. why i am not able to assign it to a variable?
a=`command`
echo $a returns a blank..
try
a=$(commands)
you need to use bash shell for above
BR