Remove the lines starting with a character in shel

2019-08-20 05:56发布

I have dataset as:

file.txt
de
fds
fds
a
sa
 -1
 2
 -3
 1
}

I would like to delete all the lines starting with characters or special characters, but preserve the negative values. So my outfile is:

out.txt
-1
2
-3
1

This question is related to one of my earlier questions Remove the lines starting with a character in shell with which I can remove all lines starting with a character including negative sign.

I need to modify my code.

my code:

grep -E "^[0-9].*" file.txt

2条回答
你好瞎i
2楼-- · 2019-08-20 06:29

You can try like this:

grep '^ *[-+]\?[0-9]\+' file.txt > out.txt

(Or)

grep -E '^ *[-+]?[0-9]+' file.txt > out.txt
查看更多
孤傲高冷的网名
3楼-- · 2019-08-20 06:39
$ grep -E '^[-| ][0-9]' file.txt > out
$ cat out
-1
 2
-3
 1
查看更多
登录 后发表回答