Bishop Minter
Bishop Minter

Reputation: 107

Need help reading a grep/sed/regex statement

Can someone please take the following statements:

kernelHash=$(grep -m 1 -o '"uImage-[0123456789.]*-ts-armv7l": [^, }]*' $SecureBootJson | sed 's/^.*: //')

kernelHash=$(grep -oP '"uImage-[0123456789.]*-ts-armv7l.*?\btype\b"' $SecureBootJson | cut -f3 -d ' ' | sed 's/,//')

and break them down for me and explain what they are supposed to accomplish?

Upvotes: 0

Views: 70

Answers (1)

Will Barnwell
Will Barnwell

Reputation: 4089

Both are setting a variable kernelHash based on the value of the file whose name/path is stored in the variable $SecureBootJson. I will assume you understand what the pipes | are doing.

Grep

man grep helps us parse the arguments to grep:

  • -m 1: Stop reading a file after 1 matching line
  • -o: Print only the matched (non-empty) parts of a matching line
  • -P: Interpret the regex as a Perl regex

Grep regexes

'"uImage-[0123456789.]*-ts-armv7l": [^, }]*'

Matches a "key": value pair

  1. "uImage-: literally
  2. [0123456789.]*: zero or more digits or dots
  3. -ts-armv7l":: literally
  4. [^, }]*: zero or more characters that are not ,, or }

and

'"uImage-[0123456789.]*-ts-armv7l.*?\btype\b"'

Matches a "quoted" string

  1. "uImage-: literally
  2. [0123456789.]*: zero or more digits or dots
  3. -ts-armv7l: literally
  4. .*?: lazily matches zero or more non-newline characters
  5. \btype\b": Matches the word 'type' using word boundaries

cut

cut -f3 -d ' '

From man cut:

  • -f3: return the 3rd field
  • -d ' ': split on single spaces

So this cut command splits the string on spaces and returns the 3rd section.

The sed commands

sed 's/^.*: //'

Replaces all non-newline characters between the beginning of the line the rightmost ': ' with nothing. Assuming the grep left us with a "key": value line where value does not contain ': '(which it cannot thanks to grep), we are left with value.

sed 's/,//'

Replaces the first , found in the string with nothing.

Note

There are a number of quirks and opportunities for improvement in these commands, but I have not included them in the main body of the answer, because the question was "what does this do?". My foremost suggestion would be the use of jq to parse Json.

Upvotes: 3

Related Questions