I have a file like
ABCD_1_0
ABCD_25_6
ABCD_326_12
ABCD_10_3
where ABCD (can be something else) but always 4 characters followed by a underscore and then numbers that go from 1-9999 and then followed by underscore and then a number that can go from 0-100
from this file I want to strip anything from second underscore onwards i.e. I need a file
ABCD_1
ABCD_25
ABCD_326
ABCD_10
Please help
cat tawk.dat
ABCD_1_0
ABCD_25_6
ABCD_326_12
ABCD_10_3
awk -F_ '{ print $1"_"$2 }' tawk.dat
ABCD_1
ABCD_25
ABCD_326
ABCD_10
by sed..
sed 's/\(...._[0-9]\{,4\}\)_.*/\1/g' inputfile > outfile
sed 's/\(_[^_]*\).*/\1/' infile
cut -d_ -f-2 infile
1 Like
As one of the many possibilities:
>cat 1
ABCD_1_0
ABCD_25_6
ABCD_326_12
ABCD_10_3
>cat 1 | awk 'match($0, /_[0-9]+/) { print substr($0,1,RSTART+RLENGTH-1)}'
ABCD_1
ABCD_25
ABCD_326
ABCD_10
Thanks for a tremendous response, I now need to learn awk and sed. Thanks a million again. God Bless.