Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find everything after first comma in lines and remove it

Tags:

regex

sed

awk

I have a file containing a bunch of lines like this blabla-any-characters**,**numbers-and-characters
I would like to keep everything before the first comma and remove everything else.
Any hint how to do this with awk or sed?
Thanks

like image 868
codyLine Avatar asked Dec 04 '25 18:12

codyLine


2 Answers

You can use sed like this:

sed -i.bak 's/,.*$//' file

,.*$ will match anything after first comma and will replace it by an empty string.

-i.bak is for inline editing in sed.

like image 63
anubhava Avatar answered Dec 06 '25 09:12

anubhava


Try this with cut:

cut -d ',' -f 1 file
like image 43
Cyrus Avatar answered Dec 06 '25 10:12

Cyrus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!