Add Date string at end of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add Date string at end of line
# 1  
Old 05-02-2014
Blade Add Date string at end of line

HOWTO Append Date String (MMYYYY) to End of Line

file

A.txt
B.txt

Output:

A_052014.txt
B_052014.txt
# 2  
Old 05-02-2014
Where does the date come from - the date when the code was run against the file or the filetime? Or some other measure.
# 3  
Old 05-02-2014
Current Month and Year
# 4  
Old 05-02-2014
Please use code tags as required by forum rules!

It does not seem to me you want the date string appended to the end-of-line but inserted in the middle, just before the "extension" part. Try
Code:
awk '{$1=$1"_"DT}1' FS="." OFS="." DT="052014" file
A_052014.txt
B_052014.txt

# 5  
Old 05-02-2014
Code:
for line in $(cat /Users/eric/Desktop/file.txt);do foo=$(date +"%m%Y");echo ${line%.txt}_$foo.txt;done

# 6  
Old 05-03-2014
Or try

Code:
$ awk 'BEGIN{ month_year = strftime("%m%Y",systime()) }{$1=$1"_"month_year}1' FS="." OFS="." file

# 7  
Old 05-03-2014
If you want to rename actual files
Code:
ls | while read line
do
mv $line echo ${line%.*}_$(date +%m%Y).${line##*.}
done

If you want to change the content of the file
Code:
awk -v DT="$(date +%m%Y)" '{$(NF-1) = ($(NF-1) "_" DT)}1' FS='.' OFS='.' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Suppress new line at end here string?

I'm trying to fully escape/quote a directory path for use in a regular expression with no characters interpreted symbolically. Printable characters get \* and white space gets "*" $ inputDirectory="/home/dir1/dir/" $ echo $( { while read -r -n1 a; do ] ]] && echo -n "\"""$a""\"" || echo -n... (7 Replies)
Discussion started by: Michael Stora
7 Replies

3. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

4. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

How to add day of week at the end of each line that shows the date?

I have a file that looks like: file1: www_blank_com 20121008153552 www_blank_com 20121008162542 www_blank_com 20121009040540 www_blank_com 20121009041542 www_blank_com 20121010113548 www_blank_com 20121011113551 www_blank_com 20121012113542 I want the new file to show the day of... (3 Replies)
Discussion started by: castrojc
3 Replies

7. UNIX for Dummies Questions & Answers

Appending Date at the end ONLY in first line of file

Hi, My requirement is to append a date in format DDMMYYYYHHMISS at the end of first line of file which is HEADER. I am trying command sed -i '1s/.*/&<date_format>/' <file_name> Where <date_format>=`date +%m%d%Y%H%M%S` I am somehow misisng the right quotes ti get this added in above... (2 Replies)
Discussion started by: sanjaydubey2006
2 Replies

8. Shell Programming and Scripting

Add string end of line

Hello How can I add a string (always the same) at the end of a specific line in a file... The file is: 000000001 041 L $$aspa 000000001 088 L $$aJ.E.N. 551 000000001 090 L $$aINFORMES JEN 000000001 100 L $$aautor 1 ---- 000000002 041 L $$aeng 000000002 088 L $$aJ.E.N. 1... (13 Replies)
Discussion started by: ldiaz2106
13 Replies

9. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

10. Shell Programming and Scripting

to put date at the end of each line

#!/bin/ksh if test -f file6.txt then rm file6.txt fi a=`date +"%F"` awk '{print $0,"$a"}' file3.txt > file6.txt ----------------------------------------------------------------- i need to append date at the end of each line in file 3 and o/p it to file6.txt (3 Replies)
Discussion started by: ali560045
3 Replies
Login or Register to Ask a Question