Sequence generation in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sequence generation in shell
# 1  
Old 12-10-2014
Sequence generation in shell

Hi,

I am using the nested for loops to generate the sequence ,
taking start and end sequence number input from test.txt
(sample content mentioned below).
Also , can I print the rest of columns as well with sequence number into the same file.

Code:
for i in `cat test.txt|cut -d"," -f7` 
do 
for j in `cat test.txt|cut -d"," -f8` 
do
 for k in $(seq $i $j) 
do
 echo $k >>sequence.txt 
done 
done
 done

Please guide how to correct the above code for the mentioned requirement.

test.txt
Code:
HPAALI41215,Aalia Enterprises HP,145010,01-JUL-14,,C,4021375283,4021375682 HPAALI41215,Aalia Enterprises HP,145010,01-NOV-14,,C,4019669798,4019670032 
HPAALI41215,Aalia Enterprises HP,145010,01-NOV-14,,C,4021949831,4021950830 HPAALI41215,Aalia Enterprises HP,145010,01-NOV-14,,C,4022105356,4022106355


Last edited by Don Cragun; 12-10-2014 at 05:14 AM.. Reason: Add CODE tags.
# 2  
Old 12-10-2014
What are you expecting the commands:
Code:
seq 4021375283 4021375682
seq 4021375283 HPAALI41215
seq 4021375283 4021950830
seq 4021375283 HPAALI41215
seq 4021949831 4021375682
seq 4021949831 HPAALI41215
seq 4021949831 4021950830
seq 4021949831 HPAALI41215

to do?

What output are you trying to produce?
# 3  
Old 12-10-2014
Desired output is the sequence , where start number is the field 7 and end number is field 8 in the input file.
e.g >> start number - 4021375283 and end number - 4021375682
After this , start and end number will be from next line having same fields 7 and 8
Output will be -
Code:
4021375283       
4021375284       
4021375285       
4021375286       
4021375287       
4021375288       
4021375289
----till last number--------
4021375682
4019669798
4019669799
4019670032

Input file has the content mentioned below having 4 rows.
Code:
HPAALI41215,Aalia Enterprises HP,145010,01-JUL-14,,C,4021375283,4021375682 
HPAALI41215,Aalia Enterprises HP,145010,01-NOV-14,,C,4019669798,4019670032 
HPAALI41215,Aalia Enterprises HP,145010,01-NOV-14,,C,4021949831,4021950830 
HPAALI41215,Aalia Enterprises HP,145010,01-NOV-14,,C,4022105356,4022106355


Last edited by Scrutinizer; 12-10-2014 at 07:33 AM..
# 4  
Old 12-10-2014
Please use code tags as required by forum rules!

NOT using them leads to misconceptions as in your post #1.

Try
Code:
cut -d"," -f7,8 < file4 | tr ',' ' ' | while read A B; do echo seq $A $B; done
seq 4021375283 4021375682
seq 4019669798 4019670032
seq 4021949831 4021950830
seq 4022105356 4022106355

and remove the echo if happy. With a recent bash, you can try
Code:
while read A B; do echo seq $A $B; done < <(cut -d"," -f7,8 < file4 | tr ',' ' ')

# 5  
Old 12-10-2014
Thanks RudiC and I apologize for not using CODE button
Output required is different.
I have a range of numbers , where first number is always smaller than the second.

Code:
4021375283,4021375682
4019669798,4019670032
4021949831,4021950830
4022105356,4022106355

I need to break it into a output sequence as

Code:
4021375283  -- field 7 of first row
4021375284
4021375285
4021375286
4021375287
4021375288
4021375289
4021375290
4021375291
4021375292
4021375293
---continue-----
4021375682  --  field 8 of first row
4019669798  --   field 7 of second row
4019669799
4019669800
4019669801
4019669802
4019669803
4019669804
4019669805
4019669806
4019669807
4019669808
4019669809
---continue-----
401967003   ---  field 8 of second row
4019670033
 so on and exit


Last edited by tushar.modgil; 12-10-2014 at 07:34 AM.. Reason: correction
# 6  
Old 12-10-2014
just tweaking the above a bit

