This is the script I’m trying to add a last argument to that prints the FNR into the fourth field.
#!/usr/bin/awk -f
{ sub(/\r$/,"") }
/^BEGIN_DATA_FORMAT/{
getline
for (i=1;i<=NF;i++)
if ($i~/LAB/) a[i]=$i
}
/^BEGIN_DATA$/,/^END_DATA$/{
s="";
if (NF<2) next; else
for (j in a)
s=s?s"\t"$j:$j
print s
}
This is what the output from this script looks like:
48.34 -55.88 19.19
26.95 24.36 13.43
25.53 4.45 -20.68
71.27 6.68 24.28
...
This is my second script:
#!/usr/bin/awk -f
{ OFS = "\t"; $4="(Untitled "FNR-1")"; print $0 }
Piping the first script into the second script returns the intended result with the FNR starting from 0 in the fourth field.
48.34 -55.88 19.19 (Untitled 0)
26.95 24.36 13.43 (Untitled 1)
25.53 4.45 -20.68 (Untitled 2)
71.27 6.68 24.28 (Untitled 3)
...
I tried combining the scripts but I don’t get the output I’m trying for.
#!/usr/bin/awk -f
{ sub(/\r$/,"") }
/^BEGIN_DATA_FORMAT/{
getline
for (i=1;i<=NF;i++)
if ($i~/LAB/) a[i]=$i
}
/^BEGIN_DATA$/,/^END_DATA$/{
s="";
if (NF<2) next; else
for (j in a)
s=s?s"\t"$j:$j
print s
}
{
OFS = "\t"
$4="(Untitled "FNR-1")"
print $4
}
This is the output given. The problem is that it’s referencing the same file instead of the output from the last command.
(Untitled 0)
(Untitled 1)
(Untitled 2)
(Untitled 3)
(Untitled 4)
(Untitled 5)
(Untitled 6)
(Untitled 7)
(Untitled 8)
(Untitled 9)
(Untitled 10)
(Untitled 11)
(Untitled 13)
(Untitled 14)
(Untitled 15)
48.34 -55.88 19.19
(Untitled 17)
26.95 24.36 13.43
(Untitled 18)
25.53 4.45 -20.68
(Untitled 19)
71.27 6.68 24.28
(Untitled 20)
...
I’ve also tried this:
#!/usr/bin/awk -f
{ sub(/\r$/,"") }
/^BEGIN_DATA_FORMAT/{
getline
for (i=1;i<=NF;i++)
if ($i~/LAB/) a[i]=$i
}
/^BEGIN_DATA$/,/^END_DATA$/{
s="";
if (NF<2) next; else
for (j in a)
s=s?s"\t"$j:$j
OFS = "\t"
$4="(Untitled "FNR-1")"
print s OFS $4
}
The output is closer but the problem is that it’s still counting from the FNR of that argument. I need it to start at 0.
48.34 -55.88 19.19 (Untitled 17)
26.95 24.36 13.43 (Untitled 18)
25.53 4.45 -20.68 (Untitled 19)
71.27 6.68 24.28 (Untitled 20)
...
Could someone show me the correct way to combine these scripts?
Just use an incrementing variable rather than the record number:
You should define
OFSin theBEGINblock instead of redefining it for each line.Instead of doing
{ sub(/\r$/,"") }why don’t you use “dos2unix” on your file first?