How to get the dates from a list of file names?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the dates from a list of file names?
# 1  
Old 06-29-2012
How to get the dates from a list of file names?

Hi All,

I have a .txt file with the list of filenames as given below.

Code:
 
/dev_data/dev3/ctl/20120622_Employee.txt
/dev_data/dev3/ctl/20120623_Employee.txt
/dev_data/dev3/ctl/20120624_Employee.txt

I want to read this file & write the dates alone from the filenames into a .done file.

Can anyone tell me how to get only the dates from this .txt file to the .done file delimited by comma ?

Quote:
ie cat .done file
20120622,20120623,20120624
Thanks Much
Freddie

Last edited by vbe; 06-29-2012 at 01:20 PM.. Reason: code tags! not quotes....
# 2  
Old 06-29-2012
Code:
sed 's|.*/\([0-9]\{8\}\).*|\1|' inp.txt|paste -sd',' - > inp.done


Last edited by elixir_sinari; 06-29-2012 at 01:36 PM..
# 3  
Old 06-29-2012
Code:
in=/path/to/input/file
out=/path/to/output/file

awk -F/ '
{ sub(/_.*/,"",$NF); printf "%s,", $NF }
' "$in" > "$out"

# 4  
Old 06-30-2012
not tested
Code:
perl -F"[\/_]" -lane 'push @a,$F[5];END{print join ",",@a}' input.txt

# 5  
Old 06-30-2012
Code:
sed ':i;{s:\n:,:g;s:/.*/\|_Employee.txt::g;N;bi}' infile

# 6  
Old 06-30-2012
Another way..
Code:
awk -F "[/_]" '{x= x $6 "," } END {sub(/,$/,"",x);print x}' file

Also (contains a trailing comma),
Code:
while read LINE
do
 a=${LINE%_*}
 echo -n ${a##*/},
done < file

# 7  
Old 06-30-2012
This will work is ksh or bash shell:
Code:
$ cat test.txt
/dev_data/dev3/ctl/20120622_Employee.txt
/dev_data/dev3/ctl/20120623_Employee.txt
/dev_data/dev3/ctl/20120624_Employee.txt

$ for x in `cat test.txt`;do fn=${x##/*/};echo $fn>>files.done;done

$ cat files.done
20120622_Employee.txt
20120623_Employee.txt
20120624_Employee.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove dates from file names

Hi, I'm using a shell script. I have extracted current date files to a directory1 and the date should be removed on both sides of a CSV file. FYI... I'm looking to remove the date from the file name and not inside the CSV file. Directory1 2017-07-12_gmr_tag_log_20170711.csv... (0 Replies)
Discussion started by: shivamayam
0 Replies

2. Shell Programming and Scripting

List the file names available on FTP server before selecting the required file

Below is my script code.which shows the environment name and then fetch the file from the ftp server but I am facing one issue.The script should be run in both way.We can pass the arguments with script and select the environment name then file name.Here the issue is I am not able to list the files... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

3. Linux

Get all the files from a FTP location with previous week's dates in the file names using Linux

I have a weird requirement where I have to get the files from a FTP(Lets say FTP1) location and place it on my current FTP(Lets say FTP2) location. The issue is, these are daily files (in a pattern Sales_YYYYMMDD_report.csv) and are placed every day on FTP1 and my process usually runs on Monday(eg.... (2 Replies)
Discussion started by: dhruuv369
2 Replies

4. UNIX for Dummies Questions & Answers

List Directory names which have the file

Hi All, Can any one help me to list out the directory names which contain the specified file. See for example File name : file.201307014.LKT Have the directory structure as below. /app/work/data/INDIA/file.201307014.LKT /app/work/data/AMERICA/file.201307014.KTP... (5 Replies)
Discussion started by: Balasankar
5 Replies

5. Shell Programming and Scripting

List the file names that differ

Hello, I have two directories - prev and current . They both have same multiple subdirectories and files. Now the current directory can have some updated files and some new files added that is not in prev. I want to find the list of file names that differ. I am doing this because i can not... (2 Replies)
Discussion started by: jakSun8
2 Replies

6. Shell Programming and Scripting

get file names from the list

Hi Experts, Here is my scenario: Am maintaining a file which has list of logs with complete path and file names like bleow a/b/c/Daily/file1_20111012.log d/e/f/Monthly/file1_20111001.log g/h/Daily/file1_20110120.log i/Daily/file1_20110220.log How to copy the file names frm the list... (7 Replies)
Discussion started by: laxm
7 Replies

7. Shell Programming and Scripting

List of file names

I have the following list of file names stored in $fnames, so that if I do foreach f ($fnames) echo "$f" end I will get n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run1 n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run2 n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run3... (3 Replies)
Discussion started by: kristinu
3 Replies

8. UNIX for Dummies Questions & Answers

Help with list of latest file names

Hi Gurus, I need to list only those files which are most recent and the latest log file. For example if I have the following file list: JOB001.LOG_00uv02_00006 - Jul 7 12:16 JOB001.LOG_00vi0t_00001 - Aug 4 21:58 JOB001.LOG_00vi0t_00002 - Aug 5 09:15 JOB001.LOG_00vi0t_00003 - Aug 5... (6 Replies)
Discussion started by: shash
6 Replies

9. UNIX for Dummies Questions & Answers

Get List of Unique File Names

I have a large directory of web pages. I am doing a search through the web pages using grep and would like to get a list of unique file names of search results. The following command works fine to give me a list of file names where term appears: grep -l term *.html However, since these are... (3 Replies)
Discussion started by: rjulich
3 Replies

10. UNIX for Dummies Questions & Answers

How to Pass a list of file names to ls

Hi I have a list of file names generated from a find command. The list does not show complete file information. I would like to do this: generate the list of file names pass each file name generated to ls -l command what is the best way to do this without a script? I have tried... (2 Replies)
Discussion started by: GMMike
2 Replies
Login or Register to Ask a Question