LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   cp a file to named date (http://www.linuxquestions.org/questions/linux-general-1/cp-a-file-to-named-date-4175447906/)

zimbot 01-30-2013 06:09 PM

cp a file to named date
 
so I have a log that spits out lines in the form of
Transferring file `1MIN_TEST01.mov'
and i wish to have only
1MIN_TEST01.mov
the Base File Name
and finally i wish to have that log named the date
so if today is jan 30 2013 - 013013.txt

here is what i have
<code>
#!/bin/bash
### this is a xample line from log:: Transferring file `1MIN_TEST01.mov'
## ~/log/\f_h_logfilea.txt
#h="Transferring file `1MIN_TEST01.mov'"
#######################
#2day=$(date)+%m%d%y
#2day=$(`date +%y%m%d`)
d=`date +%m%d%y`
sed 's/Transferring file//g' ~/\log/\f_h_logfilea.txt > ~/\log/\f_h_logfile4a.txt
sed "s/\`//g" ~/\log/\f_h_logfile4a.txt > ~/\log/\f_h_logfile4b.txt
sed "s/'//g" ~/\log/\f_h_logfile4b.txt > ~/\log/\f_h_logfile4c.txt
sed "s/ //g" ~/\log/\f_h_logfile4c.txt > ~/\log/\f_h_logfilefin.txt
echo $d
##cp ~/\log/\f_h_logfilefin.txt ~/\log/\f_h_logfilefin.txt+`date +%m%d%y`
cp ~/\log/\f_h_logfilefin.txt ~/\log/\$d.txt

</code>

now this will echo
013013
but the file that is cp'ed is named $d.txt

I am ever so confused how
echo $d can be 013013 ( provided the day is jan 30 2013
and the file name is literal $d.txt

any help is appreciated.

also -- likely there is a better means of sed ..
i just keep running it through and cleaning it up
my thinking was those 'versions'
f_h_logfile4a.txt
f_h_logfile4b.txt
f_h_logfile4c.txt
f_h_logfile4fin.txt
just get over written and i have a tidy 013013.txt

towheedm 01-31-2013 12:00 AM

If you intend to add code tags manually, place them with [] and not <>, ie: [CODE]...[/CODE].

Also, it is better to use the $HOME env var instead of the ~ when referring your home directory within a script. This ensures it gets expanded correctly.

Quote:

Originally Posted by zimbot (Post 4880981)
sed 's/Transferring file//g' ~/\log/\f_h_logfilea.txt > ~/\log/\f_h_logfile4a.txt
sed "s/\`//g" ~/\log/\f_h_logfile4a.txt > ~/\log/\f_h_logfile4b.txt
sed "s/'//g" ~/\log/\f_h_logfile4b.txt > ~/\log/\f_h_logfile4c.txt
sed "s/ //g" ~/\log/\f_h_logfile4c.txt > ~/\log/\f_h_logfilefin.txt

Why all the escapes (\)?

These can be replaced with (I've used the , as my delimiter):
Code:

sed -i "s,Transferring file \`\(.*\)',\1," $HOME/log/f_h_logfilea.txt
The -i option edit files in place.

Quote:

Originally Posted by zimbot (Post 4880981)
I am ever so confused how
echo $d can be 013013 ( provided the day is jan 30 2013
and the file name is literal $d.txt

Again, unnecessary use of escape.
Code:

cp ~/\log/\f_h_logfilefin.txt ~/\log/\$d.txt
The \$d means literally $d and not the var $d.
After running the above sed command, simply move the log file to the new name:
Code:

mv $HOME/log/f_h_logfilea.txt $HOME/log/$d.txt
You new script is now quite simple:
Code:

#! /bin/bash
old_logfile="$HOME/log/f_h_logfilea.txt"
new_logfile="$HOME/log/$(date +%m%d%y).txt"
sed -i "s,Transferring file \`\(.*\)',\1," $old_logfile
mv $old_logfile $new_logfile
exit

Hope it helps.

zimbot 01-31-2013 09:21 AM

thanks
 
1st thanks for the note about code tags...
and opps on me ... i ahve just seen how one may ( # gadget in interface ) mark code.

and
yep that works
much cleaner
thanks for the word about use of $HOME , did not know that one

and I see the sed is working as
sed -i "s,Transferring file \`\(.*\)',\1,"
I struggle to understand it part by part .
what is the ,1, doing?

also I may need additionally to sed OUT
Making directory `trashbox'

much thanks

I love this community -

towheedm 01-31-2013 11:40 PM

The command:
Code:

sed -i "s,Transferring file \`\(.*\)',\1,"
  • -i - sed option for editing in place. All changes are saved to the input file. See man sed for more info.
  • "......" - I've used weak-quotes because part of the pattern we're searching for includes the string-quote character, ie: the ' character. This avoids unnecessary escaping and potential problems.
  • s, - sed's substitute command. I'm using the , as my delimiter, just my personal preference.
  • Transferring file \`\(.*\)' - Search for Transferring file followed by a space then by a backtick character (the backtick is a reserved character in the SHELL so it must be escaped). The part in red is referred to as back-references in RegEx. sed remembers whatever pattern is inside the brackets. You can have up to 9 remembered patterns. Then followed by a apostrophe character.
  • \1 - Replace the old pattern with the first back-reference (remembered pattern), ie:1MIN_TEST01.mov.

Hope I've explained it properly so you can sed out the other stuff.

zimbot 02-01-2013 08:38 AM

thanks
 
Brother towheedm,
I offer High Thanks for your Lucid and detailed explanation.:hattip:
To your credit - i understand , i grow.
If ever in cincinnati - I am buying yall a beverage of choice.:)

that matter of
\1 - Replace the old pattern with the first back-reference (remembered pattern),
i never read that in any of my investigation nor the matter of
as back-references in RegEx. sed remembers whatever pattern is inside the brackets.

The best part about being shown - is it enables one to learn more on one's own.

wonderful

thanks again

zimbot 02-01-2013 08:39 AM

using the , as my delimiter, is now my personal preference as well

towheedm 02-01-2013 08:23 PM

Here are two links on back-references:
http://www.grymoire.com/Unix/Sed.html#uh-4
http://www.grymoire.com/Unix/Regular.html#uh-10


All times are GMT -5. The time now is 01:18 AM.