Fadri
Fadri

Reputation: 13

delete numbering in a textfile using sed

I try to delete a numbering in a textfile using sed on my raspberry pi.

textfile:

0 100
1 125
2 160
3 200
4 250
5 320
6 400
7 500
8 640
9 800
10 1000
11 1250
12 1600

Output after sed:

100
125
160
200
250
320
400
500
640
800
1000
1250
1600

sed -i 's/^.{,2}//' - only removes the first 2 numbers but when you have more than 10 it won't work.

is there a way to remove the first value until a space?

Upvotes: 1

Views: 67

Answers (2)

John1024
John1024

Reputation: 113834

This removes leading digits while leaving lines without leading digits unchanged:

$ sed 's/^[[:digit:]]\{1,\}[[:blank:]]*//' textfile 
100
125
160
200
250
320
400
500
640
800
1000
1250
1600

How it works

In sed, substitute commands have the form s/old/new/ where old is a regular expression. In our case, we want the replacement to be nothing so the new field is empty.

We want to start by matching digits at the beginning of the line: ^[[:digit:]]\{1,\}. The ^ means beginning-of-the-line. [[:digit:]]\{1,\} matches one or more of any digit.

We also want to remove the space after the digits. [[:blank:]]* matches any combination of zero or more blanks or tabs.

In olden days, we might have used [0-9] to represent digits. With modern unicode fonts, this is no longer safe. [:digit:]] will match all digits in unicode fonts and only digits.

Upvotes: 1

karakfa
karakfa

Reputation: 67467

awk to the rescue!

awk '{print $2}'

will only print the second field. You can achieve the same with cut

cut -d" " -f2

Upvotes: 0

Related Questions