Nexus490
Nexus490

Reputation: 329

Removing Leading 0 and applying Regex to Sed

I have several file names, for ease I've put them in a file as follows:

01.action1.txt
04action2.txt
12.action6.txt
2.action3.txt
020.action9.txt
10action4.txt
15action7.txt
021action10.txt
11.action5.txt
18.action8.txt

As you can see the formats aren't consistent what I'm trying to do is extract the first numbers from these file names 1,4,12,2,20 etc

I have the following regex

(\.)?action\d{1,}.txt

Which is successfully matching .action[number].txt but I need to also match the leading 0 and apply it to my substitute with blank in sed so i'm only left with the leading numbers. I'm having trouble matching the leading 0 and applying the whole thing to sed.

Thanks

Upvotes: 0

Views: 35

Answers (2)

justaguy
justaguy

Reputation: 3022

I don't know if the below awk is helpful but it works as well:

awk '{print $1 + 0}' file
1
4
12
2
20
10
15
21
11
18

Upvotes: 1

Cyrus
Cyrus

Reputation: 88999

With GNU sed:

sed -r 's/0*([0-9]*).*/\1/' file

Output:

1
4
12
2
20
10
15
21
11
18

See: The Stack Overflow Regular Expressions FAQ

Upvotes: 1

Related Questions