AWK : extract the content from the lines, where the program found its targeted word.($1, $2)

 [prabhucloudxlab@cxln4 ~]$ cat>filegrep.txt
This is Linux 2000
This is Windows 3000
This is MAC 4000
This is Linux 2000
This is Windows 3000
This is MAC 4000
This is Linux 2000
This is Windows 3000
This is MAC 4000
This is Linux 2000
This is Windows 3000
This is MAC 4000







[prabhucloudxlab@cxln4 ~]$ cat>hello.sh

#! /bin/bash
# extract the content from the lines, where the program found its targeted word.
# $1’ represents the first word of that line, similarly ‘$2’ represents the second, ‘$3’ represents the third word


echo "enter a filename to print from awk"
read fileName

if [[ -f $fileName ]]
then
                echo "printing 1st word  which is belongs to linux"
        awk '{print $1}' $fileName
        echo "==============================================================="
                echo "printing 1st word which is belongs to linux"
        awk ' /Linux/ {print $1}' $fileName
        echo "==============================================================="
                echo "printing 3rd word which is belongs to windows"
        awk ' /Windows/ {print $3}' $fileName
        echo "==============================================================="
                echo "printing 3rd word which is belongs to Linux"
        awk ' /Linux/ {print $4}' $fileName
        echo "==============================================================="

                echo "printing 3rd word, 4th word which is belongs to Linux"
                awk '/Linux/ {print $3,$4} ' $fileName
        echo "==============================================================="

                echo "Use of NR (no of records) built-in variables (Display Line Number)"
                awk ' {print NR,$0} ' $fileName
        echo "==============================================================="

                echo "Another use of NR built-in variables (Display Line From 3 to 4)"
                awk 'NR==3, NR==4 {print NR,$0} ' $fileName
        echo "==============================================================="

                echo "Use of NF (Last Feild) built-in variables (Display Last Field)"
                awk ' {print $3,$NF} ' $fileName
                echo "==============================================================="
else
    echo "$fileName doesn't exist"
fi





[prabhucloudxlab@cxln4 ~]$ sh hello.sh

enter a filename to print from awk
filegrep.txt

printing 1st word  which is belongs to linux
This
This
This
This
This
This
This
This
This
This
This
This
===============================================================
printing 1st word which is belongs to linux
This
This
This
This
===============================================================
printing 3rd word which is belongs to windows
Windows
Windows
Windows
Windows
===============================================================
printing 3rd word which is belongs to Linux
2000
2000
2000
2000
===============================================================
printing 3rd word, 4th word which is belongs to Linux
Linux 2000
Linux 2000
Linux 2000
Linux 2000
===============================================================
Use of NR (no of records) built-in variables (Display Line Number)
1 This is Linux 2000
2 This is Windows 3000
3 This is MAC 4000
4 This is Linux 2000
5 This is Windows 3000
6 This is MAC 4000
7 This is Linux 2000
8 This is Windows 3000
9 This is MAC 4000
10 This is Linux 2000
11 This is Windows 3000
12 This is MAC 4000
===============================================================
Another use of NR built-in variables (Display Line From 3 to 4)
3 This is MAC 4000
4 This is Linux 2000
===============================================================
Use of NF (Last Feild) built-in variables (Display Last Field)
Linux 2000
Windows 3000
MAC 4000
Linux 2000
Windows 3000
MAC 4000
Linux 2000
Windows 3000
MAC 4000
Linux 2000
Windows 3000
MAC 4000
===============================================================