Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete the second word of every line of top(1) output?

Tags:

unix

sed

I have a formatted list of processes (top output) and I'd like to remove unnecessary information. How can I remove for example the second word+whitespace of each line.

Example:

1 a hello
2 b hi
3 c ahoi

Id like to delete a b and c.

like image 340
Andreas Hartmann Avatar asked Dec 06 '12 10:12

Andreas Hartmann


2 Answers

You can use cut command.

 cut -d' ' -f2 --complement file

--complement does the inverse. i.e. with -f2 second field was choosen. And with --complement if prints all fields except the second. This is useful when you have variable number of fields.

GNU's cut has the option --complement. In case, --complement is not available then, the following does the same:

cut -d' ' -f1,3- file

Meaning: print first field and then print from 3rd to the end i.e. Excludes second field and prints the rest. Edit:

If you prefer awk you can do: awk {$2=""; print $0}' file

This sets the second to empty and prints the whole line (one-by-one).

like image 189
P.P Avatar answered Sep 19 '22 18:09

P.P


Using sed to substitute the second column:

sed -r 's/(\w+\s+)\w+\s+(.*)/\1\2/' file 
1 hello
2 hi
3 ahoi

Explanation:

(\w+\s+) # Capture the first word and trailing whitespace
\w+\s+   # Match the second word and trailing whitespace
(.*)     # Capture everything else on the line

\1\2     # Replace with the captured groups 

Notes: Use the -i option to save the results back to the file, -r is for extended regular expressions, check the man as it could be -E depending on implementation.

Or use awk to only print the specified columns:

$ awk '{print $1, $3}' file
1 hello
2 hi
3 ahoi

Both solutions have there merits, the awk solution is nice for a small fixed number of columns but you need to use a temp file to store the changes awk '{print $1, $3}' file > tmp; mv tmp file where as the sed solution is more flexible as columns aren't an issue and the -i option does the edit in place.

like image 42
Chris Seymour Avatar answered Sep 17 '22 18:09

Chris Seymour