简体   繁体   English

bash脚本的awk部分无法正常工作

[英]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. 在ubuntu上将脚本用作#/bin/sh时,一切似乎都很好,但是现在在Red Hat上将同一脚本与#/bin/bash ,这会造成问题。

   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... 假设$padlen=1$length=13这些分别是输出...

[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 . 在最后三个字节中为00 01 11 the 00 is padding and 01 is padding length. 00是填充,而01是填充长度。

[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 ... 或者Atleast建议我一些替代方案...

EDIT: * 编辑: *

This is another sample file where the suggested solution not working out assuming $_padlen=3 and $_length=1159 . 这是另一个示例文件,假设$_padlen=3$_length=1159 ,建议的解决方案$_padlen=3

 [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 l可以简单地计算为wc -w decrypt.txt|cut -d " " -f1p2

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: 对于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: 对于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。 NF is number of fields on a particular record (which by default is a line). NF是特定记录(默认情况下是一行)上的字段数。

pad.txt 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 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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