简体   繁体   中英

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.

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

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}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM