Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed or awk question 2

Status
Not open for further replies.

chomps303

MIS
Sep 30, 2003
83
0
0
US
I have a standard ascii comma, quoted file. I need to add todays date to the end of the record.

This will run in a script and run daily... So the date will be a variable.

ie:

"aaaaaaa","bbbbb","cccccc","ddddd" < Now

"aaaaaaa","bbbbb","cccccc","ddddd","20041207" < Needs

Thanks
Brandt
 
sed -e "s/$/,\"$(date +%Y%m%d)\"/g" file

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
vgersh99

When I try your line I get the Literal:


"AIL","A","","","","000000000156425","","","3","1","","N","N","","","","","","",
"","20041114","20041126","","","","","","","","","","","","","Y","","$(date +%Y%m%d)"

Thanks

Brandt
 
Hi:

I hate to be picky, but if you use printf, it should work with bash also. print's not defined with Linux. If you're not crossing midnight, there's also no reason to execute the date command for each line, but that's your choice:

mydate=$(date +%Y%m%d)
while read line
do
printf "%s,\"%s\"\n" $line $mydate
done < file
 
#cat file
"asdfads","adsfasdf","asew"
"4343wr","aefaewetwe4343","adf"

# while read line^Jdo^Jprint $line,\"`date +%Y%m%d`\"^Jdone < file
"asdfads","adsfasdf","asew","20041207"
"4343wr","aefaewetwe4343","adf","20041207"
 
Using the sed statement from vgersh99 (works fine for me):

#cat file
"asdfads","adsfasdf","asew"
"4343wr","aefaewetwe4343","adf"

#sed -e "s/$/,\"$(date +%Y%m%d)\"/g" file
"asdfads","adsfasdf","asew","20041207"
"4343wr","aefaewetwe4343","adf","20041207"
 
if you're NOT running from within ksh/bash, try:

sed -e "s/$/,\"`date +%Y%m%d`\"/g" file

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
THANKS to all who responded. Once again you all are the best reference available in the Unix world.

Worked great!!

Brandt Eppler
PMP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top