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"
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)
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 ...
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:-
If the next number is in sequence and the sequence flag is not set, then set the flag, remembering the start point.
If the next number is in sequence and the sequence flag is set, do nothing.
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.
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:
If the sequence flag is not set, append the current number to the output.
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:-
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!)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)