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.
I have a file (file01) with several words that I need to output to another file (file02). I need file02 to be formated to a single column, will all non-alphabet characters removed, except for dash (-) and appostrophe (') characters.
When I use the following, it formats correcly and removes all non-alphabet characters (including ' and -)
tr ' ' '\n' <$FILEDIR/file01 | sed -e 's/[^a-z A-Z]//;/^$/ d' >$FILEDIR/file02
When I try to specify the exclusion of ' and - characters as in below, the command fails. What am I doing wrong?
tr ' ' '\n' <$FILEDIR/file01 | sed -e 's/[^a-z A-Z]//;s/[\']//; s/[\-];/^$/ d' >$FILEDIR/file02
Note that the sed expression uses single quotes (') for it's info, you'll need to escape the single quote in the body of the sed block. That's prolly the error.
Ok, after playing, it looks like you'll have to change the sed block characters from a single quote(') to a double quote("), which will then allow you to escape the single quote in the body:
Code:
cat filename.txt | sed -e "s/[^a-zA-Z\\'-]//g;/^$/ d"
Originally posted by ahh However, I also noted that the single quote doesn't have to be escaped when enclosed in double quotes.
(= I thought I had tried it without and it didn't work, but I just tried it again and sure enough. (= Ok, well at any rate, you should be good to go. (=
Thank you both. I ended up using the command below. Note the \ as suggested [^a-zA-Z\'-] meant that I wanted to keep the character \ as well. I removed it and my universe is all well.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.