LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2005, 10:36 AM   #1
tifu
LQ Newbie
 
Registered: Mar 2005
Location: markham, ontario
Posts: 6

Rep: Reputation: 0
Angry sed question


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

Thanks

Omar
 
Old 03-18-2005, 11:17 AM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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.
 
Old 03-18-2005, 11:23 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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"
This worked as expected.
 
Old 03-18-2005, 11:28 AM   #4
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
I was playing around with this too, and came to the same conclusion.

However, I also noted that the single quote doesn't have to be escaped when enclosed in double quotes.
 
Old 03-18-2005, 11:32 AM   #5
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Quote:
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. (=
 
Old 03-18-2005, 12:02 PM   #6
tifu
LQ Newbie
 
Registered: Mar 2005
Location: markham, ontario
Posts: 6

Original Poster
Rep: Reputation: 0
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.

Thanks again

tr ' ' '\n' <$FILEDIR/file01 | sed -e "s/[^a-zA-Z'-]//;/^$/ d" >$FILEDIR/file02
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed question ckoniecny Linux - General 3 11-11-2005 09:31 AM
sed question sphynx Programming 6 04-28-2004 06:25 PM
little sed question freelinuxcpp Linux - Software 3 01-20-2004 07:36 AM
sed question zoomzoom Linux - Software 2 10-20-2003 04:04 PM
A question on 'sed' Barbarian Programming 4 04-11-2002 10:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:25 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration