how to remove lines from file that don't match regex?

China☆狼群 提交于 2020-01-11 12:44:06

问题


I have a big file that looks like this:

7f0c41d6-f9c6-47aa-a034-d40bc629c973.csv
159890
159891
24faaed6-62ee-4175-8430-5d73b09911c8.csv
159907
5bad221f-25ef-44fa-9086-fd152e697928.csv
642e4ac3-3d46-4b4c-b5c8-aa2fa54d0b04.csv
d0e145a5-ceb8-4d4b-ae47-11e0c9a6548d.csv
159929
ba678cbd-af57-493b-a69e-e7504b4bc328.csv
7750840f-9bf9-4a68-9f25-a2ba0968d481.csv
159955
159959

And I'm only interesting in *.csv files, can someone point me how to remove files that do not end with .csv.

Thank you.


回答1:


grep "\.csv$" file 

will pull out only those lines ending in .csv

Then if you want to put them in a different file;

grep "\.csv$" file > newfile



回答2:


sed is your friend:

sed -i.bak '/\.csv$/!d' file

-i.bak : in-place edit. creates backup file with .bak extension




回答3:


 ([0-9a-zA-Z-]*.csv$)

This is the regex code that only select the filename ending with .csv extensions.

Hope this will help you.




回答4:


If you are familiar with the vim text editor (vim or vi is typically installed on many linux boxes), use the following vim Ex mode command to remove lines that don't match a particular pattern:

:v/<pattern>/d

For example, if I wanted to delete all lines that didn't contain "column" I would run:

:v/"column"/d

Hope this helps.




回答5:


If it is the case that you do not want to have to save the names of files in another file just to remove unwanted files, then this may also be an added solution for your needs (understanding that this is an old question).

This single line for loop using the grep "\.csv" file solution recursively so you don't need to manage multiple files names being saved here or there.

for f in *; do if [ ! "$(echo ${f} | grep -Eo '.csv')" == ".csv" ]; then rm "${f}"; fi; done

As a visual aid to show you that it works as intended (for removing all files except csv files) here is a quick and dirty screenshot showing the results using your sample output.

And here is a slightly shorter version of the single line command:

for f in *; do if [ ! "$(echo ${f} | grep -o '.csv')" ]; then rm "${f}"; fi; done

And here is it's sample output using your sample's csv file names and some randomly generated text files.

The purpose for using such a loop with a conditional is to guarantee you only rid yourself of the files you want gone (the non-csv files) and only in the current working directory without parsing the ls command.

Hopefully this helps you and anyone else that is looking for a similar solution.



来源:https://stackoverflow.com/questions/19057125/how-to-remove-lines-from-file-that-dont-match-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!