简体   繁体   中英

Linux grep command with complex delimiter

I have some files inside a directory:

140221_alzproddbadm01.log
140221_alzproddbadm02.log
140221_alzproddbadm03.log
132123_ehxsk01.log
try.sh
logs
expan
140221_alzproddbadm04.log
23891_ehxsk02.log

I only need to grep these:

140221_alzproddbadm04.log
23891_ehxsk02.log
140221_alzproddbadm01.log
140221_alzproddbadm02.log
140221_alzproddbadm03.log
132123_ehxsk01.log

format is not complicated ( numbers + _ + bla bla bla ) but i don't know how to write right regex for my problem.

ls -l | grep root | cut -d " " -f9 | grep #what to write next?

You can do it like this:

$ grep -E '[0-9]*_[[:alpha:]]*.log' file
140221_alzproddbadm01.log
140221_alzproddbadm02.log
140221_alzproddbadm03.log
132123_ehxsk01.log
140221_alzproddbadm04.log
23891_ehxsk02.log

Or even

ls -l [0-9]*_[a-z]*.log | grep root | ...

Why not just wildcard the files appropriately ? eg

$ grep pattern *.log

so the shell itself will simply pass anything ending'.log' to grep . Note that this isn't to do with grep specifically but a shell globbing function and thus applicable to any command.

If you have a specific list of files ( /tmp/listOfFiles.txt ) then perhaps:

    #!/bin/bash
    while IFS= read -r file
    do
           grep pattern "$file"
    done < /tmp/listOfFiles.txt

See here for more info.

只需使用find命令:

find /path/to/dir -name "*\.log"

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