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
# 8  
Old 05-03-2014
A simple sed solution:
Code:
sed 's/\.txt$/_'$(date +%m%Y)'&/'

Regards,
Alister

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

Quote:
Originally Posted by protocomm
Code:
for line in $(cat /Users/eric/Desktop/file.txt);do foo=$(date +"%m%Y");echo ${line%.txt}_$foo.txt;done

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:
Code:
while IFS= read -r line; do
    ....
done < file.txt

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 9  
Old 05-03-2014
Quote:
Originally Posted by alister
A simple sed solution:
Code:
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:
Code:
while IFS= read -r line; do
    ....
done < file.txt

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

Last edited by protocomm; 05-03-2014 at 05:53 PM..
# 10  
Old 05-03-2014
Quote:
Originally Posted by protocomm
Don't understand, can you give me an example when this command line doesn't work ? if i double quote the command subshell cat, i can read file with spaces ?
If the following script is problems.sh:
Code:
 
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:
Code:
$ 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

Last edited by alister; 05-03-2014 at 06:03 PM..
This User Gave Thanks to alister For This Post:
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