codyLine
codyLine

Reputation: 519

Find everything after first comma in lines and remove it

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

Upvotes: 2

Views: 13085

Answers (4)

Cyrus
Cyrus

Reputation: 88583

Try this with cut:

cut -d ',' -f 1 file

Upvotes: 4

zedfoxus
zedfoxus

Reputation: 37039

You can do that with awk relatively easily.

$ cat test.txt
blabla-any-characters**,**numbers-and-characters
john,smith
hello,world

$ awk -F',' '{print $1}' test.txt
blabla-any-characters**
john
hello

Upvotes: 2

JNevill
JNevill

Reputation: 50034

For awk you could use:

awk -F"," '{print $1}' file

Which will delimit each record using a comma and then only print the first element.

Upvotes: 2

anubhava
anubhava

Reputation: 784998

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.

Upvotes: 8

Related Questions