Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

regex - perl one liner + how to filter files

background: my target is to filter the files that contain the word - old

I want to print all files expect the files that contain the old word (capital or small letters ) ,

according to the following rules:

if Az-zZ is before the old name or after the old name then line should be printed

if Az-Zz is after the old name and before old word then line should be printed

if 0-9 is after the old name and before old word then line should be printed

if 0-9 is before the old name and after old is non Az-zZ or 0-9 then line should not printed

if 0-9 is after the old name and before old is non Az-zZ or 0-9 then line should not printed

examples

  /DIR3/DATA/A4/Via/OOld/TriR.txt            --> should  be print
  /DIR4/DATA/A4/Via/AOld1/Comne.txt          --> should be print
  /DIR5/DATA/A4/Via/BOld/TriR.txt     --> should be print
  /DIR5/DATA/A4/Via/aOld/TriR.txt      --> should be print
  /DIR5/DATA/A4/Via/1OldA/TriR.txt      --> should be print
  /DIR5/DATA/A4/Via/POld1/TriR.txt      --> should be print
  /DIR4/DATA/A4/Via/1Old1/Comne.txt    --> should  be print
  /DIR4/DATA/A4/Via/1Old1/Comne.txt    --> should  be print
  /DIR4/DATA/A4/Via/Comne.txt    --> should  be print


  /DIR1/DATA/A4/Via/5Old/CentalS.txt   --> should not be print
  /DIR4/DATA/A4/Via/Old1/Comne.txt    --> should not be print
  /DIR1/DATA/A4/Via/Old/CentalS.txt   --> should not be print
  /DIR4/DATA/A4/Via/Old11/Comne.txt    --> should not be print
  /DIR4/DATA/A4/Via/OLD@/Comne.txt    --> should not be print
  /DIR4/DATA/A4/Via/.OLd/Comne.txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne.Old_txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne.old_txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne.0old_txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne.old6_txt    --> should not be print
  /DIR4/DATA/A4/Via/home/Comne___0old_txt    --> should not be print

Please advice how to implement this by perl one liner line syntax

 echo $PATH | perl one liner line
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If the file paths are in a single file, then you can write

perl -ne'$w='old';print if /[a-z]$w|$w[a-z]|[0-9]$w[0-9]/i' myfile

or, if you prefer

perl -ne'print if /(.)old(.)/i && "$1$2" =~ /[a-z]|[0-9]{2}/i' myfile

Actually, it looks like you want to filter out members of the PATH environment variable, so you would want

perl -E'say for grep /(.)old(.)/i && "$1$2" =~ /[a-z]|[0-9]{2}/i, split /:/, $ENV{PATH}'

Update

I misunderstood your question. This should work for you

perl -lnE'/(.)old(.)/i && "$1$2" !~ /[a-z]|[0-9]{2}/i or say for /[^:]+/g'

I don't understand your requirement to both pipe PATH to it and specify a given path, so this works with either. Also you can put an input file after the command


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...