sed - remove period at end of line

19,121

Solution 1

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. 

Solution 2

If you need a single sed command to do this, without using dos2unix, which changes the original file in place, you can do something like this (may require GNU sed)

sed -E 's/\.(^M?)/\1/' testfile

Where you type ^M on the command line as Ctrl+V followed by Ctrl+M.

This will remove a '.', optionally followed by a Carriage-return character, and replaces the CR if it was present in the original.

Solution 3

sed -r 's/\.$//'

This should also work for removing the last period.

Solution 4

sed 's/period[.| ]*$//g' ts.txt > ts1.txt

input file: sometexthere.123..22.no_period moretext_with_period.
no_period_here_either period.

output file: sometexthere.123..22.no_ moretext_with_ no_period_here_either

Share:
19,121
user559555
Author by

user559555

Updated on July 14, 2022

Comments

  • user559555
    user559555 almost 2 years

    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

  • user559555
    user559555 over 12 years
    This seems to work. dos2unix seems to do "something" with the file so that the command sed 's/\.$//g' now works... What has dos2unix actally done? My text file is a column from a very large csv file in case people wonder.
  • Thomas Kelley
    Thomas Kelley over 12 years
    At the end of every line on a Linux file is \n, but on the end of every line on a Windows file is \r\n. Your regex wasn't matching because the period was not directly next to the \n (the \r was in the way).
  • jaypal singh
    jaypal singh over 12 years
    I have added an example in this solution. Once it is reviewed you should be able to see it. If you notice such anomalies, a good way of inspecting is to do a cat -v filename. This would show all non-printing characters so that they are visible.
  • Keith Thompson
    Keith Thompson over 12 years
    @Jaypal: cat -A filename, if you have a version of cat that supports it, is even better. In addition to showing non-printable characters in printable format, it replaces tabs with ^I and adds $ to the end of each line.
  • jaypal singh
    jaypal singh over 12 years
    cat -vet filename where -A option is not available. :)
  • ishamdo
    ishamdo almost 6 years
    I believe you have a redundant '\' sed -r 's/\.$//' worked for me.