delete last field in linux

How to Delete Last Field in Linux

Awk command in Linux allows you to work with text in numerous ways. It is powerful especially if your text is organized as columns or uses some kind of delimiters. Sometimes you may need to extract the last field from text file or command output. You can use the awk command to easily get the last field in Linux. Here are the steps to delete last field in Linux.


How to Delete Last Field in Linux

By default, awk command uses whitespace characters as delimiters in text file or command output. We will look at different use cases to delete the last field in Linux.


1. Delete Last Field in Command Output

Let us say you have the following output for ls command.

$ ls -l
-rw-r--r-- 1 ubuntu ubuntu 200M Apr 27 22:04 file1.txt
-rw-r--r-- 1 ubuntu ubuntu 400M Apr 27 22:04 file2.txt
-rw-r--r-- 1 ubuntu ubuntu 500M Apr 27 22:04 file3.txt
-rw-r--r-- 1 ubuntu ubuntu 600M Apr 27 22:04 file4.txt
-rw-r--r-- 1 ubuntu ubuntu 700M Apr 27 22:04 file5.txt

Here is the command to get last field in the above command output. We basically pipe it to awk command and use $NF awk variable to get the last field. $NF stores the last column by default, in awk.

$ ls -l | awk '{print $NF}'
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

Here is the command to delete last column of ls -l command’s output. In this case, we specify awk command to output all fields where NF contains the number of fields in the input. We subtract 1 from it, to prevent the last field from being counted & printed. The 1 at the end evaluates to true and triggers the default awk behavior of printing $0 that is printing the entire record except the last field.

$ ls -l | awk 'NF{NF-=1};1'
or
$ ls -l | awk 'NF{NF--};1'
or
$ ls -l | awk 'NF{--NF};1' 
-rw-r--r-- 1 ubuntu ubuntu 200M Apr 27 22:04
-rw-r--r-- 1 ubuntu ubuntu 400M Apr 27 22:04
-rw-r--r-- 1 ubuntu ubuntu 500M Apr 27 22:04
-rw-r--r-- 1 ubuntu ubuntu 600M Apr 27 22:04
-rw-r--r-- 1 ubuntu ubuntu 700M Apr 27 22:04


2. Delete Last Column in File Input

You can also delete last column from a file, instead of a command output. Let us say you have a file data.txt which has the output of above ls -l command

$ cat data.txt
-rw-r--r-- 1 ubuntu ubuntu 200M Apr 27 22:04 file1.txt
-rw-r--r-- 1 ubuntu ubuntu 400M Apr 27 22:04 file2.txt
-rw-r--r-- 1 ubuntu ubuntu 500M Apr 27 22:04 file3.txt
-rw-r--r-- 1 ubuntu ubuntu 600M Apr 27 22:04 file4.txt
-rw-r--r-- 1 ubuntu ubuntu 700M Apr 27 22:04 file5.txt

Here is the command to get the last field from this file in Linux, using awk command. In this case, we specify the input filename at the end of awk command.

$ awk '{print $NF}' data.txt
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

Here is the command to delete last field from file in Linux, using awk command.

$ awk 'NF{NF-=1};1' data.txt
or
$ awk 'NF{NF--};1' data.txt
or
$ awk 'NF{--NF};1' data.txt
-rw-r--r-- 1 ubuntu ubuntu 200M Apr 27 22:04
-rw-r--r-- 1 ubuntu ubuntu 400M Apr 27 22:04
-rw-r--r-- 1 ubuntu ubuntu 500M Apr 27 22:04
-rw-r--r-- 1 ubuntu ubuntu 600M Apr 27 22:04
-rw-r--r-- 1 ubuntu ubuntu 700M Apr 27 22:04


3. Delete Last Field with Different Delimiter

As mentioned earlier, the default delimiter used by awk is whitespace. If your file or command output contains different delimiter, you can specify it using -F option. Let us say your file has the following contents.

$ cat data.txt
/home/parent/child1/child2/child3/file1.txt
/home/parent/child1/child2/file2.txt
/home/parent/child1/file3.txt

In the above case, the delimiter is forward slash. In such cases, you can use the following command to get the last field.

$ awk -F '/' '{print $NF}' data.txt
file1.txt
file2.txt
file3.txt

You can use the following command to delete last field from input.

$ awk -F '/' 'NF{NF-=1};1' data.txt
or
$ awk -F '/' 'NF{NF--};1' data.txt
or
$ awk -F '/' 'NF{--NF};1' data.txt
file1.txt
file2.txt
file3.txt

In this article, we have learnt how to delete the last field in Linux.

Also read:

How to Harden Apache Server in CentOS
How to Display Specific Columns in Linux
How to List Active Connections in PostgreSQL
How to Create Swap Space in Ubuntu/Debian
How to Fix ‘mv: argument list too long’

Leave a Reply

Your email address will not be published. Required fields are marked *