awk Remove First Column

awk remove first column

Using awk command with sub() method

Use the awk command with sub() method to remove the first column from the given CSV file (inputFile.csv) in Bash. We will not update the input file but display the output on the Bash console.

Here we used $1="" to replace first column with empty string and sub() method to replace remaining , with empty string.

Use the awk command to remove the first column from the given CSV file (inputFile.csv) in Bash. Note that we are not required to update the input file but store the modified content in a separate output file (output.csv) using the redirection operator (>).

Use the awk command with the -i parameter set to inplace to remove the first column from the specified CSV file (inputFile.csv) by directly modifying it.

The following example is similar to the above, but we use the cp command to copy the inputFile.csv into the inputFileBackup.csv file to create the backup and then use the awk command to remove the first column from the given CSV file (inputFile.csv).

Using awk Command with for loop

Use the awk command to remove the first column from the given CSV file (inputFile.csv) in Bash. We will not update the input file but display the output on the Bash console.

Use the awk command to remove the first column from the given CSV file (inputFile.csv) in Bash. Note that we are not required to update the input file but store the modified content in a separate output file (output.csv) using the redirection operator (>).

Use the awk command with the -i parameter set to inplace to remove the first column from the specified CSV file (inputFile.csv) by directly modifying it.

The following example is similar to the above, but we use the cp command to copy the inputFile.csv into the inputFileBackup.csv file to create the backup and then use the awk command to remove the first column from the given CSV file (inputFile.csv).

The awk is a Linux tool we use to process and manipulate data. Let’s understand how the awk command removed the first column from the input file, which was inputFile.csv in our case.

In the awk command, the -F specified the field separator as a comma (,). This instructed the awk command to treat every comma-separated value in the CSV file as a separate field. The {...} denoted the code block that must be run for every line in the inputFile.csv file.

We used a for loop inside the code block to iterate through the current line’s each field. It began from the second field (i=2) and continued until the last field (i<=NF), where NF represented the total number of fields in the line.

Inside the for loop, we used a printf statement to print the current field's value ($i); it also checks if the current field is the last field using i==NF. If it is the last field, the RS (Record Separator) was printed to move to the next line; otherwise, the FS (Field Separator) was printed to separate the values.

Remember, the awk command preserved the structure of the CSV file, whether we took the backup or wrote the modified content in a separate file.

Was this post helpful?

Leave a Reply

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