Search code examples
regexbashshellawkgrep

How do I ignore a pipe symbol at the front?


How do I ignore forward slash and space at the start of the line in regular expressions?

In the Example below, I need to ignore the pipe and space because I am using grep and awk

The actual command gives me

cmd

size=5.0G features='0' hwhandler='0' wp=rw
|-+- policy='round-robin 0' prio=1 status=active
| `- 3:0:0:3   sdh  8:112   active ready running    #Line 3
`-+- policy='round-robin 0' prio=1 status=enabled
  `- 4:0:0:3   sdl  8:176   active ready running    #Line 5

By doing this:

cmd | grep -E '[0-9]+:[0-9]+:[0-9]+:[0-9]+' | awk '{print $3}'

I was able to get the sdh, sdl. But the problem is, I need to ignore the '|' upfront, to make the Line 3 and Line 5 same. Please advise.

Edit 1 I need to get two information

1) the Number

3:0:0:3
4:0:0:3

2) Disk name

sdh
sdl

Solution

  • Rather than try to make each of your two lines have identical number of fields, just use the -o option of grep to only part of the line that matches your regular expression. Then you won't need the awk command at all.

    cmd | grep -o -E '[0-9]+:[0-9]+:[0-9]+:[0-9]+'
    

    Since you actually need more than just what was in your original question:

    cmd | grep -E '[0-9]+:[0-9]+:[0-9]+:[0-9]+' | sed 's/^| //' | awk '{print $2, $3}'