Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed - remove period at end of line

Tags:

regex

sed

I'm trying to remove periods that are at the end of line in a text file. Some lines have periods at the end, some don't:

$cat textfile
sometexthere.123..22.no_period
moretext_with_period.  **<-- remove this period**
no_period_here_either
period.   **<-- remove this period**

I've tried this, but it doesn't seem to work:

sed 's/\.$//g' textfile > textfile2

(GNU sed version 4.2.1)

Thanks

like image 318
user559555 Avatar asked Nov 29 '11 20:11

user559555


1 Answers

This is a shot in the dark, but I had this problem before when I tried to mix Windows files with Linux files. Windows adds an extra \r on every line break (in addition to the standard \n)Have you tried using dos2unix?

[user@localhost ~]$ cat testfile
abc
def.
[user@localhost ~]$ sed 's/\.$//g' testfile
abc
def.
[user@localhost ~]$ dos2unix testfile
dos2unix: converting file testfile to UNIX format ...
[user@localhost ~]$ sed 's/\.$//g' testfile
abc
def
[user@localhost ~]$ 

Example this -

[user@localhost ~]$ cat temp.txt 
this is a text created on windows
I will send this to unix
and do cat command.

[user@localhost ~]$ cat -v temp.txt 
this is a text created on windows^M
I will send this to unix^M
and do cat command. 
like image 161
Thomas Kelley Avatar answered Nov 03 '22 00:11

Thomas Kelley