user3144292
user3144292

Reputation: 412

bash script strip string matching pattern

In a script I have I'm piping a line from the dmesg system log. On certain distros the lines are logged with the timestamp in front of each line as well.

Take this output for example:

[   12.291091] BTRFS info (device sda2): disk space caching is enabled

On some distros the [] timestamp appears, while on other it's only:

BTRFS info (device sda2): disk space caching is enabled

Can someone please let me know a quick way to handle the removal of the [ time ] part and display that line without it? The tricky part I have is that on some distros it's not there, so I'm not sure how to handle that. Thank you!

Upvotes: 0

Views: 186

Answers (3)

clt60
clt60

Reputation: 63974

I would use this:

sed -E 's/^[ \t]*\[[^]]*\][ \t]*//'
  • will remove any spaces/tabs at the beginning of the line
  • optional [....]
  • any spaces after the [...]

some sed need the -E for interpeting \t as <tab>

To grep only lines containing BTRFS directly from the log:

grep -oP '\A(\s*\[[^]]*\])?\s*\K(?=BTRFS).*' <<EOF
    [   12.291091] BTRFS [info] (device sda2): disk space caching is enabled
[   12.291091] BTRFS [info] (device sda2): disk space caching is enabled
BTRFS [info] (device sda2): disk space caching is enabled
EOF

prints

BTRFS [info] (device sda2): disk space caching is enabled
BTRFS [info] (device sda2): disk space caching is enabled
BTRFS [info] (device sda2): disk space caching is enabled

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74705

This sed command does what you want:

sed 's/^\[[^]]*] *//'

It matches a [ at the start of the line, followed by any number of characters that aren't a ], followed by any number of spaces.

You could also do something similar using grep with Perl-style regular expressions enabled:

grep -Po '^(\[[^]]*] *)?\K.*'

The ? means that the first part of the match is optional. This uses \K to remove the beginning of the match, if it exists.

Testing it out:

$ cat file
[ 12.291091] BTRFS info (device sda2): disk space caching is enabled
BTRFS info (device sda2): disk space caching is enabled
$ sed 's/^\[[^]]*] *//' file
BTRFS info (device sda2): disk space caching is enabled
BTRFS info (device sda2): disk space caching is enabled
$ grep -Po '^(\[[^]]*] *)?\K.*' file
BTRFS info (device sda2): disk space caching is enabled
BTRFS info (device sda2): disk space caching is enabled

Upvotes: 4

Avinash Raj
Avinash Raj

Reputation: 174874

You could try the below sed command. It works whether the first [] is present or not.

$ echo '[   12.291091] BTRFS info (device sda2): disk space caching is enabled' | sed 's/^\[[^]]*\] *//'
BTRFS info (device sda2): disk space caching is enabled
$ echo 'BTRFS info (device sda2): disk space caching is enabled' | sed 's/^\[[^]]*\] *//'
BTRFS info (device sda2): disk space caching is enabled

Upvotes: 3

Related Questions