1

I have some directories, each of these contains a file with list of integers 1-N which are not necessarily consecutive and they may be different lengths. What I want to achieve is a single file with a list of all those integers as though they had been generated in one list.

What I am trying to do is to add the final value N from file 1 to all the values in file 2, then take the new final value of file 2 and add it to all the values in file 3 etc.

I have tried this by setting a counter and looping over the files, resetting the counter when I get to the end of the file. The problem is the p=0 will continue to reset which is kind of obvious in the code but I am not sure how else to do it.

What I tried:

p=0
for i in dirx/dir_*; do
        (cd "$i" || exit;
        awk -v p=$p 'NR>1{print last+p} {last=$0} END{$0=last; p=last; print}' file >> /someplace/bigfile)
done

Which is similar to the answer suggested in this question Replacing value in column with another value in txt file using awk

Now I'm wondering whether I need an if else, if it's the first dir then p=0 if not then p=last value from the first file though I'm not sure on that or how I'd get it to take the last value. I used awk because that's what I understand a small amount of and would usually use.

StormyTeacup
  • 115
  • 3
  • Are you in effect wanting a single file which contains a single list of integers, which is the result of concatinating the lists contained in the separate files? – 0xadecimal Sep 30 '19 at 14:06
  • Yes but they must follow on from the previous list, eg if the last number in the first file is 3000 and the first number in the second file is 2 the new list would have all the numbers from the first file including 3000 then go to 3002 etc. I can't just cat file1 file2. – StormyTeacup Sep 30 '19 at 14:19

1 Answers1

2

With GNU awk

gawk '{print $1 + last} ENDFILE {last = last + $1}' file ...

Demo:

$ cat a
1
2
4
6
8

$ cat b
2
3
5
7

$ cat c
1
2
3

$ gawk '{print $1 + last} ENDFILE {last = last + $1}' a b c
1
2
4
6
8
10
11
13
15
16
17
18
glenn jackman
  • 238,783
  • 38
  • 220
  • 352