validating data in columns and writing them to new file

I have a file that contains records in the below format:

23857250998423948239482348239580923682396829682398094823049823948
23492780582305829852095820958293582093585823095892386293583203248
23482038509825098230958235234230502958205983958235820358205892095
396873950075037609888888000000XT035890980820000000000000000000000
503689234986246723490502389593XT824959500305000000000000000000000
234235248649368345435349689459XT834534845900005289234994939459300
423852349234943923495655494969XT240895300503030300000000000000000
2352463574767845656534634634643XT45235246537457846868686474674577

I want to check for X & T in column's 31 & 32 and write only those records to new file.

the new file should have only 4 records:

396873950075037609888888000000XT035890980820000000000000000000000
503689234986246723490502389593XT824959500305000000000000000000000
234235248649368345435349689459XT834534845900005289234994939459300
423852349234943923495655494969XT240895300503030300000000000000000

If there are line feeds between records, it seems a pretty simple grep/regex problem! Not homework, I hope!

1 Like
perl -F"" -alne '{($F[30]." ".$F[31])==("X T")?print:next;}' input_file
1 Like

Thank you! Can't use perl, only in unix please

awk -F "" '$31=="X" && $32=="T"'  input_file

1 Like
grep 'XT' filename > newfilename

Well, PERL, awk, sed, grep are all apps, but PERL is a full language (in which people shell out a lot!) not native to all UNIX systems. I like sed's moderately expanded regex:

sed '^.\{30\}XP/!d' infile >outfile
1 Like

coooll...some minor correcti:)n...thanks DG

1 Like

It is nice if a solution is all of: terse, intuitively understandable, deconstructable and reusable. :smiley:

1 Like

Awesome thanks, It worked!!

---------- Post updated at 05:08 PM ---------- Previous update was at 05:07 PM ----------

Thx DG...coolll!!

Sed is pretty close to vi and ksh/bash mode 'set -o vi' command recall and modify, so the skills are also transferrable. Of course, many of these tools can use regex.