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 .

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

3 Answers 3

2
tr -s \| " " < file.txt |xargs -l1 seq > output.txt
2
  • Why the files?, stdin and stdout are more flexible. Nov 22, 2018 at 9:55
  • In the question there was an input file and output also. Nov 22, 2018 at 9:56
1

With awk:

awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output
2
  • 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. Nov 22, 2018 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. Nov 22, 2018 at 14:37
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .