Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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:
-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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.