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 dealing with Linux text

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Linux text processing skills, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

As a Linux developer, guide has to face text processing scenes almost every day. Therefore, mastering text processing routines and skillfully using text processing commands is of great significance to improve work efficiency. This paper takes a practical example to introduce how to use grep, awk, sed, cut and other commands to process text, so as to quickly complete the originally tedious operation.

Background

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

$docker images

How to operate it? Is it possible to delete them one by one by hand?

$docker image rm f0fa889be9e8

Just imagine, if there are hundreds of such images to be deleted, how long will it be? But how can it be improved?

Text filtering

Consider filtering the image named none from the results, and the grep command can be done:

$docker images | grep none

Field extraction

Next, extract the container ID column, which is pediatric for awk:

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

Of course, you can get the same result by segmenting fields with the cut command. Because cut can only be segmented by a single character, and there are multiple spaces in the original text, text replacement is required first.

Text substitution

Text substitution is what the sed command is good at. Replace one or more spaces with one:

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

Text segmentation

Next, use the cut command to split the result further and fetch the third field:

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

Yes, we got the same result as using the awk command.

Batch deletion

Next, delete the images in batch 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, all unwanted images have been deleted and only need to be done once:

$docker images

The following operation is also equivalent:

$docker images | grep none | sed's / * / / g' | cut-d'- f 3 | after reading the above content in xargs docker image rm, have you mastered the skills of Linux text processing? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Servers

Wechat

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

12
Report