我试图删除第四个字符前的零。如果零出现在第四个位置之后,则无需删除它。我不能正确地做到这一点。
条件:

01230 <------Delete 1 zero at start.
001230 <-----Delete 2 zeros at start.
0001230 <----Delete 3 zeros at start.
00001230<----Delete 4 zero at start.
000001230<---Delete 4 zero at start and leave 1, output 01230
1234560<-----Delete nothing.

例子:
输入文件:
cat file
0000abc0
00abcde0
0abcede0
00000abcede0

预期产量:
abc0
abcde0
abcede0
0abcede0

已经尝试过的:(当然没有帮助)
    cat file |sed 's/^[0]//g' <----This just delete one zero at the start
    000abc0
    0abcde0
    abcede0
    0000abcede0

    cat file | sed 's/^[0][0][0][0]//g'<---THis only works for line having 4 zeros.
    abc0
    00abcde0
    0abcede0
    0abcede0

cat file | sed 's/^[0]*//g' <-----Removes all the zeros at start.
abc0
abcde0
abcede0
abcede0

cat file | sed 's/0//g'{4} <------I am lost what it do!!
000abc
00abcde0
0abcede0
000abcede

最佳答案

使用{}指定发生次数,-r允许扩展regexp语法:

sed -r 's/^0{1,4}//g'

10-08 19:43