Many a time we come across a situation where in we will have to search for a pattern and if a match exists then we will have to print entire paragraph containing that specific pattern (One best example would be extracting DDL of Sequence from db2look or printing only Indirect (Local) databases from System database directory)
In order to achieve this we can use different commands like grep, awk etc
In AIX, this can be achieved by using a simple "grep -ip <pattern>". The same will not work in Linux.
Let's leverage on below snippet which works seamlessly on both AIX and Linux.
grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
Above Snippet should be executed at Shell prompt first before it can be used as below.
command | grepp <pattern>
E.g.
>grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
>db2 "list db directory" | grepp "Indirect"
Above example shall print only Indirect (Local) databases from System database directory.
Let me demonstrate the usage of the same command visually with screenshots.
I'm using SUSE Linux server for this and a file by name "demo" which contains 2 paragraphs in it.
Now execute the same Snippet at the command prompt as shown below and try using the same (grepp pattern <filename>) to search for a pattern and print entire paragraph.
In this example, OS doesn't recognize grepp initially. After executing the snippet at the prompt, I've tried to search for a word called "test" in the file "demo" and it printed entire 2nd paragraph.
Comments??? Most welcome!
In order to achieve this we can use different commands like grep, awk etc
In AIX, this can be achieved by using a simple "grep -ip <pattern>". The same will not work in Linux.
Let's leverage on below snippet which works seamlessly on both AIX and Linux.
grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
Above Snippet should be executed at Shell prompt first before it can be used as below.
command | grepp <pattern>
E.g.
>grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
Above example shall print only Indirect (Local) databases from System database directory.
Let me demonstrate the usage of the same command visually with screenshots.
I'm using SUSE Linux server for this and a file by name "demo" which contains 2 paragraphs in it.
Now execute the same Snippet at the command prompt as shown below and try using the same (grepp pattern <filename>) to search for a pattern and print entire paragraph.
In this example, OS doesn't recognize grepp initially. After executing the snippet at the prompt, I've tried to search for a word called "test" in the file "demo" and it printed entire 2nd paragraph.
Comments??? Most welcome!
No comments:
Post a Comment