LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-30-2013, 07:09 PM   #1
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 122

Rep: Reputation: 17
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
 
Old 01-31-2013, 01:00 AM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 119Reputation: 119
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 View Post
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 View Post
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.
 
1 members found this post helpful.
Old 01-31-2013, 10:21 AM   #3
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 122

Original Poster
Rep: Reputation: 17
Talking 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 -
 
Old 02-01-2013, 12:40 AM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 119Reputation: 119
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.

Last edited by towheedm; 02-01-2013 at 12:44 AM.
 
1 members found this post helpful.
Old 02-01-2013, 09:38 AM   #5
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 122

Original Poster
Rep: Reputation: 17
Talking thanks

Brother towheedm,
I offer High Thanks for your Lucid and detailed explanation.
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
 
Old 02-01-2013, 09:39 AM   #6
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 122

Original Poster
Rep: Reputation: 17
using the , as my delimiter, is now my personal preference as well
 
Old 02-01-2013, 09:23 PM   #7
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 119Reputation: 119
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Keep original mod date of a file and 'date' it back to same file? SilversleevesX Programming 4 07-16-2010 12:12 PM
(bind) named: couldn't open pid file '/var/run/named/named.pid' - any help? samengr Linux - Server 6 04-01-2009 07:22 AM
file /var/lib/named/var/named/reverse/named.zero failed: file not found Toadman Linux - Software 15 03-18-2009 08:01 PM
service named cant start error in named.conf file gayanasa Linux - Server 2 07-02-2008 10:58 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 10:59 AM


All times are GMT -5. The time now is 09:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration