AIX: remove the last symbols (CRLF) from a file

倖福魔咒の 提交于 2019-12-11 14:44:37

问题


There is a large file where the last symbols are \r\n. I need to remove them. It seems to be equivalent to removing the last line(?). UPD: no, it's not: a file have only one line, which ends with \r\n.

I know two ways, but both don't work for AIX:

sed 's/\r\n$//' file # I don't why it doesn't work
head -c-2 # head doesn't work with negative numbers

Is there any solution for AIX? A lot of large files must be processed, so performance is important.


回答1:


Usually, if you need to edit a file via a script in place, I use ed due to historical reasons. For example:

ed - /tmp/foo.txt <<EOF
g/^$/d
w
q
EOF

ed is more than a bit cantankerous. Note also that you did not really remove the empty lines at the bottom of the file but rather all of the empty lines. With ed and some practice you can probably achieve deleting only the empty lines at the bottom of the file. e.g. go to the bottom of the file, search up for a non-empty line, then move down a line and delete from that point to the end of the file. ed command scripts act (pretty much) as you would expect.

Also, if they really do have \r\n, then those are not going to be considered empty lines but rather lines with a control-M (\r) in them. You may need to adjust your pattern if that is the case.




回答2:


My answer https://stackoverflow.com/a/46083912/3220113 to the duplicate question should work here too. Another solution is using

awk ' (NR>1) { print s }
      {s=$0}
      END { printf("%s",substr($2, 1, length($2)-1) ) }
    ' inputfile


来源:https://stackoverflow.com/questions/46041958/aix-remove-the-last-symbols-crlf-from-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!