[英]Grep data from a file bash shell script
I have file named shadow.lab4 which contain below characters and stored in desktop: 我有一个名为shadow.lab4的文件,其中包含以下字符并存储在桌面中:
$6$bIhKGKp3$LSd47ADZexr.4rBm8y29DLPfd1kxwyuliCea8fExg0ohMT25OAEqUOxKm7t6dj/M50PjACjD.gn.VDD8f4MVy0
Now I am trying to retrieve the encrypted data by using grep command and store it inside variable encr. 现在我尝试使用grep命令检索加密数据并将其存储在变量encr中。 Then show retrieved data on the screen by using
然后使用在屏幕上显示检索到的数据
echo $encr
My expected output should be 我的预期输出应该是
LSd47ADZexr.4rBm8y29DLPfd1kxwyuliCea8fExg0ohMT25OAEqUOxKm7t6dj/M50PjACjD.gn.VDD8f4MVy0
Do you know code that I have to use to get my expected output by using 'grep'? 您是否知道我必须使用'grep'来获取预期输出的代码?
I don't know why you use ":"
as a separator for cut
, but there's no colon at all in your input string. 我不知道为什么你使用
":"
作为cut
的分隔符,但你的输入字符串中根本没有冒号。 Change the cut part of the script to 将脚本的剪切部分更改为
cut -d '$' -f 4
ENCR: ENCR:
If the encryption is always the 4th field in the string: 如果加密始终是字符串中的第4个字段:
encr=$(awk -F "$" '{ print $4 }' shadow.lab4)
If the encryption is always the last field in the string: 如果加密始终是字符串中的最后一个字段:
encr=$(awk -F "$" '{ print $NF }' shadow.lab4)
Results: 结果:
echo "$encr"
LSd47ADZexr.4rBm8y29DLPfd1kxwyuliCea8fExg0ohMT25OAEqUOxKm7t6dj/M50PjACjD.gn.VDD8f4MVy0
SALT: 盐:
To access the salt, if it's always the third field: 要访问salt,如果它始终是第三个字段:
salt=$(awk -F "$" '{ print $3 }' shadow.lab4)
To access the salt, if it's always the second last field: 要访问salt,如果它始终是倒数第二个字段:
salt=$(awk -F "$" '{ print $(NF-1) }' shadow.lab4)
Results: 结果:
echo "$salt"
bIhKGKp3
sed
方式:
encr = `sed 's/.*\$//' file.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.