简体   繁体   中英

remove lines from file that does not have dot extension in bash

I am having such of file that contains lines as below:

/folder/share/folder1
/folder/share/folder1/file.gz
/folder/share/folder2/11072012
/folder/share/folder2/11072012/file1.rar

I am trying to remove these lines:

/folder/share/folder1/
/folder/share/folder2/11072012

To get a final result the following:

/folder/share/folder2/11072012/file1.rar
/folder/share/folder1/file.gz

In other words, I am trying to keep only the path for files and not directories.

This

awk -F/ '$NF~/\./{print}'
  • splits input records on the character "/" using the command line switch -F
  • examines the last field of the input record $NF (where NF is the number of fields in the input record) to see if it DOES contain the character "." (the !~ operator)
  • if it matches, oputput the record.

Example

$ echo -e '/folder/share/folder.2/11072012
/folder/share/folder2/11072012/file1.rar' | mawk -F/ '$NF~/\./{print}'
/folder/share/folder2/11072012/file1.rar
$

NB: my microscript looks at . ONLY in the filename part of the full path.

Edit in my 1st post I reversed the logic, to print dotless files instead of dotted ones.

您可以使用find命令仅获取文件列表

find <directory> -type f

With awk:

awk -F/ '$NF ~ /\./{print}' File

Set / as delimiter, check if last field ( $NF ) has . in it, if yes, print the line.

Text only result

sed -n 'H
$ {g
:cycle
   s/\(\(\n\).*\)\(\(\2.*\)\{0,1\}\)\1/\3\1/g
   t cycle
   s/^\n//p
   }' YourFile
  • Based on file name and folder name assuming that:

    • line that are inside other line are folder and uniq are file (could be completed by a OS file existence file on result)
    • line are sorted (at least between folder and file inside)
  • posix version so --posix on GNU sed

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