Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform multiple line into one line in bash stdout?

Tags:

bash

shell

I sometimes do this in my shell :

sam@sam-laptop:~/shell$ ps aux | grep firefox | awk '{print $2}'
2681
2685
2689
4645

$ kill -9 2681 2685 2689 4645

Is there a way I can transform the multiple lines containing the PIDs into one line separated by spaces ? (It's a little bit annoying to type the PIDs every time and I really would like to learn :) )

Thanks a lot.

like image 264
Zenet Avatar asked Feb 08 '10 19:02

Zenet


People also ask

What does %% mean in bash?

The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching. Follow this answer to receive notifications.

How do I echo multiple lines in bash?

To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.


1 Answers

The easy way for this is using xargs

ps aux | grep firefox | awk '{print $2}' | xargs kill -9

This will invoke the kill command with all pids at the same time. (Exactly what you want)

like image 146
Peter Smit Avatar answered Oct 16 '22 18:10

Peter Smit