ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Hi folks,
I hate to admit, but I am just terrible with man and info pages so most
of the time I end up doing stupid stuff so I understand better what I
am doing.
I am wanting to learn more about sed and there is probably a good
tutorial out there for me but I just haven't found it yet. Maybe
because sed is such a great tool with a lot of abilities.
So, here's my stupid stuff and maybe someone can help me out.
I touched a file (something silly)
Code:
www.any.is_a.jpt
Now, I want to get rid of any "." (simple enough)
or I want to get rid of all "." and "_" (simple enough)
Code:
bash-3.1$ ls
www.any.is_a.jpt
bash-3.1$ echo www.any.is_a.jpt | sed 's/[.]//'
wwwany.is_a.jpt
bash-3.1$ echo www.any.is_a.jpt | sed -e 's/[.]//g' -e 's/[_]//'
wwwanyisajpt
bash-3.1$
But, what I really wanted to do is just remove the "first and second"
"." from the file so it looks like this "wwwanyisa.jpt"
I would not be surprised if this problem could be solved using sed. However, if it were my choice, I would look for another tool. sed is very line oriented.
However, it might be possible to construct a regular expression that has everything wildcarded except each "." character. Then, using the substitute command, you might be able to replace each "." with nothing.
Somtimes, it's okay to use something else. awk might let you make every "." into its own field, depending again on how you format the regular expression.
for "to keep part of the pattern", your solution might be close.
sed and awk are great. They are the right tools to use for the appropriate job, but don't be afraid to use something else, if you find it more appropriate. Perl was invented after sed and awk for a reason.
I use sed to add columns to a delimited data file that is going to be loaded into a database table; to convert "," delimiters to "|" delimiters; and to remove the last character in a line. sed is perfect in these instances, because it is clean, terse, and makes sense for my use. However, when I need C, awk, or Perl, I use those too.
Last edited by cmnorton; 02-10-2008 at 03:58 PM.
Reason: To add more information
I understand what your saying, I just have a hard time understanding
what is best for each situation. Just doing silly things like this
really help me understand and I wanted to get a good understanding
of the older "basic" stuff like bash, sed, & awk, before I tried to
use the more extensive tools.
Anyway, this little exercise did help me and I found that the
following will work although I do agree it's kind of crazy because
I'm actually stripping out the "." and then the next "." to accomplish
what maybe "Perl" would do without evaluating the code twice.
Here is how I accomplished this with sed.
Code:
bash-3.1$ ls
www.any.is_a.jpt
bash-3.1$ echo www.any.is_a.jpt | sed -e 's/[.]//1' -e 's/[.]//1'
wwwanyis_a.jpt
bash-3.1$ echo www.any.is_a.jpt | sed -e 's/[.]//1' -e 's/[.]//1' -e 's/[_]//g'
wwwanyisa.jpt
bash-3.1$
Here is one solution that has sed at the core, but relies on other commands as well:
Code:
#!/bin/bash -
# @(#) s1 Demonstrate elimination of characters except last.
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) rev sed
SOURCE="www.any.is_a.jpt a.b.c.com d.....e.net"
for NAME in $SOURCE
do
echo
echo " Converting \"$NAME\" to:"
echo "$NAME" |
rev |
sed "s|[.]||2g" |
rev
done
exit 0
Producing:
Code:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
rev (local) - no version provided.
GNU sed version 4.1.2
Converting "www.any.is_a.jpt" to:
wwwanyis_a.jpt
Converting "a.b.c.com" to:
abc.com
Converting "d.....e.net" to:
de.net
See man or info pages for details ... cheers, makyo
Hi folks,
I hate to admit, but I am just terrible with man and info pages so most
of the time I end up doing stupid stuff so I understand better what I
am doing.
I am wanting to learn more about sed and there is probably a good
tutorial out there for me but I just haven't found it yet. Maybe
because sed is such a great tool with a lot of abilities.
So, here's my stupid stuff and maybe someone can help me out.
I touched a file (something silly)
Code:
www.any.is_a.jpt
Now, I want to get rid of any "." (simple enough)
or I want to get rid of all "." and "_" (simple enough)
Code:
bash-3.1$ ls
www.any.is_a.jpt
bash-3.1$ echo www.any.is_a.jpt | sed 's/[.]//'
wwwany.is_a.jpt
bash-3.1$ echo www.any.is_a.jpt | sed -e 's/[.]//g' -e 's/[_]//'
wwwanyisajpt
bash-3.1$
But, what I really wanted to do is just remove the "first and second"
"." from the file so it looks like this "wwwanyisa.jpt"
Thanks in advance.
Code:
# a=www.any.is_a.jpt
# printf "%s.%s\n" `echo ${a%.*} | sed 's/[._]//g'` ${a##*.} #can use bash's own substitution without sed too
wwwanyisa.jpt
I understand what your saying, I just have a hard time understanding
what is best for each situation. Just doing silly things like this
really help me understand and I wanted to get a good understanding
of the older "basic" stuff like bash, sed, & awk, before I tried to
use the more extensive tools.
Anyway, this little exercise did help me and I found that the
following will work although I do agree it's kind of crazy because
I'm actually stripping out the "." and then the next "." to accomplish
what maybe "Perl" would do without evaluating the code twice.
Here is how I accomplished this with sed.
Code:
bash-3.1$ ls
www.any.is_a.jpt
bash-3.1$ echo www.any.is_a.jpt | sed -e 's/[.]//1' -e 's/[.]//1'
wwwanyis_a.jpt
bash-3.1$ echo www.any.is_a.jpt | sed -e 's/[.]//1' -e 's/[.]//1' -e 's/[_]//g'
wwwanyisa.jpt
bash-3.1$
Again, thanks
Thanks for posting this. I completely forgot you can pass the output of sed back into sed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.