2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sedでうまくいかない時の^Mの削除 [linux環境で有効(多分)]

Last updated at Posted at 2018-01-12

結論

$ cat file_A  | unix2dos > file_A 

のどちらかで解決。

ーーーーーー

人から転送してもらったテキストファイルをlinux環境でsedコマンド使って置換した時にvi環境で^Mが発生してしまった。。
別に支障はないのだけどどうしても気になる。

---file_A---

  SIGMA  = 0.05   
  NPAR   = 15
  LORBIT = 11

というファイルに対してキーワードのある1行分を置換したい時、

sed -i 'NPAR/c\  NPAR=10' file_A

で出来たのだけど、置き換わったファイルが次のようになってしまってる。

---file_A(置換後)---

  SIGMA  = 0.05^M
  NPAR   = 15^M
  LORBIT = 11^M

・・・・なんか気持ち悪い。。

[sed ^M 削除]
で調べたら

sed -e 's/^M//g' file_A

で直るそうなのだが自分の環境(Linux)では効果がない。。
しばらくネットで探して見たら

$ cat file_A  | unix2dos > file_A

で解決した(どちらか一方でOk)。Windows環境の改行コード(CRLF)がLinuxで化けた(?)みたい。

参考記事
https://qiita.com/dharry/items/b4eead2868dddf1499f0
http://www.koikikukan.com/archives/2013/10/15-013333.php

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?