LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-23-2013, 10:38 PM   #1
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Rep: Reputation: 15
sed end-of-line metacharacter $ not working


I have a short test file (named "dict") comprising 25 words, each on a separate line. A few of these words are the plural form of the word above (e.g. "aardvark" followed by "aardvarks"). I want to mark all the words that end with an "s" as part of a larger project that will eventually delete plural forms from a much larger file (over 100,000 words), but my only concern at this stage of my self-education is to target those words that end with an "s" by replacing the terminal "s" with an "x".

I thought it could be done with the following:
Code:
sed "s/s$/x/g" <dict
However, that returns exactly the contents of the "dict" file with no replacements.

I can replace all "s" characters with
Code:
sed "s/s/x/g" <dict
and I can replace all initial "a" characters with
Code:
sed "s/^a/x/g" <dict
but the end-of-line $ isn't doing what I expected.

I'm using Kubuntu 12.04, if that makes any difference. Any ideas where I should look for a fix?
 
Old 02-24-2013, 01:51 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Perhaps you should investigate the obvious first - maybe the "s" isn't the last character in the line. Whitespace(s) can be difficult to discern.
 
2 members found this post helpful.
Old 02-24-2013, 02:40 AM   #3
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Code:
sed 's/.*s$/x/g'  dict
 
Old 02-24-2013, 08:24 AM   #4
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Original Poster
Rep: Reputation: 15
syg00 -- That helped! I did a hex dump of the "dict" file and found that it had apparently been created in an MS Windows environment. There was a "0d 0a" at the end of each line instead of simply a "0a". I got rid of all those superfluous carriage-return codes with
Code:
sed "s/\x0d//g" <dict >dict2
mv dict2 dict
After that, the original sed code worked fine!
Code:
sed "s/s$/x/g" <dict
whizje, the code you suggested changes the entire word (after I modified the file to delete the carriage-return codes) to a single "x", not just the trailing "s" characters. A variation on that may be useful as I develop my sed script further, but it's not exactly what I was aiming at right now.
 
Old 02-24-2013, 10:31 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For converting line endings, see the tool http://linux.die.net/man/1/dos2unix and unix2dos.
Usually part of the std install and also handles MAC line endings as well.
 
  


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 replace end of line with character grob115 Programming 13 02-14-2013 05:52 PM
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 11:27 AM
sed - Problem appending at the end of line jdom Linux - Newbie 1 11-05-2010 01:21 AM
sed mathing at the end of line by the beging of the next sqn Programming 9 02-18-2010 08:10 AM
[SOLVED] sed: How to remove the end of a line? angel115 Programming 2 10-01-2007 10:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:18 PM.

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