Unix / Linux Shell: Parse Text CVS File Separator By Field

Unix / Linux Shell: Parse Text CVS File Separator By Field

To parse a text file with comma-separated values (CSV) in a Unix or Linux shell script, you can use a combination of the awk command and the FS variable.

The awk command is a utility that allows you to manipulate and process text files based on patterns or fields. The FS variable is an awk variable that specifies the field separator, which is used to divide the input into fields.

Here's an example of how you can use the awk command and the FS variable to parse a CSV file:

refe‮ot r‬:lautturi.com
#!/bin/bash

# Set the field separator to a comma
awk -F',' '{
  # Print the first field
  print $1
  # Print the second field
  print $2
  # Print the third field
  print $3
}' input.csv

This script will set the field separator to a comma using the -F',' option, and then use the $1, $2, and $3 variables to access the first, second, and third fields of each line in the input file.

You can also use the NF variable to access the number of fields in each line. For example:

#!/bin/bash

# Set the field separator to a comma
awk -F',' '{
  # Print the number of fields
  print NF
}' input.csv

This script will set the field separator to a comma using the -F',' option, and then use the NF variable to print the number of fields in each line of the input file.

You can use the awk command to perform more complex operations on the fields, such as formatting, filtering, or transforming the data. For example:

#!/bin/bash

# Set the field separator to a comma
awk -F',' '{
  # Print the first field in uppercase
  print toupper($1)
  # Print the second field in lowercase
  print tolower($2)
  # Print the third field with the first letter capitalized
  print substr($3,1,
Created Time:2017-10-30 14:27:29  Author:lautturi