Have you ever needed to print data in a specific format, with spaces between columns, using AWK? AWK is a powerful text processing tool that is commonly used in Unix and Linux systems. In this article, we will show you how to print data with spaces in columns using AWK, with examples that are easy to understand for entry-level users.
What is AWK?
AWK is a programming language that is used for text processing and manipulation. It is a command-line tool that is available in Unix and Linux systems. AWK is often used for filtering, transforming, and summarizing data. It is a powerful tool that can handle large amounts of data with ease. AWK processes data in records, where each record is a line of text. Each record is then split into fields, which are separated by whitespace or a specific delimiter.
Printing with Spaces in Columns using AWK
To print data with spaces in columns using AWK, you can use the printf function. The printf function allows you to format the output in a specific way. To print data with spaces in columns, you can use the %s format specifier. The %s format specifier is used to print a string, and the spaces between the columns are added using the sep variable.
Here is an example of how to print data with spaces in columns using AWK:
$ echo "apple banana cherry" | awk '{printf "%s %-10s %-10s
", $1, $2, $3}'
apple banana cherry
In this example, the echo command is used to create a string with three words: "apple", "banana", and "cherry". The string is then piped to the awk command. The awk command uses the printf function to print the data in three columns. The %s format specifier is used to print each word, and the -10s format specifier is used to print each word in a column that is 10 characters wide. The spaces between the columns are added using the sep variable, which is set to a single space.
Here is another example of how to print data with spaces in columns using AWK. In this example, the data is read from a file:
$ cat data.txt
apple 10
banana 20
cherry 30
$ awk '{printf "%s %-10s %s
", $1, $2, "calories"}' data.txt
apple 10 calories
banana 20 calories
cherry 30 calories
In this example, the awk command reads the data from a file called data.txt. The awk command uses the printf function to print the data in three columns. The first column contains the fruit name, the second column contains the number of calories, and the third column contains the word "calories". The second column is left-justified using the -10s format specifier. The spaces between the columns are added using the sep variable, which is set to a single space.
In this article, we have shown you how to print data with spaces in columns using AWK. We have explained what AWK is and how it can be used for text processing and manipulation. We have also provided examples of how to print data with spaces in columns using AWK. These examples are easy to understand for entry-level users. We hope that you have found this article useful and that you are now able to print data with spaces in columns using AWK.
References
| Title | URL |
|---|---|
| AWK User's Guide | https://www.gnu.org/software/gawk/manual/gawk.html |
| AWK - A Tutorial and Introduction | https://www.grymoire.com/Unix/Awk.html |