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

Where does the date come from - the date when the code was run against the file or the filetime? Or some other measure.

Current Month and Year

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

awk '{$1=$1"_"DT}1' FS="." OFS="." DT="052014" file
A_052014.txt
B_052014.txt
for line in $(cat /Users/eric/Desktop/file.txt);do foo=$(date +"%m%Y");echo ${line%.txt}_$foo.txt;done

Or try

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

If you want to rename actual files

ls | while read line
do
mv $line echo ${line%.*}_$(date +%m%Y).${line##*.}
done

If you want to change the content of the file

awk -v DT="$(date +%m%Y)" '{$(NF-1) = ($(NF-1) "_" DT)}1' FS='.' OFS='.' file

A simple sed solution:

sed 's/\.txt$/_'$(date +%m%Y)'&/'

Regards,
Alister

---------- Post updated at 12:31 PM ---------- Previous update was at 12:19 PM ----------

That's an extremely fragile and unreliable method for reading a file.

After the cat command substitution runs, its output undergoes field splitting. With the default IFS value, the distinction between single instances and multiple consecutive sequences of whitespace and blank lines will be lost.

After field splitting, pathname expansion is performed. If there are any pathname pattern matching metacharacters in the file, unintended globbing can occur. Normally these characters are just *, ?, and [, but if extended globbing is enabled, there are others. If nullglobbing is enabled, words can be lost.

You cannot defend against these expansions because any quoting places the entire file's contents in the variable at once, yielding at most a single iteration.

You should never use that approach ... ever. A safe way to read a file's lines into a variable:

while IFS= read -r line; do
    ....
done < file.txt

Regards,
Alister

1 Like

Yes it's true, i have understood my beginner error, i wrote this command line so quickly�.thx

If the following script is problems.sh:

 
mkdir newdir && cd newdir

cat <<EOF > file.txt
*.txt


a   b   .txt
EOF

echo BEGIN CORRECT SOLUTION
sed 's/\.txt$/_'$(date +%m%Y)'&/' file.txt

echo
echo BEGIN INCORRECT SOLUTION
for line in $(cat file.txt); do
	foo=$(date +"%m%Y")
	echo ${line%.txt}_$foo.txt
done

echo
echo BEGIN INCORRECT QUOTED SOLUTION
for line in "$(cat file.txt)"; do
        foo=$(date +"%m%Y")
        echo ${line%.txt}_$foo.txt
done

Its output:

$ sh problems.sh 
BEGIN CORRECT SOLUTION
*_052014.txt


a   b   _052014.txt

BEGIN INCORRECT SOLUTION
file_052014.txt
a_052014.txt
b_052014.txt
_052014.txt

BEGIN INCORRECT QUOTED SOLUTION
file.txt a b _052014.txt

Note how your code expands the *.txt into file.txt. Note also how the blank lines are lost. And finally, obeserve how it splits the single line "a b _052014.txt" into three lines.

The quoted version fails because jams the entire file into the variable and the loop only iterates once. It also needs to quote the $line in the echo statement (this is the reason that *.txt still becomes file.txt).

Regards,
Alister

1 Like