Code:
cut -d"," -f7,8 < file4 | tr ',' ' ' | while read A B; do `echo seq $A $B`; done

# 7  
Old 12-10-2014
Quote:
Originally Posted by r_t_1601
just tweaking the above a bit

Code:
cut -d"," -f7,8 < file4 | tr ',' ' ' | while read A B; do `echo seq $A $B`; done

Note that in the original sample input in this thread, the comma delimited field 7 and 8 for the 1st line is
Code:
4021375283,4021375682 HPAALI41215

not just 4021375283,4021375682. But, tweaking your suggestion a tiny bit more:
Code:
cut -d"," -f7,8 < file4 | tr ',' ' ' | while read A B junk; do `echo seq $A $B`; done

or getting rid of a process in the pipeline and the unneeded command substitution in the loop:
Code:
cut -d"," -f7,8 < file4 | while IFS=', ' read A B junk; do seq $A $B; done

might do what is wanted. I'm not sure that seq will print numbers in that range correctly. On some systems, the command:
Code:
seq 4021375283 4021375284

will print:
Code:
4.02138e+09
4.02138e+09

So, maybe you want something more like:
Code:
cut -d"," -f7,8 < file4 | while IFS=', ' read A B junk; do seq -f '%.0f' $A $B; done

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Dynamic file generation using shell

I have to generate the file dynamically from the source file based on the below control file. control_file.txt 1,3,5,-1,8,-1,4 The control file contain the position of column which i required from the source file, Example 1 column ,3 column ,5 column ,blank column(-1 indicates blank... (2 Replies)
Discussion started by: rspwilliam
2 Replies

3. 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

4. UNIX for Advanced & Expert Users

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... (9 Replies)
Discussion started by: panchalh
9 Replies

5. Shell Programming and Scripting

Shell scripting for this sequence

KINDLY HELP ME FOR SHELL SCRIPTING FOR THIS TASK. My input file consists of thousands of sequence in this format. The given input file consists of four sequences which are starting with ‘>’ symbol (each sequence shown in different colour for easy understanding). I have to use a command at $... (3 Replies)
Discussion started by: kswapnadevi
3 Replies

6. Shell Programming and Scripting

Shell scripting for this sequence to compare

I have two input files (given below) and to compare each line of the File1 with each line of File2 starts with '>sample1'. If a match occurs and that matched line in the File2 contains another line or sequence of lines starting with "Chr" they have to be displayed in output file with that sample.... (4 Replies)
Discussion started by: hravisankar
4 Replies

7. Shell Programming and Scripting

Shell scripting : Help Me for this sequence

I have two input files (given below) and to compare each line of the File1 with each line of File2 starts with '>sample1'. If a match occurs and that matched line in the File2 contains another line or sequence of lines starting with "Chr" they have to be displayed in output file with that sample.... (9 Replies)
Discussion started by: hravisankar
9 Replies

8. Shell Programming and Scripting

Monitor file generation for an hour using Korn shell script

Hi, I am new to this unix scripting, I got a requirement like.. Files with *.XML extension will be generating in a /home/sample/ folder for every 15 mins. I need to monitor those files in that particular folder for every hour. If no file has been generated in that particular folder for an... (7 Replies)
Discussion started by: siri_886
7 Replies

9. Shell Programming and Scripting

ID generation in sequence

i have an xml tag. The value for the tag should be iterated through out the xml document. eg: <data><id><id><name>a</name><addr>aaa</addr><phnumb>3456</phnumb><state>ca</state><city>ny</city></data>... (6 Replies)
Discussion started by: Sgiri1
6 Replies

10. Shell Programming and Scripting

Sequence number generation on a key column

Hi Folks, Can you help me with this issue: I have to generate the numbers say from 1001 for each record in a file based on a key field, the catch is the generated number should be unique based on key column. (EMP_NUMBER) Example: Input File: EMP_NUMBER EMP_NAME 8908 ... (6 Replies)
Discussion started by: sbasetty
6 Replies
Login or Register to Ask a Question