简体   繁体   中英

awk part of bash script not working properly

Everything seems fine when using script as #/bin/sh on ubuntu but now on using the same script with #/bin/bash on red hat this part is creating problem.

   awk '{

    for (i = NF  - p - 2; i < NF  - 2; i++)

    printf "%s", ($i (i < NF - 2 - 1 ? OFS : ORS))

    }' p="$_padlen" RS=  ORS='\n' decrypt.txt > pad.txt

also this one ..

   awk '{

    for (i = NF - l - p - 2; i < NF - p - 2; i++)

    printf "%s", ($i (i < NF - p - 2 - 1 ? OFS : ORS))

    }' l="$_length" p="$_padlen" RS=  ORS='\n' decrypt.txt > prot_n_data.txt

assuming $padlen=1 and $length=13 these are the respective outputs...

[root@localhost scripts]# cat decrypt.txt 
00 15 00 15 00 0d dc ff  61 62 63 64 0a 00 01 11

Among last three bytes which are 00 01 11 . the 00 is padding and 01 is padding length.

[root@localhost scripts]# cat pad.txt
0a

[root@localhost scripts]# cat prot_n_data.txt 
00 15 00 15 00 0d dc ff  61 62 63 64 0a 00 01 11 00 15 00 15 00 0d dc ff 
61 62 63 64

While the desired output is ...

# cat pad.txt
00

# cat prot_n_data.txt 
00 15 00 15 00 0d dc ff  61 62 63 64 0a

Completely stucked getting no clue ...Please help me .

OR Atleast suggest me some alternative ...

EDIT: *

This is another sample file where the suggested solution not working out assuming $_padlen=3 and $_length=1159 .

 [root@localhost scripts]# cat decrypt.txt 
 00 17 00 17 04 87 5d c5  74 68 69 73 20 69 73 20
 73 61 6d 70 6c 65 20 64  61 74 61 20 74 6f 20 73
 65 6e 64 0a 77 65 20 73  68 6f 75 6c 64 20 63 6f
 6e 63 65 6e 74 72 61 74  65 20 6f 6e 20 69 74 2e
 0a 74 68 69 73 20 69 73  20 73 61 6d 70 6c 65 20
 64 61 74 61 20 74 6f 20  73 65 6e 64 0a 77 65 20
 73 68 6f 75 6c 64 20 63  6f 6e 63 65 6e 74 72 61
 74 65 20 6f 6e 20 69 74  2e 0a 74 68 69 73 20 69
 73 20 73 61 6d 70 6c 65  20 64 61 74 61 20 74 6f
 20 73 65 6e 64 0a 77 65  20 73 68 6f 75 6c 64 20
 63 6f 6e 63 65 6e 74 72  61 74 65 20 6f 6e 20 69
 74 2e 0a 74 68 69 73 20  69 73 20 73 61 6d 70 6c
 65 20 64 61 74 61 20 74  6f 20 73 65 6e 64 0a 77
 65 20 73 68 6f 75 6c 64  20 63 6f 6e 63 65 6e 74
 72 61 74 65 20 6f 6e 20  69 74 2e 0a 74 68 69 73
 20 69 73 20 73 61 6d 70  6c 65 20 64 61 74 61 20
 74 6f 20 73 65 6e 64 0a  77 65 20 73 68 6f 75 6c
 64 20 63 6f 6e 63 65 6e  74 72 61 74 65 20 6f 6e
 20 69 74 2e 0a 74 68 69  73 20 69 73 20 73 61 6d
 70 6c 65 20 64 61 74 61  20 74 6f 20 73 65 6e 64
 0a 77 65 20 73 68 6f 75  6c 64 20 63 6f 6e 63 65
 6e 74 72 61 74 65 20 6f  6e 20 69 74 2e 0a 74 68
 69 73 20 69 73 20 73 61  6d 70 6c 65 20 64 61 74
 61 20 74 6f 20 73 65 6e  64 0a 77 65 20 73 68 6f
 75 6c 64 20 63 6f 6e 63  65 6e 74 72 61 74 65 20
 6f 6e 20 69 74 2e 0a 74  68 69 73 20 69 73 20 73
 61 6d 70 6c 65 20 64 61  74 61 20 74 6f 20 73 65
 6e 64 0a 77 65 20 73 68  6f 75 6c 64 20 63 6f 6e
 63 65 6e 74 72 61 74 65  20 6f 6e 20 69 74 2e 0a
 74 68 69 73 20 69 73 20  73 61 6d 70 6c 65 20 64
 61 74 61 20 74 6f 20 73  65 6e 64 0a 77 65 20 73
 68 6f 75 6c 64 20 63 6f  6e 63 65 6e 74 72 61 74
 65 20 6f 6e 20 69 74 2e  0a 74 68 69 73 20 69 73
 20 73 61 6d 70 6c 65 20  64 61 74 61 20 74 6f 20
 73 65 6e 64 0a 77 65 20  73 68 6f 75 6c 64 20 63
 6f 6e 63 65 6e 74 72 61  74 65 20 6f 6e 20 69 74
 2e 0a 74 68 69 73 20 69  73 20 73 61 6d 70 6c 65
 20 64 61 74 61 20 74 6f  20 73 65 6e 64 0a 77 65
 20 73 68 6f 75 6c 64 20  63 6f 6e 63 65 6e 74 72
 61 74 65 20 6f 6e 20 69  74 2e 0a 74 68 69 73 20
 69 73 20 73 61 6d 70 6c  65 20 64 61 74 61 20 74
 6f 20 73 65 6e 64 0a 77  65 20 73 68 6f 75 6c 64
 20 63 6f 6e 63 65 6e 74  72 61 74 65 20 6f 6e 20
 69 74 2e 0a 74 68 69 73  20 69 73 20 73 61 6d 70
 6c 65 20 64 61 74 61 20  74 6f 20 73 65 6e 64 0a
 77 65 20 73 68 6f 75 6c  64 20 63 6f 6e 63 65 6e
 74 72 61 74 65 20 6f 6e  20 69 74 2e 0a 74 68 69
 73 20 69 73 20 73 61 6d  70 6c 65 20 64 61 74 61
 20 74 6f 20 73 65 6e 64  0a 77 65 20 73 68 6f 75
 6c 64 20 63 6f 6e 63 65  6e 74 72 61 74 65 20 6f
 6e 20 69 74 2e 0a 74 68  69 73 20 69 73 20 73 61
 6d 70 6c 65 20 64 61 74  61 20 74 6f 20 73 65 6e
 64 0a 77 65 20 73 68 6f  75 6c 64 20 63 6f 6e 63
 65 6e 74 72 61 74 65 20  6f 6e 20 69 74 2e 0a 74
 68 69 73 20 69 73 20 73  61 6d 70 6c 65 20 64 61
 74 61 20 74 6f 20 73 65  6e 64 0a 77 65 20 73 68
 6f 75 6c 64 20 63 6f 6e  63 65 6e 74 72 61 74 65
 20 6f 6e 20 69 74 2e 0a  74 68 69 73 20 69 73 20
 73 61 6d 70 6c 65 20 64  61 74 61 20 74 6f 20 73
 65 6e 64 0a 77 65 20 73  68 6f 75 6c 64 20 63 6f
 6e 63 65 6e 74 72 61 74  65 20 6f 6e 20 69 74 2e
 0a 74 68 69 73 20 69 73  20 73 61 6d 70 6c 65 20
 64 61 74 61 20 74 6f 20  73 65 6e 64 0a 77 65 20
 73 68 6f 75 6c 64 20 63  6f 6e 63 65 6e 74 72 61
 74 65 20 6f 6e 20 69 74  2e 0a 74 68 69 73 20 69
 73 20 73 61 6d 70 6c 65  20 64 61 74 61 20 74 6f
 20 73 65 6e 64 0a 77 65  20 73 68 6f 75 6c 64 20
 63 6f 6e 63 65 6e 74 72  61 74 65 20 6f 6e 20 69
 74 2e 0a 74 68 69 73 20  69 73 20 73 61 6d 70 6c
 65 20 64 61 74 61 20 74  6f 20 73 65 6e 64 0a 77
 65 20 73 68 6f 75 6c 64  20 63 6f 6e 63 65 6e 74
 72 61 74 65 20 6f 6e 20  69 74 2e 0a 0a 0a 0a 0a
 0a 0a 0a 0a 0a 0a 0a 00  00 00 03 11

