Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete string from line that matches regex with AWK

Tags:

awk

I have file that contains a lot of data like this and I have to delete everything that matches this regex [-]+\d+(.*)

Input:

zxczxc-6-9hw7w
qweqweqweqweqwe-18-8c5r6
asdasdasasdsad-11-br9ft

Output should be:

zxczxc
qweqweqweqweqwe
asdasdasasdsad

How can I do this with AWK?


1 Answers

AFAIK awk doesn't support \d so you could use [0-9], your regex is correct only thing you need to put it in correct function of awk.

awk '{sub(/-+[0-9].*/,"")} 1'  Input_file

You don't need the extra <plus> sign afther [0-9] as this is covered by the .*

like image 56
RavinderSingh13 Avatar answered Oct 30 '25 14:10

RavinderSingh13