Home > Enterprise >  Unix shell script for sequence mismatch
Unix shell script for sequence mismatch

Time:08-16

I have scenario where I have thousands of record and i have below number like below

Ex...

2345645555689
2345667766690
2345667888892

If we see 89 and 90 are in sequence so it should go to 1 file and if sequence gets break like 92 then new file should be created. Looking for shell script for the same. Could you please help on the same. Thanks.

CodePudding user response:

# assuming positive first input number
prev=-1
while read
do
  if (( REPLY > (prev 1) ))
  then
    file=f_$REPLY
  fi
  echo $REPLY >>$file
  prev=$REPLY
done

Beware: untested by me

CodePudding user response:

From your description, you seem to want to change output file when (line2 - line1) modulo 100 is greater than 1.

awk '
    NR==1 || ($0-prev)0 > 1 {
        close(outfile)
        outfile = "out"(  n)
    }
    {
        print > outfile
        prev = $0
    }
' input-file
  • NR==1 initialise output file before p is set
  • ($0-prev)0 > 1 = condition for changing output file
  • Related