Domanda

I want to do some manipulation on a file with values coded by ASCII symbols.

I have a test file that looks something like this:

a>
b!

I would like to use an awk script to to convert them to integer values and then do further manipulation. I've looked and found a method of converting ASCII to integer:

for(n=0;n<256;n++)chr[n]=sprintf("%c",n)

but I don't know how to pass my integer values from this array to another array wherein I want to do my numeric manipulation.

Example output would be:

195
95

I can get it to work if I use a test file like this (without incorporation of the above code):

11
22

and the code:

cat testfile.txt | awk 'BEGIN {FS=""} {for (i=1;i<=NF;i++) ar[i]=ar[i]+$i} 
END {for(y=1;y<=length(ar);y++) print ar[y]}'

I get the desired result:

3
3

Does anyone have suggestions for how to convert the ASCII into an integer and then pass it to the rest of my awk statement?

È stato utile?

Soluzione

You can try:

awk 'BEGIN {
    FS=""
    for(n=0;n<256;n++)ord[sprintf("%c",n)]=n
}
{
    for (i=1;i<=NF;i++) ar[i]=ar[i]+ord[$i]
} 

END {
    for(y=1;y<=length(ar);y++) print ar[y]
}' file

Output:

195
95
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top