0

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 .

  • There seems to be several questions here: Which bit are you stuck on? – ctrl-alt-delor Nov 22 '18 at 9:54
  • I don't even have any kind of idea on how to start . – bboy Nov 22 '18 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 '18 at 10:47
1

With awk:

awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output
  • 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 '18 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 '18 at 14:37
2
tr -s \| " " < file.txt |xargs -l1 seq > output.txt
  • Why the files?, stdin and stdout are more flexible. – ctrl-alt-delor Nov 22 '18 at 9:55
  • In the question there was an input file and output also. – Ipor Sircer Nov 22 '18 at 9:56
0

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 '%d\n' "$((a++))"
        done
done

$ ./script <infile >outfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.