Extract date from files based on file pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract date from files based on file pattern
# 1  
Old 07-25-2016
Extract date from files based on file pattern

I want to extract dates from the files and i have different types of files with pattern. I have list file with the patterns and want to date extract based on it in a sh script

Files in the directory :

Code:
file1_20160101.txt
file2_20160101_abc.txt
filexyz20160101.txt


list file with patterns:

pattern.lst
-----------
Code:
file1_*.txt
file2_*_abc.txt
filexyz*.txt

Basically I want to read the pattern.lst file, read the each line, find the file in the directory and print the file name and its date separately.
Can anyone please help me here ?

Thanks

Last edited by lijjumathew; 07-25-2016 at 06:33 PM..
# 2  
Old 07-26-2016
This may help you

Code:
[akshay@localhost tmp]$ cat f
file1_20160101.txt
file2_20160101_abc.txt
filexyz20160101.txt
file2_20160101_dabc.txt
filexyz20160101.txt
file2_20160101_xabc.txt
filexyz20160101d.txt

[akshay@localhost tmp]$ while read s ; do touch $s; done < f

[akshay@localhost tmp]$ ls file*.txt -1
file1_20160101.txt
file2_20160101_abc.txt
file2_20160101_dabc.txt
file2_20160101_xabc.txt
filexyz20160101d.txt
filexyz20160101.txt

[akshay@localhost tmp]$ cat pattern.lst 
file1_*.txt
file2_*_abc.txt
filexyz*.txt

[akshay@localhost tmp]$ cat test.sh 
#!/usr/bin/env bash

while read pattern; do
   ls $pattern -1 | perl -lne '/([0-9]{4}[0-9]{2}[0-9]{2})/ and print $_," => ",$1'      
done < pattern.lst

[akshay@localhost tmp]$ bash test.sh 
file1_20160101.txt => 20160101
file2_20160101_abc.txt => 20160101
filexyz20160101d.txt => 20160101
filexyz20160101.txt => 20160101

# 3  
Old 07-26-2016
You didn't specify, whether you need to use a particular shell, or a free to choose. For Zsh, you would get a list of the files/directories matching your pattern simply by

Code:
echo ${~$(<pattern.lst)}

However, it is not clear what you mean by "extracting the date". For example, if you have the file

Code:
file1_11470505_20160101_12121212.txt

it would certainly match the pattern file1_*.txt, but which date would you like to get? Syntactically, 11470505, 20160101 and 12121212 are all valid dates.

Last edited by rovf; 07-26-2016 at 03:01 AM.. Reason: Adding code tag
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract some characters from lines based on pattern

Hi All, i would like to get some help regarding extracting certain characters from a line grepped. blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah... (10 Replies)
Discussion started by: mad man
10 Replies

2. UNIX for Beginners Questions & Answers

Extract file name based on the pattern

Hello All, I have multiple files in a hadoop /tmp/cloudera directory. Filename are as follows ABC_DATA_BAD5A_RO_F_20161104.CSV ABC_DATA_BAD6C_VR_F_20161202.CSV ABC_DATA_BAD7A_TR_F_20162104.CSV ABC_DATA_BAD2A_BR_F_20161803.CSV ABC_DATA_BAD3T_KT_F_20160106.CSV I just need filenames... (6 Replies)
Discussion started by: prajaktaraut
6 Replies

3. UNIX for Advanced & Expert Users

Concatenation of multiple files based on file pattern

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

4. UNIX for Dummies Questions & Answers

Split a huge 7 GB File Based on Pattern into 4 files

Hi, I have a Huge 7 GB file which has around 1 million records, i want to split this file into 4 files to contain around 250k messages each. Please help me as Split command cannot work here as it might miss tags.. Format of the file is as below <!--###### ###### START-->... (6 Replies)
Discussion started by: KishM
6 Replies

5. Shell Programming and Scripting

Help needed - Split large file into smaller files based on pattern match

Help needed urgently please. I have a large file - a few hundred thousand lines. Sample CP START ACCOUNT 1234556 name 1 CP END ACCOUNT CP START ACCOUNT 2224444 name 1 CP END ACCOUNT CP START ACCOUNT 333344444 name 1 CP END ACCOUNT I need to split this file each time "CP START... (7 Replies)
Discussion started by: frustrated1
7 Replies

6. Shell Programming and Scripting

split XML file into multiple files based on pattern

Hello, I am using awk to split a file into multiple files using command: nawk '{ if ( $1 == "<process" ) { n=split($2, arr, "\""); file=arr } print > file }' processes.xml <process name="Process1.process"> ... (3 Replies)
Discussion started by: chiru_h
3 Replies

7. Shell Programming and Scripting

Splitting large file into multiple files in unix based on pattern

I need to write a shell script for below scenario My input file has data in format: qwerty0101TWE 12345 01022005 01022005 datainala alanfernanded 26 qwerty0101mXZ 12349 01022005 06022008 datainalb johngalilo 28 qwerty0101TWE 12342 01022005 07022009 datainalc hitalbert 43 qwerty0101CFG 12345... (19 Replies)
Discussion started by: jimmy12
19 Replies

8. Shell Programming and Scripting

Split a file into multiple files based on the input pattern

I have a file with lines something like. ...... 123_start ...... ....... 123_end .... ..... 456_start ...... ..... 456_end .... ..... 789_start .... .... 789_end (6 Replies)
Discussion started by: abinash
6 Replies

9. Shell Programming and Scripting

How to extract log data based on date

Hi Gurus, I've been having some problem in extracting the log data based on the current date and month. As shown in the sample data below, how to extract the log info for Aug 11? Sample data: root pts/ta userpc Wed Aug 11 09:46 - 20:21 (10:35) root pts/ta userpc... (13 Replies)
Discussion started by: superHonda123
13 Replies

10. Shell Programming and Scripting

extract based on pattern

I have a mail log file and I want to extract some lines belonging to one domain. For example Input File: Dec 12 03:15:28 postfix/smtpd: 3F481EB0295: client=unknown, sasl_method=PLAIN, sasl_username=abcd@xyz.com Dec 12 03:22:08 postfix/smtpd: 60B56EE001D: client=5ad9b9ba.com,... (7 Replies)
Discussion started by: Bijayant Kumar
7 Replies
Login or Register to Ask a Question