Robin
Robin

Reputation: 11

CSV file parsing in Bash

I have a CSV file with sample entries given below. What I want is to write a Bash script to read the CSV file line by line and put the first entry e.g 005 in one variable and the IP 192.168.10.1 in another variable, that I need to pass to some other script.

005,192.168.10.1
006,192.168.10.109
007,192.168.10.12
008,192.168.10.121
009,192.168.10.123

Upvotes: 1

Views: 85

Answers (3)

Aaron
Aaron

Reputation: 24802

Here's how I would do it with GNU tools :

while read line; do
    echo $line | cut -d, -f1-2 --output-delimiter=' ' | xargs your_command
done < your_input.csv

while read line; do [...]; done < your_input.csv will read your file line by line. For each line, we will cut it to its first two fields (separated by commas since it's a CSV) and pass them separated by spaces to xargs which will in turn pass as parameters to your_command.

Upvotes: 1

codeforester
codeforester

Reputation: 42969

A more efficient approach, without the need to fork cut each time:

#!/usr/bin/env bash

while IFS=, read -r field1 field2; do
    # do something with $field1 and $field2
done < file.csv

The gains can be quite substantial for large files.

Upvotes: 3

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

If this is a very simple csv file with no string literals, etc. you can simply use head and cut:

#!/bin/bash

while read line
do
    id_field=$(cut -d',' -f 1 <<<"$line") #here 005 for the first line
    ip_field=$(cut -d',' -f 2 <<<"$line") #here 192.168.0.1 for the first line
    #do something with $id_field and $ip_field
done < file.csv

The program works as follows: we use cut -d',' to obtain the first and second field of that line. We wrap this around a while read line and use I/O redirection to feed the file to the while loop.

Of course you substitute file.csv with the name of the file you want to process, and you can use other variable names than the ones in this sample.

Upvotes: 0

Related Questions