intruder
intruder

Reputation: 417

How to truncate the first digit of a number?

For example, my file has the following data:

$ cat sample.txt 

19999119999,string1,dddddd
18888135790,string2,dddddd
15555555500,string3,dddddd

This is a sample data. How can we remove ONLY first digit from each row? My output should be:

$ cat output.txt
9999119999,string1,dddddd
8888135790,string2,dddddd
5555555500,string3,dddddd

Is there any way to parse each line character wise using grep or sed? Or any other way to get the desired output?

Upvotes: 0

Views: 228

Answers (3)

fedorqui
fedorqui

Reputation: 289735

You just need to print from the second character on:

$ cut -c2- file
9999119999,string1,dddddd
8888135790,string2,dddddd
5555555500,string3,dddddd

Or, using sed, remove the first char:

$ sed 's/^.//' file
9999119999,string1,dddddd
8888135790,string2,dddddd
5555555500,string3,dddddd

Upvotes: 4

blackSmith
blackSmith

Reputation: 3154

Grep can solve this with a look behind. For that you need -P option :

grep -Po '(?<=^\d)(.+)' file   

or in shorthand :

grep -Po '^\d\K.+' file

The (?<=^\d)/^\d\K part is the look behind that matches the first digit.

Upvotes: 0

Ren
Ren

Reputation: 2946

Try this:

$ sed -r 's/^[0-9](.*)/\1/' sample.txt

Output:

9999119999,string1,dddddd
8888135790,string2,dddddd
5555555500,string3,dddddd
  • ^[0-9] - The first digit of each line
  • (.*) - The content of each line except the first digit
  • \1 - Denote the content of (.*)

Sorry for my bad English.

Upvotes: 1

Related Questions