Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the skills of Linux in dealing with text?

Shulou Source: shulou.com Published: 2022-06-01 21:08:20 11月13日 Update

This article mainly explains "what are the skills of Linux text processing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "What are the skills of Linux processing text"!

background

I have a lot of Docker images on my development machine, and now I need to delete those named none:

$ docker images

How does it work? Could it be that he deleted them manually one by one?

$ docker image rm f0fa889be9e8

Imagine if there were hundreds of such images to be deleted, how many years and months would it take! But how?

text filtering

Consider filtering out mirrors named none from the results first, which grep can do:

$ docker images | grep none

field extraction

Next, extract the container ID column, which is child's play for awk:

$ docker images | grep none | awk '{print $3}'

Of course, you can get the same result by cutting the field with the cut command. Because cut can only be split by a single character, and the original text has multiple spaces, text replacement is required first.

text replacement

Text substitution is something sed is good at. Here's how to replace one or more spaces with one:

$ docker images | grep none | sed 's/ */ /g'

Text segmentation

Next, use the cut command to slice the result further and extract the third field:

$ docker images | grep none | sed 's/ */ /g' | cut -d ' ' -f 3

Yes, we get the same results as using awk.

batch delete

Next, remove mirrors in bulk via xargs:

$ docker images | grep none | awk '{print $3}' | xargs docker image rm

In this example, xargs will eventually execute the following command:

$ docker image rm f0fa889be9e8 257954316455 99739acbfe7a 52b10754a70c 86878eefdd39

See no, all unwanted mirrors are removed clean, and only one operation is required:

$ docker images

The following operation is also equivalent:

$ docker images | grep none | sed 's/ */ /g' | cut -d ' ' -f 3 |xargs docker image rm At this point, I believe that everyone has a deeper understanding of "what are the skills of Linux text processing", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Text commands mirroring results techniques processing fields same next content multiple spaces learning good practical clean deeper three examples interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Apple Shulou Technology OPPO Reno