Extract Date from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract Date from file
# 1  
Old 09-29-2007
Extract Date from file

I am new to unix scripting. we are using bash. My task is I have dir which contains 30 files.

The first column in each file is Date field. For all the files I need to extract
the date part Ex(2007-09-05) from the file and add that at the end of that file.

for example:

The file names are like test.txt,test1.txt,test2.txt

it need to be changed as test_2007-09-05.txt etc.

test.txt

Date Source Location Point count
2007-09-05 15:31:46 Phone Dallas 100 864
2007-09-05 15:31:46 Email Dallas 1000 514



test1.txt
Date Source Location Point count
2007-09-05 15:31:46 Phone Atlanta 100 864
2007-09-05 15:31:46 Email Atlanta 1000 514



test2.txt

Date Source Location Point count
2007-09-05 15:31:46 Phone Arizona 100 864
2007-09-05 15:31:46 Email Arizona 1000 514



Can you please paste any sample script.

Thanks for help,

mag
# 2  
Old 09-29-2007
With GNU grep:

Code:
for f in *.txt;do 
	mv "$f" "${f%.txt}_$(egrep -om1 '^[0-9]{4}-[0-9]{2}-[0-9]{2}' "$f").txt"
done

Without GNU grep:

Code:
for f in *.txt;do 
	mv "$f" "${f%.txt}_$({ read< <(egrep '^[0-9]{4}-[0-9]{2}-[0-9]{2}' "$f");echo ${REPLY%% *};}).txt"
done

# 3  
Old 09-30-2007
Code:
for each in *.txt
do
mv $each ${each}_$(awk -F" " 'NR==2 { print $1}' $each )
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 : file1_20160101.txt file2_20160101_abc.txt filexyz20160101.txt list file with... (2 Replies)
Discussion started by: lijjumathew
2 Replies

2. UNIX for Dummies Questions & Answers

How to extract date from a file?

Hello Gurus, I have one file from which I want to extract only dates. The below is the file format : ========================= Process ID 16842770 Log Read Checkpoint Oracle Integrated Redo Logs 2014-07-04 17:06:11 <=== SCN 1779.852353022 (7641599172606) Process ID ... (6 Replies)
Discussion started by: pokhraj_d
6 Replies

3. UNIX for Dummies Questions & Answers

Extract date ranged data from log file

Hi, I am trying to extract lines of data within a log file on a Redhat 5 Linux system. eg I need all the lines with a particular username over the last 3 minutes. the log file may read like this, and I want a way to search all the lines extracting all the relevant lines over the last 3... (2 Replies)
Discussion started by: mantis100
2 Replies

4. Shell Programming and Scripting

Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server 20102009 is the date of the file ddmmaaaa format the complete route is /dprod/informatica/Fuentes/CRED20102009.csv i want to extract the date to create a new file named Parameters.txt I need to create Parameters.txt with this... (6 Replies)
Discussion started by: angel1001
6 Replies

5. Shell Programming and Scripting

To extract date and time separately from the file name

Hi., My current script extracts only if the date and time are in 3rd and 4th pos. #!/bin/bash echo "Enter the file name to extract the timestamp" read fname IFILE=$fname F=$IFILE IFS="_." f=($F) echo "Date ${f} Time ${f}" How to generalize the script to extract the... (3 Replies)
Discussion started by: IND123
3 Replies

6. Shell Programming and Scripting

Extract data from log file from or after the specific date

Hi , I am having a script which will start a process and appends the process related logs to a log file. The log file writes logs with every line starting with date in the format of: date +"%Y %b %d %H:%M:%S". So, in the script, before I start the process, I am storing the date as DATE=`date +"%Y... (5 Replies)
Discussion started by: chiru_h
5 Replies

7. Shell Programming and Scripting

Extract date from log file

Hello All, I just need to extract the date portion from a apache log file I am able to do it using the chain of command - Logfile contents - First record - ========================== 197.130.211.240 - - "GET /jp/index.shtml HTTP/1.1" 200 24255... (4 Replies)
Discussion started by: jambesh
4 Replies

8. Shell Programming and Scripting

Extract Expiry date and server_name from the following file

Hi Guys, I am trying to write one ksh script where I have to go to lots of unix boxes and read one text file which will have data like this though this is not complete data. And I just have to extract expire date ( which is 2nd date value 08/31/2009 in the first one ) and the server name ( astro... (0 Replies)
Discussion started by: pareshan
0 Replies

9. Shell Programming and Scripting

How to extract date with time from file

Hi, I have a file where there is a date and time field, the format for it is yyyy-mm-dd hours:mins:sec the position of date field may vary anywhere in the line and it might be different and it is specified along with the variable AppTimeStamp how do i extract date and time both from the... (5 Replies)
Discussion started by: prash_b
5 Replies

10. Shell Programming and Scripting

extract date portion from file

Hi, I have a file where there is a date field (single line variable length file) how to extract just the date portion from it the position of date field may vary anywhere in the line but will always have the format mm-dd-yyyy for eg . xxxxxxxxxxxxxxx09-10-2006xxxxxxxxxxxxxxxxxxxx (5 Replies)
Discussion started by: misenkiser
5 Replies
Login or Register to Ask a Question