muhihsan
muhihsan

Reputation: 2350

Grep data from a file bash shell script

I have file named shadow.lab4 which contain below characters and stored in desktop:

$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. 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'?

Upvotes: 1

Views: 904

Answers (3)

Steve
Steve

Reputation: 54592

ENCR:

If the encryption is always the 4th field in the string:

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=$(awk -F "$" '{ print $3 }' shadow.lab4)

To access the salt, if it's always the second last field:

salt=$(awk -F "$" '{ print $(NF-1) }' shadow.lab4)

Results:

echo "$salt"
bIhKGKp3

Upvotes: 1

Lev Levitsky
Lev Levitsky

Reputation: 65871

sed way:

encr = `sed 's/.*\$//' file.txt

Upvotes: 0

user529758
user529758

Reputation:

I don't know why you use ":" as a separator for cut, but there's no colon at all in your input string. Change the cut part of the script to

cut -d '$' -f 4

Upvotes: 3

Related Questions