Articles

Display content of file in shell script using awk

Display content of file in shell script using awk


In this chapter of shell scripting, we will learn about:

  • display content of file in shell script row wise
  • display content of file in shell script column wise
  • display content of file in shell script with index
  • understand use of awk command in linux

Suppose we have a file say tih.txt given below.

CourseName  CourseId  CourseFee  CourseDuration

Java        J-001     4000        36
C++         CPP-005   3500	  30
Linux       Lin-008   4500        42

We want to write a shell script that will:

  • read all the content of file with and without index
  • read content of file for any specific row (s)
  • read content of file for any specific coloumn (s)

We will use awk command in linux to write shell script to print content of file in different ways

Lets begin with the task to display content of file in shell script.


SHELL SCRIPT TO PRINT ALL CONTENT OF FILE

Display content of file in shell script with the use of awk command is demonstrated below:

file name: fileDetails.sh

echo "Use of Awk to print all content of file"

awk '{print $0}' tih.txt

echo "Details printing complete"

OUTPUT

When we execute above shell script then we get below output in given screenshot:

display content of file in shell script

As per the screenshot, it is visible that all the contents of file is displayed once shell script is executed.

To execute shell script: ./fileDetails.sh


DISPLAY CONTENT OF FILE IN SHELL SCRIPT WITH INDEX

In the above example, when we execute the shell script the index of each row is not displayed.

 

So lets see, how to display the content of file along with index. We have modified the script as seen below:

echo "Use of Awk to print all content of file"

awk '{print NR,$0}' tih.txt

echo "Details printing complete"

OUTPUT

The screenshot of the output is displayed below:

display content of file in shell script output

 

What we did to get index?

In the above shell script, we have just added NR (highlighted in brown) to request awk command to print all the content with index. 

Note: You can add NR similarly in other examples that we will explain later to print specific row or coloumn from file to get output with index.

Now we are done with this.Lets move ahead to display content of file in shell script for specific column.


DISPLAY CONTENT OF FILE IN SHELL SCRIPT FOR SPECIFIC COLUMN

Suppose we want to print the first column only from the file i.e CourseName.

 

Can we do it?

Yes, definitely we can.

 

Lets see how to display content of file in shell script for CourseName column only.

echo "Use of Awk to print all content of file"

awk '{print NR,$1}' tih.txt

echo "Details printing complete"

OUTPUT

We have displayed output of above execution in below screenshot:

display content of file in shell script for first column

 

Display any random column(s) from file

Below shell script is going to display column number 2 & 3 from the file tih.txt.

echo "Use of Awk to print all content of file"

awk '{print NR,$2,$3}' tih.txt

echo "Details printing complete"

Now columns 2 and 3 will be printed by below shell script. 

Just add the column number in $ that you want to be displayed. Above we have added 2 & 3 so courseId and CourseFee is displayed.

Note if you don't want indexing then you can remove NR from above script. Try it yourself once.

display content of file in shell script for multiple column

 

Display last column of file in shell script

Now if you want to specifically display only last column then you can use NF as shown in below script.

echo "Use of Awk to print all content of file"

awk '{print $NF}' tih.txt

echo "Details printing complete"

OUTPUT

$ ./fileDetails.sh
Use of Awk to print all content of file
CourseDuration
 
36
30
42
Details printing complete
 

Please execute and check it yourself to verify.

 


shell command to display contents of a file row wise

So far we have seen how to display content of file completely and column wise.

 

Now its time to go ahead and see how to display content of file row wise using shell script.

echo "Use of Awk to print all content of file"

awk '{print $0}' tih.txt | head -1

echo "Details printing complete"

OUTPUT

display content of file in shell script row wise

You can see only first row is printed that is highlighted in yellow.

 

Now if you want to print 2 columns then just edit as below

awk '{print $0}' tih.txt | head -2

Just change the number after head to the row count that you want to be displayed as part of your result.

 

Thus we have covered a lot of different ways to diplay the content of file using shell script and use of awk command in linux.

 

Next, we will see how to display content of file in shell script matching certain regular expression pattern.

 

See you in next chapter. Thanks for your time.

 


Shell Script Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sonu Pandit
Technology geek, loves to write and share knowledge with the world. Having 10+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : May 19,2024  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!