Sed - delete the period at the end of the line

I am trying to remove periods that are at the end of a line in a text file. Some lines have periods at the end, some do not:

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

I tried this, but it does not work:

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

(GNU version version 4.2.1)

thanks

+4
source share
4 answers

This is a shot in the dark, but I used to have this problem when I tried to mix Windows files with Linux files. Windows adds an extra \r for each line break (in addition to the \n standard). 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 ~]$ 

An example of this is

 [ 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. 
+7
source

If this requires a single sed command, without using dos2unix , which modifies the source file in place, you can do something like this (GNU sed may be required)

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

If you type ^M on the command line as Ctrl + V , then Ctrl + M.

This will remove the '.', Optionally followed by a carriage return character, and replace CR if it was in the original.

+3
source

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

+1
source
 sed -r 's/\\.$//' 

should also work to remove the last period.

0
source

Source: https://habr.com/ru/post/1383729/


All Articles