awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the -
and last id in it, indicated by the digit after the - , I am trying to use awk
to print the missing line or lines in file following the pattern of the previous line.
For example, in the file below the next id to print using thr previous line would be RETT-02 ,
since the previous is RETT-01 . Since the last line in the file is 03 , that is
the only missing line, however the next time it may be different (with more lines).

The awk below works great as long as I enter the values to use, but is there a way to automaticaly set these? Thank you :).

RETT-01
RETT-03
awk '/RETT-03/{print "RETT-02"}1' file

RETT-01
RETT-02
RETT-03

If you know the first and last ID, why not generate the sequence yourself:

[user@host ~]$ seq -f "RETT-%.0f" 1 5
RETT-1
RETT-2
RETT-3
RETT-4
RETT-5
3 Likes

Try:

awk -F- 'NR>1 && $2>p+1{for(i=p+1; i<$2; i++) printf "%s-%02d\n",$1,i} {p=$2}1' file

---
variation, irrespective of the length of number..

awk -F- 'NR>1 && $2>p+1{for(i=p+1; i<$2; i++) printf "%s-%0*d\n",$1,length(p),i} {p=$2}1' file
1 Like

Hello cmccabe,

Could you please try following and let me know if this helps you.
Let's say we have following Input_file:

cat Input_file
RETT-01
RETT-03
RETT-11
RETT-13
RETT-33
RETT-34
RETT-34

Then following is the code for same.

awk -F"-" 'NR==1{print;val=$2;next} ($2-val>1){while($2-val>1){val++;printf("%s%02d\n",$1 FS, val);};print;val=$2;next} {val=$2;print}'  Input_file

Hello Scrutinizer,

Sorry my code looks very similar to you but I was on this page for quite sometime and didn't know you have replied already to it, though mine one of slightly different. Thought to let you know on same.

Thanks,
R. Singh

1 Like

Thank you all :slight_smile: