Need Help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need Help
# 1  
Old 06-21-2011
Need Help

I want to append the date at the end of each line.
I have tried the following, but neither is working.

Kindly help me

[CODE]
>outputFile.txt
Code:
    chmod 777 outputFile.txt
    copy inputFile.txt outputFile.txt

#Adding Date to the end of each line
Code:
    var=$(date '+%d-%m-%Y')

#Reading each line and writing each line and then appending date to it.
Code:
    while read line
    do
    printf "%s,%s\n" "$line" "$var"
    done < outputFile.txt

Also used this
# using sed
Code:
 sed "s/$/,$var/" text.file


Last edited by sampandey31; 06-21-2011 at 03:30 AM.. Reason: Please use code tags, thank you
# 2  
Old 06-21-2011
Everything should work, if only outputFile isn't empty.
(Why outputFile? It is input for the loop.)
# 3  
Old 06-21-2011
Quote:
Originally Posted by yazu
Everything should work, if only outputFile isn't empty.
(Why outputFile? It is input for the loop.)
No output file is not empty as I have used

Code:
 cp inputFile.txt outputFile.txt

But the date has not been appended to the outputFile.txt

Kindly tell me my mistake.
# 4  
Old 06-21-2011
use

Code:
 
sed -i

for the while loop -- redirect the output to new file

Code:
while read line
    do
    printf "%s,%s\n" "$line" "$var"
    done < outputFile.txt > new_outputfile.txt

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 06-21-2011
Code:
% cat testfile
I want to append the date at the end of each line.
I have tried the following, but neither is working.

% while read line; do printf "%s,%s\n" "$line" $(date '+%d-%m-%Y'); done <testfile
I want to append the date at the end of each line.,21-06-2011
I have tried the following, but neither is working.,21-06-2011

% var=$(date '+%d-%m-%Y')

% while read line; do printf "%s,%s\n" "$line" "$var"; done <testfile >outputfile

% cat outputfile
I want to append the date at the end of each line.,21-06-2011
I have tried the following, but neither is working.,21-06-2011

This User Gave Thanks to yazu For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question