justaguy
justaguy

Reputation: 3022

remove all text after semi-colon using awk

I am trying to parse the below output using awk to remove all text after the semi-colon in each line. Thank you :).

awk -F '\t' 'BEGIN { OFS = FS } NR != 1 { sub(/; [^ ]*$/, "", $NF) } 1' file1.txt >: result.txt

file1.txt

chr1 40539722 40539865 PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1 
chr1 40542503 40542595 PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1 
chr1 40544221 40544340 PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1 
chr1 40546058 40546169 PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1;PPT1

Desired result.txt

chr1 40539722 40539865 PPT1
chr1 40542503 40542595 PPT1
chr1 40544221 40544340 PPT1
chr1 40546058 40546169 PPT1

Upvotes: 2

Views: 402

Answers (2)

fedorqui
fedorqui

Reputation: 290165

You want to cut the file and show the first field on ;-separated values. So what about using cut itself?

$ cut -d';' -f1 file
chr1 40539722 40539865 PPT1
chr1 40542503 40542595 PPT1
chr1 40544221 40544340 PPT1
chr1 40546058 40546169 PPT1

-d sets the delimiter and -f indicates which fields you want to print.

Upvotes: 1

melpomene
melpomene

Reputation: 85837

Is there anything wrong with just doing

awk -F ';' '{ print $1 }'

? Seems much simpler.

Upvotes: 4

Related Questions