Link to home
Start Free TrialLog in
Avatar of lijunguo
lijunguoFlag for Australia

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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)
Ops...

I forgot to put one thing in my last command above:
==============================================================
perl -nae 'print @F[1];print "\n"' <file> | xargs kill -15
==============================================================
Avatar of lijunguo

ASKER

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