Ragnarrok56
Ragnarrok56

Reputation: 1

How do i convert the contents of a .txt file in linux from vertical to horizontal?

I have a bash file, do-something.sh

#!/bin/bash
ps -efa > /command.txt
sed -i 's/root     //g' command.txt
sed -i '/UID/d' command.txt
sed -i -E 's/.{26}:00:.*//g' command.txt
sed -i '1s/^/kill -9 /' command.txt
sed -e :a -e '{N; s/\n/ /g;ta}' command.txt

It currently lists process ids in command.txt in a vertical format, i'd like them to be listed horizontally so i could run the command using "./do-something" and have all processes be killed.

Upvotes: 0

Views: 181

Answers (2)

Ray Law
Ray Law

Reputation: 21

You can construct the kill command with all PID at the end in one line with this command:

ps -efa | awk 'BEGIN{printf "kill -9 "}{printf "%s ",$2}'

Upvotes: 1

Timur Shtatland
Timur Shtatland

Reputation: 12395

Simply cat the file with pids to xargs | kill -9 like so:

cat pids.txt | xargs kill -9

Example:

echo 12345 67890 | xargs kill -9

Upvotes: 1

Related Questions