Sequence number merged with hypen, shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sequence number merged with hypen, shell script
# 1  
Old 12-28-2011
Java Sequence number merged with hypen, shell script

Hello Folks,
i have to write shell scripting for given expected output manner.
in given input we have to write shell script in such a way that sequence no can b merged/link between start and end digit with hyphen "-" symbol and rest of digit separated by ","

Eg :

For Input "2 6 7 8 11 12 18 25 26 27"
output should be "2,6-8,11-12,18,25-27"

thanks in advanced.
# 2  
Old 12-28-2011
Seems a bit homeworkey this.

Can you confirm why you have to do this?

How neat do you need this to be? awk will probably process fastest but can be off-putting for many (including me!), but a shell script could be more explicit to allow for adjustments (or show your logic/working out)



Can you expand a little?



Robin
# 3  
Old 12-29-2011
actually we have process to extract data from data base after extracting we have to identify which data are missing so for that we have to write shell script or as you suggested AWK can be used but i dont have that much experience on awk cmd ...
# 4  
Old 12-29-2011
Okay, let's consider the logic (to make sure I get it right) and assuming that the input is sorted. We can use a flag to indicate if we are in a sequence or not, so if we read in the first number then for each following number we have four options:-
  1. If the next number is in sequence and the sequence flag is not set, then set the flag, remembering the start point.
  2. If the next number is in sequence and the sequence flag is set, do nothing.
  3. If the next number is not in sequence, and the sequence flag is not set, append a comma then the previous number to the output.
  4. If the next number is not in sequence, and the sequence flag is set, unset the flag then append the start number, hyphen and the last number of the sequence to the output.
After reading all the numbers, the last action is a choice of two:
  1. If the sequence flag is not set, append the current number to the output.
  2. If the sequence flag is set, append the start of sequence, hyphen and current number to the output.
I think that has it. We can then display the output, store it or whatever.

Based on that, we could write in korn shell:-

Code:
#!/bin/ksh
# Assume we get the input from the command line

prevval=$1                         # Read first item and assign as "previous value"
shift                              # Remove first from input line
seqflag="N"

for currval in $*                  # For every subsequent item
do
   ((nextval=$prevval+1))
   if [ $currval -eq $nextval ]    # We are in sequence
   then
      if [ "$seqflag" = "N" ]      # We were not previously in sequence
      then
         seqflag="Y"
         seqstart=$prevval
      fi                           # No action for continuing sequence
   else                            # We are not in sequence
      if [ "$seqflag" = "N" ]      # We were not previously in sequence
      then
         output="${output},$prevval"
      else                         # We were previously in sequence
         seqflag="N"
         output="${output},${seqstart}-${prevval}"
      fi
   fi
   prevval=$currval                # Set previous value to current value ready to loop round again
done


# So we have read all the input through so we end the output and display or file it

if [ "$seqflag" = "N" ]            # We were not previously in sequence
then
   output="${output},$currval"
else                               # We were previously in sequence
   output="${output},${seqstart}-${currval}"
fi

# Chop off leading comma (maybe better logic would avoid this!)
output="${output#,}"

echo "$output"

I called this sequencer.ksh, so I then ran it and got the following:-

Code:
$ sequencer.ksh 2 6 7 8 11 12 18 25 26 27
2,6-8,11-12,18,25-27
$ sequencer.ksh 4 5 6 7 11 12 13 14 19 22 57 199 200 201 202
4-7,11-14,19,22,57,199-202

Does this help? It goes a bit nuts if it's not in ascending order though:-
Code:
$ sequencer.ksh 1 2 3 4 5 6 7 8 9
1-9
$ sequencer.ksh 1 2 3 5 4 6 7 8 9
1-3,5,4,6-9

Smilie


It is likely that for very large number sets or especialy multi record files contining number sets that an awk could be better for performance, but may be a little terse for some to decipher (including me!)


Robin
Liverpool/Blackburn
UK
# 5  
Old 12-29-2011
hi

while executing i have to get following error,

abc@abc$ sh -x demo.ksh 2 6 7 8 11 12 18 25 26 27
prevval=2
+ shift
seqflag=N
nextval=2+1
+ [ 6 -eq ]
demo.ksh: test: argument expected
# 6  
Old 12-29-2011
Can you ksh run this and see if that changes things. The calculation line
Code:
((nextval=$prevval+1))

.... is a ksh function. Can you add
Code:
echo "\$nextval=$nextval"

.... after the calculation above and see what you get.

You may find that at the end, the removal of the comma is also ksh only too.



