Linux awk print column

How to use ‘awk’ to print columns from a text file (in any order)

One of my favorite ways to use the Unix awk command is to print columns of information from text files, including printing columns in a different order than they are in in the text file. Here are some examples of how awk works in this use case.

awk column printing examples

Suppose you have a file named foo with these contents, three columns of data separated by blanks:

$ cat foo 1 2 3 a b c

Next, here are the awk examples:

$ awk '< print $1 >' foo 1 a $ awk '< print $2 >' foo 2 b $ awk '< print $3 >' foo 3 c $ awk '< print $1, $3 >' foo 1 3 a c $ awk '< print $3, $1 >' foo 3 1 c a

As you can see, with awk you can print any column you want, and you can easily rearrange the order of the columns when you print them out.

If you’re not familiar with awk , notice how it automatically loops over every line in the text file. This built-in ability to process every line in the input file is a great feature of awk .

While all of these examples show how awk works on a file, it can also read its input from a Unix pipeline, like this:

$ cat foo | awk '< print $3, $1 >' 3 1 c a

Using a field separator with awk

If you need to specify a field separator — such as when you want to parse a pipe-delimited or CSV file — use the -F option, like this:

Summary

As shown, awk is a great tool for printing columns and rearranging column output when working with text files.

Источник

How to print a range of columns using the `awk` command

The `awk` command is one of many commands that can be used to print a range of columns from tabular data in Linux. The `awk` command is can be used directly from the terminal by executing the `awk` script file. In this tutorial, we will show you how to print a range of columns from tabular data.

Читайте также:  Эмулятор wine для linux

Example 1: Print a range of columns from a command output

The following command will print the second, third, and fourth columns from the command output, ‘ls -l‘. Here, the column numbers are stated explicitly, but a more efficient command for printing the same range of columns is shown in the next example.

The following output is produced by the command above.

Example 2: Print the range of columns from a file by using a for loop

To follow along with this example and the other examples in this tutorial, create a text file named marks.txt with the following content:

The following `awk` command will print the first three columns of marks.txt. The for loop is used to print the column values, and the loop includes three steps. The NF variable indicates the total numbers of fields or columns of the file.

The following output will be produced by running the command. The output shows the student IDs and the marks for CSE203 and CSE102.

Example 3: Print the range of columns by defining starting and ending variables

The following `awk` command will print the first three columns from the command output ‘ls -l’ by initializing the starting and ending variables. Here, the value of the starting variable is 1, and the value of the ending variable is 3. These variables are iterated over in a for loop to print the column values.

The following output will appear after running the command. The output shows the first three column values of the output, ‘ls -l’.

Example 4: Print a range of columns from a file with formatting

The following `awk` command will print the first three columns of marks.txt using printf and output field separator (OFS). Here, the for loop includes three steps, and three columns will be printed in sequence from the file. OFS is used here to add space between columns. When the counter value of the loop (i) equals the ending variable, then a newline(\n) is generated.

The following output will be generated after running the above commands.

Example 5: Print the range of columns from a file using a conditional statement

The following `awk` command will print the first and last columns from a file by using a for loop and an if statement. Here, the for loop includes four steps. The starting and ending variables are used in the script to omit the second and third columns from the file by using the if condition. The OFS variable is used to add space between the columns, and the ORS variable is used to add a newline(\n) after printing the last column.

$ cat marks.txt
$ awk -v start = 2 -v end = 3 ‘ < for (i=1; i<=NF;i++)
if( i>=start && i <=end) continue;
else printf(«%s%s», $i,(i!=NF) ? OFS : ORS) >’ marks.txt

The following output will appear after running the above commands. The output shows the first and last columns of marks.txt.

Example 6: Print the range of columns from a file using the NF variable

The following `awk` command will print the first and last columns from the file by using an NF variable. No loops or conditional statements are used to print the column values. NF indicates the number of fields. There are four columns in marks.txt. $(NF-3) defines the first column, and $NF indicates the last column.

$ cat marks.txt
$ awk » marks.txt

Читайте также:  Установить труконф на линукс

The following output is produced by running the above commands. The output shows the first and last columns of marks.txt.

Example 7: Print the range of columns from a file using substr() and index()

