Insert new lines with missing values (NA)
join
works well here:
join -a 1 -o 0,2.2 -e NA <(seq 12) A.txt 2>/dev/null
I throw away stderr because join
complains if the join field is not lexically sorted.
You can do this with an awk
script:
awk '{ while (NR + shift < $1) { print (NR + shift) " NA"; shift++ }; print } END { shift++; while (NR + shift < 13) { print (NR + shift) " NA"; shift++ } }' /tmp/test1
will produce the required output for /tmp/test1
(replace that with each file you wish to process).
In a more readable form:
#!/usr/bin/awk -f
{
while (NR + shift < $1) {
print (NR + shift) " NA"
shift++
}
print
}
END {
shift++
while (NR + shift < 13) {
print (NR + shift) " NA"
shift++
}
}
Save this as a file, say fill-missing
, make it executable, then you can simply run
./fill-missing /tmp/test1
The script processes each line, keeping track of the expected delta with the current line number in shift
. So for every line, if the current line adjusted doesn't match the first number in the line, it prints the appropriate line number followed by NA
and increments the delta; once the line numbers match, it prints the current line. At the end of the process, it prints any missing lines required to reach 12.
the awk file
BEGIN { i=1 ; }
function upto(x) { while (i<x) printf "%d NA\n",i++ ;}
{ if ( $1 == i ) print ; upto($1) ; i++ ;}
END { upto(final+1) ;}
to be called with
awk -f nl.awk -v final=12 /tmp/test1
your whole loop
cd /my/directory
ls | while read f
do
awk -f ~/nl.awk -v final=12 $f > /an/other/dir/$f
done
where
- you put awk program in your $HOME dir (
~/nl.awk
)