I have a pipe delimted file
d1000|1000
d1001|100
d1002|10
d1003|1
d1004|
d1005|
I want to modify $2 if length is less than 4 digits, and also keep empty spaces as is
so trying to do it via awk script
BEGIN { FS="|"; OFS="\t" }
{
n=1100
{ if (length($2)!=4 && length($2)>0) {$2=++n}};
print $1, $2
}
but it's printing same number over & over
d1000 1000
d1001 1101
d1002 1101
d1003 1101
d1004
d1005
whereas desired output
d1000 1000
d1001 1101
d1002 1102
d1003 1103
d1004
d1005
EDIT: here is the above code formatted legibly by gawk -o-
:
BEGIN {
FS = "|"
OFS = "\t"
}
{
n = 1100
if (length($2) != 4 && length($2) > 0) {
$2 = ++n
}
print $1, $2
}