user3577756
user3577756

Reputation: 643

How to remove all instances of a string from output in linux terminal commands

This command almost does everything I am needing

cat /etc/passwd | cut -f6- -d : | sort | uniq 

but I can't figure out how to sort out a specific string

I need to remove "/remove/this" from any line where it occurs and I need the lines of text not to be broken up for 1 word per line.

The purpose of the command is List all of the shells used by at least one user on the system, without duplicates.

Wold appreciate any input.

Upvotes: 2

Views: 10897

Answers (2)

Håken Lid
Håken Lid

Reputation: 23064

You can use sed. When the pattern you are looking for contains /, you should use a different separator character. I find : quite readable.

cat /etc/passwd | cut -f6- -d : | sort | uniq | sed s:/remove/this::

Since sed will change some lines, it's better to move sort + uniq to the end of the pipeline. sort -u combines both programs.

cat /etc/passwd | cut -f6- -d : | sed s:/remove/this:: | sort -u

Upvotes: 3

hek2mgl
hek2mgl

Reputation: 157947

You can use a single awk command for that:

awk -F: '{sub(/\/remove\/this/, "", $NF);a[$NF]} END{for(i in a){print i}}' /etc/passwd

-F: splits the line by :.

The first block runs on every line of input while sub(/\/remove\/this/, "", $NF) removes the string from the last column and a[$NF] creates and index for every shell found in passwd. Obviously if the index already exists, it will not get created again. Doing so we dedup the data.

At the END of input we are iterating trough the array a and print every index: {for(i in a){print i}}

Upvotes: 1

Related Questions