Value Increment based on count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Value Increment based on count
# 1  
Old 01-06-2012
Value Increment based on count

Hi All,

I have source file
x.txt
Code:
0001|0003
0031|0031
0045|0049

My desired output should be:
y.txt
Code:
0001
0002
0003
0031
0045
0046
0047
0048
0049

I had tried the below logic could you please help me out in this.....

Code:
 
srcpath = /temp
tgtpath= /temp
 
srcfilename=x.txt
tgtfilename=y.txt
 
cd $srcpath
 
filesrc=${srcpath}'/'${srcfilename}
filetgt=${tgtpath}'/'${tgtfilename}
 
i=1
while [ $i -le `wc -l ${filesrc} | awk '{print $1}'` ] ; do
LINE_NO = $(awk 'print NR' ${filesrc})
FROM = $(awk '{${LINE_NO}; print {$1}}' ${filesrc})
TO = $(awk '{${LINE_NO}; print {$2}}' ${filesrc})
if [ "$FROM" = "$TO" ]; then
 
$FROM >> ${filetgt}
else
while ( $FROM <= $TO) 
do 
$FROM + 1 >> ${filetgt}
done
fi
$LINE_NO+1
done

Thanks in advance
# 2  
Old 01-06-2012
Code:
while read x; do seq -f%04g `echo $x | cut -d'|' -f1` `echo $x | cut -d'|' -f2` >> y.txt; done < x.txt

Code:
# cat y.txt
0001
0002
0003
0031
0045
0046
0047
0048
0049

# 3  
Old 01-06-2012
Another approach:
Code:
awk -F"|" '{for(i=int($1);i<=int($2);i++)printf("%04d\n", i)}' file

# 4  
Old 01-06-2012
Hi kmsekhar,

One way using 'awk':
Code:
$ cat infile
0001|0003                                                                                                                                                                                                                                    
0031|0031                                                                                                                                                                                                                                    
0045|0049
$ cat script.awk
BEGIN {
    FS = "|" 
    OFS = "\n"
}

{
    diff = $2 - $1
    while ( diff >= 0 ) { 
        printf "%04i\n", $2 - (diff--)
    }   
}
$ awk -f script.awk infile
0001                                                                                                                                                                                                                                         
0002                                                                                                                                                                                                                                         
0003                                                                                                                                                                                                                                         
0031                                                                                                                                                                                                                                         
0045                                                                                                                                                                                                                                         
0046                                                                                                                                                                                                                                         
0047                                                                                                                                                                                                                                         
0048                                                                                                                                                                                                                                         
0049

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 5  
Old 01-06-2012
just another way
Code:
while IFS='|' read a b; do printf "%04d\n" $(seq $a $b); done <yourfile

# 6  
Old 01-06-2012
Thanks a lot for your solutions
# 7  
Old 01-06-2012
use

Code:
 
seq -w

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Best way to increment weeks based on fiscal start year

Hi Folks - I'm looking for the best way to to increment fiscal weeks - allow me to explain. At my one client, 10/01/17 was the beginning if year fiscal year 2018. Each week, I need to manage a unique set of variable that are updated in my application - they are called substitution variables.... (31 Replies)
Discussion started by: SIMMS7400
31 Replies

2. Shell Programming and Scripting

Create new file with increment column based on conditions

Hello, Using bash script, i need to process the following file: 887,86,,2013-11-06,1,10030,5,2,0,200,, 887,86,,2013-11-05,1,10030,5,2,0,199,, 887,138,,2013-11-06,1,10031,6,2,0,1610612736,, 887,164,,2013-11-06,1,10000,0,2,0,36000,, and to create a new file such as the below ... (2 Replies)
Discussion started by: JonhyDeep
2 Replies

3. Shell Programming and Scripting

Need to count based on date

i have a file with data as follows: 3/23/2014 22:01:18 1 3/23/2014 22:01:47 0 3/23/2014 22:03:35 0 3/23/2014 22:04:50 1 3/24/2014 1:16:41 0 3/24/2014 1:18:00 0 3/24/2014 1:18:36 0 3/24/2014 1:21:46 0 3/25/2014 13:00:31 1 3/25/2014 13:00:54 0 3/25/2014 13:13:09 0 3/25/2014 13:14:15 0... (2 Replies)
Discussion started by: ssk250
2 Replies

4. Shell Programming and Scripting

Count occurences based on interval

Hi, I have a file which has 4500 entries 10000 9880 9800 8700 8200 ... ..... ... ... ... ... ... ... ... 50 (1 Reply)
Discussion started by: Diya123
1 Replies

5. Shell Programming and Scripting

interval based count

Hi, I have a file with 4 million rows. what i am trying to achieve is for every 1000 interval count the number of rows and display it i/p 12 200 400 750 1000 1500 1800 2200 2345 2600 2896 3020 3400 (0 Replies)
Discussion started by: Diya123
0 Replies

6. UNIX for Dummies Questions & Answers

count the number of occurance and increment

Hi, I am trying to count the number of occurance of a specific value in a column and increment the variable in the second column accordingly. I have very little information about Unix. As an example, 21 1 32 1 32 2 45 1 56 1 56 2 56 3 73 1 82 1 Thanks, Natasha (2 Replies)
Discussion started by: natasha
2 Replies

7. Shell Programming and Scripting

Moving the files based on count and time.

Hi, I have a requirement ,let us say 1000 files needs to be transferred in an hour from one path to another path and if the files (1000 files) are transferred within an hour ( say 40 mins), then the process should remain idle for the remaining time ( 20 mins). (3 Replies)
Discussion started by: Asaikarthik
3 Replies

8. Shell Programming and Scripting

Dynamic Variable Based on Count

I'm trying to assign variables that include the current value of a count, but I can't seem to get it working... this script is incomplete, but some guidance on how to use a dynamic variable would be helpful: Sample Input: bash-2.03$ more sg2.txt Results for group6 443 1394 Results for... (3 Replies)
Discussion started by: earnstaf
3 Replies

9. Shell Programming and Scripting

Count of files based on date?

Hi Friends, Can anyone help me with this: To get the count of files that are existing in a directory created on a perticular date like in the example (01/08) .(having same pattern for the filename) ex: FileName Creted Date FILE001 01/08/2007 FILE005 ... (6 Replies)
Discussion started by: sbasetty
6 Replies
Login or Register to Ask a Question