0

I'm trying to understand the grep command:

#CPU physical: $(lscpu -b -p=Socket | grep -v '^#' | sort -u | wc -l)

and it seems quite understandable, the only thing I couldn't get is the control (^) before the hash (#)?

4
  • lines beginning with # - i.e. ^ means beginning of line
    – Bravo
    Dec 29, 2021 at 21:57
  • many thanks, do you suggest any reference to get through in this regards Dec 29, 2021 at 22:28
  • start with man grep ... then follow the documentation suggestions for patterns within than man page
    – Bravo
    Dec 29, 2021 at 22:41
  • I'd suggest you google for "regular expression tutorial" Dec 29, 2021 at 22:57

2 Answers 2

2

You can think of the ^ character as meaning "beginning of line", so ^# matches a # at the beginning of a line - in other words, a line that is a comment.

By using the -v option to grep, you select non-matching lines.

In effect, in this case, the command filters out comments.

See here for more information on regexes in Bash.

0

You should try execute the command without the grep -v command and see the difference.

grep -v "^#" - Removes all comments from the lscpu ouput.

Invert the sense of matching, to select non-matching line, below you have an example of how you can get the same answer without filtering output:

 egrep "physical id" /proc/cpuinfo|sort -u|wc -l

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.