The command I used is ..

 [root@localhost scripts]# awk '{printf $0}' decrypt.txt | awk 'NF{for (i=NF-l-p-1;
 i<NF-p-1;i++) printf $i" "; print ""}' p=3 l=1159
 awk: (FILENAME=- FNR=1) fatal: attempt to access field -71

l is simply calculated as wc -w decrypt.txt|cut -d " " -f1 minus p minus 2

Try this (you might have to test it against few more samples)

[jaypal~/Temp]$ cat file.txt 
00 15 00 15 00 0d dc ff  61 62 63 64 0a 00 01 11

For Pad.txt:

[jaypal~/Temp]$ awk 'NF{for (i=NF-p-1;i<NF-1;i++) printf $i; print ""}' p=1 file.txt 
00

For Prot_n_data.txt:

[jaypal~/Temp]$ awk 'NF{for (i=NF-l-p-1;i<NF-p-1;i++) printf $i" "; print ""}' p=1 l=13 file.txt 
00 15 00 15 00 0d dc ff 61 62 63 64 0a 

UPDATED:

Since your feed file overlaps into a new line, NF needs to be handled differently. NF is number of fields on a particular record (which by default is a line).

pad.txt

awk 'BEGIN{RS=""} {for (i=NF-p-1;i<NF-1;i++) printf $i" "; print ""}' p=1 file.txt | sed 's/..\{47\}/&\n/g'

prot_n_data.txt

awk 'BEGIN{RS=""} {for (i=NF-l-p-1;i<NF-p-1;i++) printf $i" "; print ""}' p=3 l=1159 file1.txt | sed 's/..\{47\}/&\n/g'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM