BASH Programming

How to Read Column Data from a Text File in a Bash Shell Script

One of the tasks that can be performed using a bash shell script is reading data from a text file and processing it. In this article, we will look at how to read a specific column of data from a text file or how to read multiple columns from a text file in a bash shell script.

How to Read Column Data from a Text File in a Bash Shell Script

Reading specific columns of data from a text file in a bash shell script come in handy in various applications like in automated repetitive tasks, software development, text management and much more, all you need to do is to just read that respective column. For further illustration below the syntax that one can follow to read any column from any text file:

#!/bin/bash
while read line; do
    # use awk to extract the third field (the name) from each line
    <any-name>=$(echo $line | awk '{print $<column-number>}')
    # print the name
    echo $<any-name>
done < <file-name>.txt

 
1: The script starts by specifying the shell interpreter to be used with the shebang line #!/bin/bash.

2: The while loop reads the contents of the text file line by line, using the read command and for each line that is read, the awk command is used to extract the field (the name) from the line. The awk command uses the pattern {print $<column-number>} to select the field, which is then stored in the variable name.

3: Finally, the script prints the value of the name variable using the echo command and this process is repeated for each line in the file until there are no more lines to read.

For reader understanding I have used the above given syntax to read one column from the text file and below is the code for it:

#!/bin/bash
while read line; do
    # use awk to extract the second field from each line
    info=$(echo $line | awk '{print $3}')
    # print the age
    echo $info    
done < company_details.txt

 
Here in the code, I have read the third column of each line of the text file named company details, the awk command is used to extract the third field (the name) from the line. The awk command uses the pattern {print $3} to select the third field, here is the output of my example code:


Below is the text file that I created for demonstration purposes:

How to Read Multiple Columns of a Text File in Bash Shell Script

If you want to read more than one column or a file, then all you need to add the column number and the rest is the same so here is the code for reading more than one column of the text file:

#!/bin/bash

# This line specifies that the script should be interpreted by the bash shell.

# loop through each line of the file
while read line; do
    # use awk to extract the third field (the name) from each line
    info=$(echo $line | awk '{print $1, $2, $3}')
    # print the name
    echo $info
done < company_details.txt

 
Below is the output of the code that reads the all the three columns in the text file:

Conclusion

The bash scripts are a versatile tool that can be used to automate various tasks especially when it comes to Unix-based systems. By combining the while loop and other shell commands one can easily read column data from a text file and perform various operations with the data.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.