The index() function returns a position if the second argument value exists in the first argument value. The substr() function can take three arguments. The first argument is a string value, the second argument is the starting position, and the third argument is the length. The third argument of substr() is omitted in the following command. Because the column starts from $1 in the `awk` command, the index() function will return $3, and the command will print from $3 to $4.

$ cat marks.txt
$ awk » marks.txt

The following output will be produced by running the above commands.

Example 8: Sequentially print a range of columns from a file using printf

The following `awk` command will print the first, second, and third columns of marks.txt by setting enough space for 10 characters.

$ cat marks.txt
$ awk ‘//’ marks.txt

The following output will be produced by running the above commands.

Conclusion

There are various ways to print the range of columns from the command output or a file. This tutorial shows how `awk` command can help Linux users to print content from tabular data.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How to Print the First Column or Last Column or Both Using `awk`

Linux’s `awk` command is a powerful utility for different operations on text files such as search, replace, and print. It is easy to use with tabular data because it automatically divides each line into fields or columns based on the field separator. When you work with a text file that contains tabular data and want to print the data of a particular column, then the `awk` command is the best option. In this tutorial, we will show you how to print the first column and/or last column of a line or text file.

Print the first column and/or last column of a command output

Many Linux commands such as the ‘ls’ command generate tabular outputs. Here, we will show you how to print the first column and/or last column from the output of the ‘ls -l’ command.

Example 1: Print the first column of a command output

The following `awk` command will print the first column from the output of the ‘ls -l’ command.

The following output will be produced after running the above commands.

Example 2: Print the last column of a command output

The following `awk` command will print the last column from the output of the ‘ls -l’ command.

The following output will be produced after running the above commands.

Example 3: Print the first and last columns of a command output

The following `awk` command will print the first and last columns from the output of the ‘ls -l’ command.

The following output will be produced after running the above commands.

Here, we will show you how to use the `awk` command to print the first column and/or last column of a text file.

Create a text file

To follow along with this tutorial, create a text file named customers.txt with the following content. The file contains three types of customer data: name with id, email, and phone number. The tab character (\t) is used to separate these values.

Читайте также:  Vmware workstation shared folders linux

Jonathon Bing — 1001 jon@gmail.com 01967456323

Micheal Jackson — 2006 mic@gmail.com 01756235643

Janifer Lopez — 3029 jani@gmail.com 01822347865

John Abraham — 4235 john@gmail.com 01590078452

Mir Sabbir — 2756 mir@gmail.com 01189523978

Example 4: Print the first column of a file without using a field separator

If no field separator is used in the `awk` command, then a space is used as the default field separator. The following `awk` command will print the first column by using the default separator.

The following output will be produced after running the above commands. Note that the output shows only the customer’s first name because the space is applied as the field separator. The solution to this problem is shown in the next example.

Example 5: Print the first column of a file with a delimiter

Here, \t is used as a field separator to print the first column of the file. The ‘-F’ option is used to set the field separator.

The following output will be produced after running the above commands. The content of the file is divided into three columns based on \t. Therefore, the customer’s name and id are printed as the first column. If you want to print the customer’s name without the id, then continue to the next example.

If you want to print the customer’s name without the id, then you have to use ‘-‘ as a field separator. The following `awk` command will print the customer’s name only as the first column.

The following output will be produced after running the above commands. The output includes the full names of the customers without their ids.

Example 6: Print the last column of a file

The following `awk` command will print the last column of customers.txt. Because no field separator is used in the command, the space will be used as a field separator.

The following output will be produced after running the above commands. The last column contains phone numbers, as shown in the output.

Example 7: Print the first and last columns of a file

The following `awk` command will print the first and last columns of customers.txt. Here, tab (\t) is used as the field separator to divide the content into columns. Here, tab (\t) is used as a separator for the output.

The following output will appear after running the above commands. The content is divided into three columns by \t; the first column contains the customer’s name and id and the second column contains the phone number. The first and last columns are printed by using \t as a separator.

Conclusion

The `awk` command can be applied in different ways to get the first column and/or last column from any command output or from tabular data. It is important to note that a field separator is required in the command, and if one is not provided, then the space is used.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Оцените статью
Adblock
detector