How to extract whatever string comes after a matched pattern in Shell script. I know this functionality in Perl scripting, but i dont know in Shell scripting.
Following is the example,
Subject_01: This is a sample subject and this may vary
I have to extract whatever string that follows “Subject_01:”
Any help please.
It depends on your shell.
If you’re using bourne shell or bash or (I believe) pdksh, then you can do fancy stuff like this:
Note that this is pretty limited in terms of format. The line above requires that you have ONE space after your colon. If you have more, it will pad the beginning of
$output.If you’re using some other shell, you may have to do something like this, with the
cutcommand:The first example uses
cut, which is very small and simple. The second example usessed, which can do far more, but is a (very) little heavier in terms of CPU.YMMV. There’s probably a better way to handle this in csh (my second example uses tcsh), but I do most of my shell programming in Bourne.