Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the skills of Linux in dealing with text?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report