Click to See Complete Forum and Search --> : Good awk tutorial, manual, howto, etc.
From my post earlier, I mentioned that I would like to strip off the name of the extension and just be left with a just be left with the name of the file. Now, I'm trying to do the opposite.I do not want to strip off, for example tar.gz, but leave the tar.gz and strip off the rest. But that doesn't just pertain to a tar.gz file, it can be any file, including tar.bz2, tgz, zip or others. I'm trying to strip off everything before the first dot. So, far from the tutorials I read, I have gotten this far:
compressed_name=`ls | grep $input`
compressed_type=`echo $compressed_name | awk '{len=length($1);
var=$1;
slen=(len-5);
type=substr(var,slen,len);
print type}'`
I'm trying to input an if statement that checks if it's a tar.gz file or tar.bz2 file, and if it is, change the slen accordingly. But I'm thinking that there's a much easier way to do it, I just can't get my hands on a decent tutorial. Please help. I'm really trying hard to learn scripting.
maccorin
05-22-2004, 07:43 PM
does it _have_ to be awk? this seems much more like a problem for sed or perl
maccorin@lappy:~$ echo "file.tar.gz" | sed -e 's:[^.]*\.\(.*\):\1:'
tar.gz
maccorin@lappy:~$
in response to your question about a good awk tutorial, i haven't read it straight through or anything, but 'info gawk' seems to be pretty good, or at least complete.
maccorin
05-22-2004, 07:54 PM
as a side not, i've almost never had a need for awk, perl is usually a much better solution IMHO, but you can't count on perl always being there, i guess that's why awk is still semi-popular
the above could have been done just as easily w/ perl, but i don't know your setup, so i'm not sure if you have it
Ok, thank you, it worked. Yeah, I'm not sure yet exactly what job is needed for a certain task yet, I just thought that awk is specifically designed for doing this kind of string manipulation. Awk seems like it has easier syntax to understand that sed, too. But I have perl, and am willing to learn it. I thought perl was for writing programs more, like C or python, though.
maccorin
05-22-2004, 08:17 PM
if you want a quick little intro to regexs in perl you can try my little tut (its a work in progress, i'd like some feedback)
http://maccorin.homelinux.org/?page=regex
No, I'm sorry, that didn't work. Due to my stupidity, I failed to notice that there's a dot before the tar.gz part, which is the number of the release. What I might have to do now is, make an if statement that checks if it's a tar.gz file, and if it is, remove everything but the last 6 letters. If it's a tar.bz2 file, remove everything but the last 7 letters. If it's a tgz file, remove everything but the last 3 letters. Can that be done in perl or sed?
maccorin
05-22-2004, 08:30 PM
Originally posted by ecks
No, I'm sorry, that didn't work. Due to my stupidity, I failed to notice that there's a dot before the tar.gz part, which is the number of the release. What I might have to do now is, make an if statement that checks if it's a tar.gz file, and if it is, remove everything but the last 6 letters. If it's a tar.bz2 file, remove everything but the last 7 letters. If it's a tgz file, remove everything but the last 3 letters. Can that be done in perl or sed?
give me an example of what you have, and what you want
like
i want tar-2.3.tar.bz2 to turn into .tar.bz2
for that you could do something like this
maccorin@lappy:~$ echo tar-2.3.tar.bz2 | perl -pe 's|.*?\.([^\d].*)|$1|'
tar.bz2
maccorin@lappy:~$
maccorin
05-22-2004, 08:34 PM
I decided i should prolly explain the above regex
Originally posted by maccorin
give me an example of what you have, and what you want
like
i want tar-2.3.tar.bz2 to turn into .tar.bz2
for that you could do something like this
maccorin@lappy:~$ echo tar-2.3.tar.bz2 | perl -pe 's|.*?\.([^\d].*)|$1|'
tar.bz2
maccorin@lappy:~$
s means this is a substitution
| is my chosen delimiter
.*? means to match as little as possible of anything while still matching the whole regex
\. matches a .
( starts a group to refer to later
[^\d] matches anything _except_ a digit (0-9)
.* matches as much as possible while still matching the rest of the regex, so the rest of the line in this case
) ends the group
| delimiter again, (this time ending the regex and starting the replacement)
$1 refers to the first group, so it takes everything in the ()
| ends the replacement part
Wow, seems complicated. It worked but I am not sure why. I just also figured out that I need to do the opposite, which is to print out only the file-2.0.1 part without the tar.gz part in order to change into the directory. Can you help me out with that? Can you also point me to some usefull tutorials online? Thank you so much, by the way, I highly appreciate your help.
maccorin
05-22-2004, 09:30 PM
just change the () group to go around the part that you want
read the tut i pointed to you
and do this
perldoc perlretut
ok thank you, I somehow missed your link to the tutorial. Very helpful. I figured out where the () go and now my script finally works after a whole day of struggling! I did:
# echo tar-2.3.tar.bz2 | perl -pe 's|(.*?)\.[^\d].*|$1|'
Does anyone want me to post the script? Thanx a bunch, man, I owe u a lot.
maccorin
05-22-2004, 11:18 PM
Originally posted by ecks
ok thank you, I somehow missed your link to the tutorial. Very helpful. I figured out where the () go and now my script finally works after a whole day of struggling! I did:
# echo tar-2.3.tar.bz2 | perl -pe 's|(.*?)\.[^\d].*|$1|'
Does anyone want me to post the script? Thanx a bunch, man, I owe u a lot.
glad you figured it out :D
definitly post your script, the more code available online, the better
flukshun
05-22-2004, 11:43 PM
An excellent tutorial on regular expressions is included with Perl. Type 'perldoc perlretut' to read it. A more advanced reference can be obtained with 'perldoc perlre' Read and take the time to soak everything in, regex might look pretty crazy but it's actually pretty straight-forward and logical, it's just a matter of knowing what character does what. If you plan on getting very far with your scripting it'll be invaluable to learn, I'm not sure how I ever managed to get anything done without it.
maccorin
05-22-2004, 11:46 PM
Originally posted by flukshun
An excellent tutorial on regular expressions is included with Perl. Type 'perldoc perlretut' to read it. A more advanced reference can be obtained 'perldoc perlre' Read and pay attention, it might look pretty crazy but it's actually pretty straight-forward and logical.
Just want to back that up. I learned everything i know about regular expressions from those 2 docs, i'm still learning to use sed effectivly because i tend to just fall back on perl since i know it better. But i have found it fairly easy to convert perl -> (sed|vim|whatever), regexs are still regexs ;p
Cool, here it is:
#!/bin/bash
download_dir=/home/ecks/Desktop
src_dir=/usr/local/src
input=$1
check_file()
{
file_exist=`ls | grep $input`
if [ -z $file_exist ] ; then
echo "No such file in current directory"
exit
fi
}
check_root()
{
user=`whoami`
if [ $user != "root" ] ; then
echo "You need to log in as root to use this script"
exit
fi
}
cd_dir()
{
tar_dir=`echo $input | perl -pe 's|(.*?)\.[^\d].*|$1|'`
cd $src_dir/$tar_dir
}
check_compressed_type()
{
compressed_type=`echo $input | perl -pe 's|.*?\.([^\d].*)|$1|'`
case $compressed_type in
"tar.gz")tar -x -z -f $input;;
"tar.bz2")tar -x -j -f;;
"*")echo "No recognized compression method found.";;
esac
}
make_env()
{
cd $download_dir
check_file
check_root
mv $input $src_dir
cd $src_dir
check_file
check_compressed_type
cd_dir
}
make_file()
{
./configure
make
make install
make clean
}
# this puts all the files in it's appropriate directories
make_env
# this actually compiles the program. Comment it if you don't want to install.
make_file
I might finish a prompt to ask if you want to actually install it when I get some more free time. I'll eventually add more compress switches too. I'm definetly liking this script, takes me two commands to type in for half a second whereas before it took me a minute just to put everything in place.
Type 'perldoc perlretut' to read it. A more advanced reference can be obtained with 'perldoc perlre'
I don't seem to have the perldoc command included with my perl install, but I do have the man pages for perlretut and perlre. That's the same thing, right?
flukshun
05-23-2004, 11:26 AM
yah, you usually have the option of installing the Perl documentation as man pages in addition to the normal pod format. I'm surprised you don't have 'perldoc' though.