Bash script to read line and increment
up vote
0
down vote
favorite
I have a file that has the following format
1|3
7|10
11|16
and I would like with a script to have the following format
1
2
3
7
8
9
10
11
12
13
14
15
16
Basically I have a range and in the first column is the start number and in the second column the end number and I would liked to have the output file with all the numbers from that range in a new line .
bash shell
New contributor
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I have a file that has the following format
1|3
7|10
11|16
and I would like with a script to have the following format
1
2
3
7
8
9
10
11
12
13
14
15
16
Basically I have a range and in the first column is the start number and in the second column the end number and I would liked to have the output file with all the numbers from that range in a new line .
bash shell
New contributor
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
There seems to be several questions here: Which bit are you stuck on?
– ctrl-alt-delor
Nov 22 at 9:54
I don't even have any kind of idea on how to start .
– bboy
Nov 22 at 10:39
Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
– ctrl-alt-delor
Nov 22 at 10:47
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file that has the following format
1|3
7|10
11|16
and I would like with a script to have the following format
1
2
3
7
8
9
10
11
12
13
14
15
16
Basically I have a range and in the first column is the start number and in the second column the end number and I would liked to have the output file with all the numbers from that range in a new line .
bash shell
New contributor
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a file that has the following format
1|3
7|10
11|16
and I would like with a script to have the following format
1
2
3
7
8
9
10
11
12
13
14
15
16
Basically I have a range and in the first column is the start number and in the second column the end number and I would liked to have the output file with all the numbers from that range in a new line .
bash shell
bash shell
New contributor
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 22 at 10:28
Rui F Ribeiro
38.3k1475126
38.3k1475126
New contributor
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 22 at 9:52
bboy
274
274
New contributor
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
There seems to be several questions here: Which bit are you stuck on?
– ctrl-alt-delor
Nov 22 at 9:54
I don't even have any kind of idea on how to start .
– bboy
Nov 22 at 10:39
Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
– ctrl-alt-delor
Nov 22 at 10:47
add a comment |
There seems to be several questions here: Which bit are you stuck on?
– ctrl-alt-delor
Nov 22 at 9:54
I don't even have any kind of idea on how to start .
– bboy
Nov 22 at 10:39
Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
– ctrl-alt-delor
Nov 22 at 10:47
There seems to be several questions here: Which bit are you stuck on?
– ctrl-alt-delor
Nov 22 at 9:54
There seems to be several questions here: Which bit are you stuck on?
– ctrl-alt-delor
Nov 22 at 9:54
I don't even have any kind of idea on how to start .
– bboy
Nov 22 at 10:39
I don't even have any kind of idea on how to start .
– bboy
Nov 22 at 10:39
Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
– ctrl-alt-delor
Nov 22 at 10:47
Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
– ctrl-alt-delor
Nov 22 at 10:47
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
With awk:
awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output
Perfect , thank you very much .
– bboy
Nov 22 at 10:40
This might be a tiny bit faster:{for (i=$1; i<=$2; i++) print i}as it doesn't have to rebuild $0 for each iteration of the loop.
– glenn jackman
Nov 22 at 14:26
@glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using theforloop is faster indeed, so I'm going to follow your recommendation.
– Stéphane Chazelas
Nov 22 at 14:37
add a comment |
up vote
2
down vote
tr -s | " " < file.txt |xargs -l1 seq > output.txt
Why the files?, stdin and stdout are more flexible.
– ctrl-alt-delor
Nov 22 at 9:55
In the question there was an input file and output also.
– Ipor Sircer
Nov 22 at 9:56
add a comment |
up vote
0
down vote
Assuming the values are strictly valid base 10 numbers (if not, clean them):
$ cat ./script.sh
#!/bin/bash
while IFS='|' read a b
do until ((a>b))
do printf '%dn' "$((a++))"
done
done
$ ./script <infile >outfile
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
With awk:
awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output
Perfect , thank you very much .
– bboy
Nov 22 at 10:40
This might be a tiny bit faster:{for (i=$1; i<=$2; i++) print i}as it doesn't have to rebuild $0 for each iteration of the loop.
– glenn jackman
Nov 22 at 14:26
@glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using theforloop is faster indeed, so I'm going to follow your recommendation.
– Stéphane Chazelas
Nov 22 at 14:37
add a comment |
up vote
1
down vote
accepted
With awk:
awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output
Perfect , thank you very much .
– bboy
Nov 22 at 10:40
This might be a tiny bit faster:{for (i=$1; i<=$2; i++) print i}as it doesn't have to rebuild $0 for each iteration of the loop.
– glenn jackman
Nov 22 at 14:26
@glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using theforloop is faster indeed, so I'm going to follow your recommendation.
– Stéphane Chazelas
Nov 22 at 14:37
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
With awk:
awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output
With awk:
awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output
edited Nov 22 at 14:38
answered Nov 22 at 10:03
Stéphane Chazelas
295k54556898
295k54556898
Perfect , thank you very much .
– bboy
Nov 22 at 10:40
This might be a tiny bit faster:{for (i=$1; i<=$2; i++) print i}as it doesn't have to rebuild $0 for each iteration of the loop.
– glenn jackman
Nov 22 at 14:26
@glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using theforloop is faster indeed, so I'm going to follow your recommendation.
– Stéphane Chazelas
Nov 22 at 14:37
add a comment |
Perfect , thank you very much .
– bboy
Nov 22 at 10:40
This might be a tiny bit faster:{for (i=$1; i<=$2; i++) print i}as it doesn't have to rebuild $0 for each iteration of the loop.
– glenn jackman
Nov 22 at 14:26
@glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using theforloop is faster indeed, so I'm going to follow your recommendation.
– Stéphane Chazelas
Nov 22 at 14:37
Perfect , thank you very much .
– bboy
Nov 22 at 10:40
Perfect , thank you very much .
– bboy
Nov 22 at 10:40
This might be a tiny bit faster:
{for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.– glenn jackman
Nov 22 at 14:26
This might be a tiny bit faster:
{for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.– glenn jackman
Nov 22 at 14:26
@glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the
for loop is faster indeed, so I'm going to follow your recommendation.– Stéphane Chazelas
Nov 22 at 14:37
@glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the
for loop is faster indeed, so I'm going to follow your recommendation.– Stéphane Chazelas
Nov 22 at 14:37
add a comment |
up vote
2
down vote
tr -s | " " < file.txt |xargs -l1 seq > output.txt
Why the files?, stdin and stdout are more flexible.
– ctrl-alt-delor
Nov 22 at 9:55
In the question there was an input file and output also.
– Ipor Sircer
Nov 22 at 9:56
add a comment |
up vote
2
down vote
tr -s | " " < file.txt |xargs -l1 seq > output.txt
Why the files?, stdin and stdout are more flexible.
– ctrl-alt-delor
Nov 22 at 9:55
In the question there was an input file and output also.
– Ipor Sircer
Nov 22 at 9:56
add a comment |
up vote
2
down vote
up vote
2
down vote
tr -s | " " < file.txt |xargs -l1 seq > output.txt
tr -s | " " < file.txt |xargs -l1 seq > output.txt
answered Nov 22 at 9:54
Ipor Sircer
10.2k11024
10.2k11024
Why the files?, stdin and stdout are more flexible.
– ctrl-alt-delor
Nov 22 at 9:55
In the question there was an input file and output also.
– Ipor Sircer
Nov 22 at 9:56
add a comment |
Why the files?, stdin and stdout are more flexible.
– ctrl-alt-delor
Nov 22 at 9:55
In the question there was an input file and output also.
– Ipor Sircer
Nov 22 at 9:56
Why the files?, stdin and stdout are more flexible.
– ctrl-alt-delor
Nov 22 at 9:55
Why the files?, stdin and stdout are more flexible.
– ctrl-alt-delor
Nov 22 at 9:55
In the question there was an input file and output also.
– Ipor Sircer
Nov 22 at 9:56
In the question there was an input file and output also.
– Ipor Sircer
Nov 22 at 9:56
add a comment |
up vote
0
down vote
Assuming the values are strictly valid base 10 numbers (if not, clean them):
$ cat ./script.sh
#!/bin/bash
while IFS='|' read a b
do until ((a>b))
do printf '%dn' "$((a++))"
done
done
$ ./script <infile >outfile
add a comment |
up vote
0
down vote
Assuming the values are strictly valid base 10 numbers (if not, clean them):
$ cat ./script.sh
#!/bin/bash
while IFS='|' read a b
do until ((a>b))
do printf '%dn' "$((a++))"
done
done
$ ./script <infile >outfile
add a comment |
up vote
0
down vote
up vote
0
down vote
Assuming the values are strictly valid base 10 numbers (if not, clean them):
$ cat ./script.sh
#!/bin/bash
while IFS='|' read a b
do until ((a>b))
do printf '%dn' "$((a++))"
done
done
$ ./script <infile >outfile
Assuming the values are strictly valid base 10 numbers (if not, clean them):
$ cat ./script.sh
#!/bin/bash
while IFS='|' read a b
do until ((a>b))
do printf '%dn' "$((a++))"
done
done
$ ./script <infile >outfile
answered Nov 23 at 0:33
Isaac
9,72811445
9,72811445
add a comment |
add a comment |
bboy is a new contributor. Be nice, and check out our Code of Conduct.
bboy is a new contributor. Be nice, and check out our Code of Conduct.
bboy is a new contributor. Be nice, and check out our Code of Conduct.
bboy is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483402%2fbash-script-to-read-line-and-increment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There seems to be several questions here: Which bit are you stuck on?
– ctrl-alt-delor
Nov 22 at 9:54
I don't even have any kind of idea on how to start .
– bboy
Nov 22 at 10:39
Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
– ctrl-alt-delor
Nov 22 at 10:47