Robin
# 7  
Old 12-29-2011
abc@abc$ sh -x demo.ksh 2 6 7 8 11 12 18 25 26 27
prevval=2
+ shift
seqflag=N
+ expr 2 + 1
nextval=3
+ echo $nextval=3
$nextval=3
+ [ 6 -eq 3 ]
+ [ N = N ]
output=,2
prevval=6
+ expr 6 + 1
nextval=7
+ echo $nextval=7
$nextval=7
+ [ 7 -eq 7 ]
+ [ N = N ]
seqflag=Y
seqstart=6
prevval=7
+ expr 7 + 1
nextval=8
+ echo $nextval=8
$nextval=8
+ [ 8 -eq 8 ]
+ [ Y = N ]
prevval=8
+ expr 8 + 1
nextval=9
+ echo $nextval=9
$nextval=9
+ [ 11 -eq 9 ]
+ [ Y = N ]
seqflag=N
output=,2,6-8
prevval=11
+ expr 11 + 1
nextval=12
+ echo $nextval=12
$nextval=12
+ [ 12 -eq 12 ]
+ [ N = N ]
seqflag=Y
seqstart=11
prevval=12
+ expr 12 + 1
nextval=13
+ echo $nextval=13
$nextval=13
+ [ 18 -eq 13 ]
+ [ Y = N ]
seqflag=N
output=,2,6-8,11-12
prevval=18
+ expr 18 + 1
nextval=19
+ echo $nextval=19
$nextval=19
+ [ 25 -eq 19 ]
+ [ N = N ]
output=,2,6-8,11-12,18
prevval=25
+ expr 25 + 1
nextval=26
+ echo $nextval=26
$nextval=26
+ [ 26 -eq 26 ]
+ [ N = N ]
seqflag=Y
seqstart=25
prevval=26
+ expr 26 + 1
nextval=27
+ echo $nextval=27
$nextval=27
+ [ 27 -eq 27 ]
+ [ Y = N ]
prevval=27
+ [ Y = N ]
output=,2,6-8,11-12,18,25-27
demo.ksh: bad substitution
abc@abc$

---------- Post updated at 06:32 PM ---------- Previous update was at 06:31 PM ----------

i had make the correction as below


nextval=`expr $prevval + 1`
echo "\$nextval=$nextval"


but still getting error, you can see on above my post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print Line as per the dependent sequence in shell script.

Hi i have a file like this as shown below: DA PROCESS_ID IDENTIFIER DA_FILE STATUS WAITING_FOR SCOPED_DEPENDENT 1836 21000 01052019 BH90P.TEMP.DA1836.FTP W NULL ... (6 Replies)
Discussion started by: krishnaswarnkar
6 Replies

2. UNIX for Beginners Questions & Answers

Shell Script to Read the given file contents into a merged one file

Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file. While reading and merging the file contents into a single file, Like to keep the format of the source file. ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

3. Shell Programming and Scripting

Will shell script executes in sequence

I have a shell script scheduled in cron job to run at every 1 minute which transfers files to remote machine and then move the files to backup folder. cd /u01/app/ftp_tmp sftp user@hostname <<-EOF cd /home/user/ftp mput * bye EOF mv /u01/app/ftp_tmp/* /u01/app/ftp_bkp Now the problem is... (6 Replies)
Discussion started by: Bhavi
6 Replies

4. Shell Programming and Scripting

Script for getting the file merged

Hi, I have two files separated by bars at each line File 1 : A|4356|13456|23456 A|4356|2986|98732 A|8765|218|1432567 File 2: B|12|13456|1234567 B|11|13456|123456789 B|33|2986|98732 B|11|2986|14578965 B|8765|218|147584 Common field is third field like 13456, 2986 and 218 in both... (5 Replies)
Discussion started by: Mannu2525
5 Replies

5. UNIX for Advanced & Expert Users

Checking missing data's sequence (shell script | UNIX command)

Dear All members, i have some trouble here, i want to ask your help. The case is: I have some data, it's like: -ABCD1234 -ABCD1235 -ABCD1237 -BCDE1111 -BCDE1112 -BCDE1114 there is some missing data's sequence (the format is: ABCD = name 1234 = sequence). I want to print the... (2 Replies)
Discussion started by: septian.tri
2 Replies

6. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

7. Shell Programming and Scripting

How to take the missing sequence Number?

Am using unix aix KSH... I have the files called MMRR0106.DAT MMRR0206.DAT MMRR0406.DAT MMRR0506.DAT MMRR0806.DAT .... ... MMRR3006.DAT MMRR0207.DAT These files are in one dircetory /venky ? I want the output like this ? Missing files are : MMRR0306.DAT MMRR0606.DAT... (7 Replies)
Discussion started by: Venkatesh1
7 Replies

8. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

9. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

10. UNIX for Dummies Questions & Answers

Purpose of - (hypen) in script or command line

Hi, I am new for unix and I am following ABS guide. What is the purpose of - (hypen ) in the below command and What it will do in this?. Can anyone explain it in detail. Rest of the things in the below command I understood somewhat. (cd /source/directory && tar cf - . ) | (cd /dest/directory &&... (1 Reply)
Discussion started by: gwgreen1
1 Replies
Login or Register to Ask a Question