Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

read input file line by line and get the number from second column

I have a output file like the following format.


david     9428     1  0 May12 ?        00:00:00 udt PHANTOM TEST1
david     9429     1  0 May12 ?        00:00:00 udt PHANTOM TEST1
david     9401     1  0 May12 ?        00:00:00 udt PHANTOM TEST1
david     9404     1  0 May12 ?        00:00:00 udt PHANTOM TEST1
david     9408     1  0 May12 ?        00:00:00 udt PHANTOM TEST1

Read the file as an input, then what I want to do is to get all of the second column number, which are process ids, and kill the process.


I'm running Linux 6.0 and kernel is 2.4. Please give a sample for bash script. Thanks in advance!
0
lijunguo
Asked:
lijunguo
3 Solutions
 
sunnycoderCommented:
Hi lijunguo,

kill `awk ' { print $2 } ' filename `

Cheers!
Sunnycoder
0
 
ravenplCommented:
apart from the above, You may be intrested with alternatives
kill -0 `pgrep udt` # change 0 to real signa
killall udt
pkill -u david
0
 
root_startCommented:
Hi lijunguo,

There are several ways you can do this, as some suggested above and I am giving another way:
==============================================================
cut -f6 -d" " <file> | xargs kill -15
==============================================================

I suggested above the "cut" command because it is faster than "awk", also, I suggested "kill -15" because the "15" lets the process to finish and not kills it abort.

The command above will report if the process you are trying to kill does not exist but if you don't want this message, just:
==============================================================
cut -f6 -d" " txt | xargs kill -15 2>> /dev/null
==============================================================

I hope it helps. =0)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TintinCommented:
rootstart

Using cut is a very bad idea.  If the user's name was davidr (or any other name with less or greater than five characters) then the cut would fail.  As for the speed difference between awk and cut, the difference if any is so negligible that you would never notice it.

I ran the cut version and awk version on my server about 5 times each and everytime it go that same times, ie:

real    0m0.003s
user    0m0.000s
sys     0m0.000s

Also note that the default signal for kill is SIGTERM (15) so doing

kill -15 pid

and

kill pid

is that same.
0
 
root_startCommented:
Hi Tintin,

Thanks for the check you sent me, the "cut" command depends on the length of username and I didn't know that.

In my opinion, if you have more that one user that you want to kill a process, you can use Tintin's command:
============================================================
kill `awk ' { print $2 } ' filename `
============================================================

But if the processes have the same username, you can use:
==============================================================
cut -f6 -d" " txt | xargs kill -15 2>> /dev/null
==============================================================

Sorry about my half correct answer.
0
 
root_startCommented:
Hi Again lijunguo,

As I told you in my first comment, there are several ways to do what you need.

Check the following:
==============================================================
perl -nae 'print @F[1];print "\n"' | xargs kill -15
==============================================================

Again, I hope it helps. =0)
0
 
root_startCommented:
Ops...

I forgot to put one thing in my last command above:
==============================================================
perl -nae 'print @F[1];print "\n"' <file> | xargs kill -15
==============================================================
0
 
lijunguoAuthor Commented:
Hi Guys,
I really appreciate your quick help. But "sunnycoder" provides an easy and simple solution, and works perfect. And it's good to know some alternatives.

Regards,
Lijunguo
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now