Search code examples
shellunixipcharactercut

How to cut a specific character in a file in unix shell script


I have a problem. I have a file. This file contains about 100 rows and each row is an IPv6 address. My task is to cut the 0 where it stands first place of a block or remove the whole block if it contains only 0. Here is an example: ORIGINAL:

2001:0db8:03cd:0000:0000:ef45:0006:0123

MODIFIED:

2001:db8:3cd:::ef45:6:123

Thnaks in advance.


Solution

  • try:

    awk '{for(i=1; i<=NF; i++) sub(/^0+/,x,$i)}1' FS=: OFS=: file
    

    or

    perl -pe 's/(^|:)0+/$1/g' file