1833189 Members
2945 Online
110051 Solutions
New Discussion

Re: AWK script

 
SOLVED
Go to solution
gaudiobe
Advisor

AWK script

Hello to all,
I created an awk script long time ago, now i need to modify this script inserting a new operationinside it.

the source file is something like this:
adjksfhluawe='string1_to_encrypt'afjskdasndkajcfc
asjrhquwerfb='string2_to_encrypt'sfjsejkbghbvzjhs
....

The new command porpose is to encrypt a string inside the original file.

I have already created an external shell script that allow me to encrypt the string.
The shell script work like this:
ShellEncrypt.sh string_to_encrypt
The output of the script will bee an ecrypted same size string.
eg.
ShellEncrypt.sh text
the output of the file will be a string like:
A3rA

I tried to use the system function inside the awk script and this partially works.

Will follow my awk program:

prog.awk script:
#########################
BEGIN { FS = "'" }
$0 ~ /'/ {
printf ($1 "'");
system("ShellEncrypt.sh "$2"");
printf ("'" $3);
}
#########################

and awk command line:
# awk -f prog.awk source.txt


Unfortunately the result will be not the expected one because after the ecryption of the string with the system command
I have the following output:
adjksfhluawe='ecrypted_string_1_
'afjskdasndkajcfc
asjrhquwerfb='ecrypted_string_2_
'sfjsejkbghbvzjhs

Instead my expected output was:
adjksfhluawe='ecrypted_string_1_'afjskdasndkajcfc
asjrhquwerfb='ecrypted_string_2_'sfjsejkbghbvzjhs

Can you help me to find a solution?

Thanks in advance for your help.
Regards
Bernardo
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: AWK script

Your script also prints out a newline.
You might be able to suppress it with:
system("X=$(ShellEncrypt.sh " $2 "); echo $X\\c")
(You may have to add more backslashes.)
gaudiobe
Advisor

Re: AWK script

I tryed the solution suggested but it does not fix my problem.

for example:
result expected:
adjksfhluawe='ecrypted_string_1_'afjskdasndkajcfc

test 1
system("X=$(ShellEncrypt.sh " $2 "); echo $X\\c"

result:
adjksfhluawe='ecrypted_string_c_
'afjskdasndkajcfc

test 2
system("X=$(ShellEncrypt.sh " $2 "); echo $X\\\c"

result:
adjksfhluawe='ecrypted_string_'afjskdasndkajcfc

test 3
system("X=$(ShellEncrypt.sh " $2 "); echo $X\\\\c"

result:
adjksfhluawe='ecrypted_string_'afjskdasndkajcfc





gaudiobe
Advisor

Re: AWK script

Hello Dennis,
your suggestion works correctly.
I found a mistake in the ShellEncrypt.sh script.

I corrected it and now it works.

Dennis Handly
Acclaimed Contributor
Solution

Re: AWK script

>but it does not fix my problem.
adjksfhluawe='ecrypted_string_'afjskdasndkajcfc
adjksfhluawe='ecrypted_string_1_'afjskdasndkajcfc

You need to explain why these aren't the same? (I assumed these were produced by your script.)
Do you want the awk script to number each output with "_N_"?
gaudiobe
Advisor

Re: AWK script

Hello Dennis,
Sorry for the previous reply.
Your suggestion works correctly.
I found a mistake in the ShellEncrypt.sh script.

I corrected it and now it works.
gaudiobe
Advisor

Re: AWK script

As suggested by Dennis I used:
system("X=$(ShellEncrypt.sh " $2 "); echo $X\\\c")

Regards,
Bernardo