How to delete nth character in a file using sed

2019-07-31 13:41发布

I have to delete a character at nth position in a big file. Vi hangs for such a big file. I know there will be some simple command in sed to do so. But, I find it difficult to understand sed scripts and its expressions.

The file I have has content like:

{"query": "Lock and", "timestamp": "2012-12-28T00:00:00.045000+00:00", "productId": 322506},,{"query": "Velvet Crush", "timestamp": "2012-12-28T00:00:00.045000+00:00", "productId": 134363}

I have to delete that extra , which is 130405 th character in that file. How do I use sed to achieve this.

EDIT:

Now I wish to replace all aoocurances of double comma by a single one in-place. How can that be done?

3条回答
【Aperson】
2楼-- · 2019-07-31 13:58

sed -i 's/.//130405' FILE

This edits the file (FILE) in place (-i), deleting any character (.) at position 130405

查看更多
贪生不怕死
3楼-- · 2019-07-31 14:00

Without even worrying about exactly where the double comma is, and assuming you want to fix any double comma throughout the file:

sed -e 's/,,/,/g' < file > file.new
mv file.new file

If you have a version of sed that supports it, you can sed -i -e '...' file to skip the redirections (but sed -i still essentially does exactly the same thing, just with a temporary file).

查看更多
祖国的老花朵
4楼-- · 2019-07-31 14:14

Why sed when you have cut?

cut -c -130404,130406-
查看更多
登录 后发表回答