1

I have 1000's of files .txt files in a folder:

The ls output of the folder looks like:

2-20160329050200862185.instan-methodstack_2016-03-06-23.55.05.436-+0000_2016-09-07-05.31.47.105-+0000.txt
2-20160329050200862185.instan-methodstack_2016-03-06-23.55.05.436-+0000_2016-09-07-05.27.47.000-+0000.txt
2-20160329050200862185.instan-methodstack_2016-03-06-23.55.05.436-+0000_2016-09-07-05.25.46.891-+0000.txt
2-20160329050200862185.instan-methodstack_2016-03-06-23.55.05.436-+0000_2016-09-07-05.23.46.788-+0000.txt
2-20160329050200862185.instan-methodstack_2016-04-27-07.02.41.320-+0000_2016-07-27-07.07.21.784-+0000.txt
2-20160329050200862185.instan-methodstack_2016-04-27-07.02.41.320-+0000_2016-07-27-07.05.22.541-+0000.txt
2-20160329050200862185.instan-methodstack_2016-04-27-07.02.41.320-+0000_2016-07-27-07.02.41.320-+0000.txt
2-20160329050200862185.instan-methodstack_2016-04-27-06.22.49.534-+0000_2016-07-27-07.01.32.824-+0000.txt

Now, I want to list all files that have date value less than an input date

For eg:

My Input date is: 2016-08-11

I want the result:

2-20160329050200862185.instan-methodstack_2016-04-27-07.02.41.320-+0000_2016-07-27-07.07.21.784-+0000.txt
2-20160329050200862185.instan-methodstack_2016-04-27-07.02.41.320-+0000_2016-07-27-07.05.22.541-+0000.txt
2-20160329050200862185.instan-methodstack_2016-04-27-07.02.41.320-+0000_2016-07-27-07.02.41.320-+0000.txt
2-20160329050200862185.instan-methodstack_2016-04-27-06.22.49.534-+0000_2016-07-27-07.01.32.824-+0000.txt

The comparison date value has to check after the string starting with +0000_

I tried multiple commands, but nothing worked.

ls | awk ' { i = substr($0, 73, 10); cond = "2015-09-07"; if expr "$i" "<" "$cond" > /dev/null; then print($0); fi }'
ls | awk ' { i = substr($0, 73, 10); if ( (date -d i +"%Y%m%d") -lt 20160907 ) print($0) endif; print("\n") }'

Please help.

3
2

Assuming parsing ls is not error prone in this case:

ls | awk '{ i = substr($0, 73, 10); if(i < "2016-08-11") print }'

or the equivalent

ls | awk '{ i = substr($0, 73, 10) } i < "2016-08-11" '
  • i = substr($0, 73, 10) saves the extracted date to variable i
  • i < "2016-08-11" if this condition is true, print input line

Since the date is in YYYY-MM-DD format, a simple string comparison will work without need of any conversion

0
0

you can try this command

you can give the date input as yyymmdd format in awk command

ls *.txt > all_files.txt
awk -F_ -vin_date="20160427" '{split($2,a,"-");date=a[1]a[2]a[3];if(date+0<in_date)print}' all_files.txt
5
  • For me, not working
    – fortune
    Feb 17 '17 at 9:03
  • when you say.. not working.. you have to provide more details.. is any error ? is all_files.txt contains only the filenames?
    – Kamaraj
    Feb 17 '17 at 9:29
  • yes there is only filenames. How do I execute the code in terminal?
    – fortune
    Feb 17 '17 at 10:26
  • just copy paste the code in terminal. make sure you change the date in_date
    – Kamaraj
    Feb 17 '17 at 10:34
  • No output is showing. Note that, I need to compare the date from the string after a match +0000_ in the filename. Tnx
    – fortune
    Feb 17 '17 at 10:56
0

Combining match and substr in awk giving me the desired result:

ls | awk 'match($0,"\+0000_"){i = substr($0,RSTART+6,10)} i < "2016-08-11" '

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.