Karthik Karthik - 3 months ago 8
Bash Question

shell parse csv file, assign fields to variable and print it in multiple columns

Im trying to curl a csv file and parse it based on their property and print it back by using variable names.

File 1:

10.0.0.1,gateway,name_of_device
10.2.4.5,server,name_of_device
10.3.5.6,PC,name_of_device


My script below,

#!/bin/sh

input=$(curl http://example.com/1.txt)

ip=$(echo "$input" | awk -F ',' '{print $1}')
type=$(echo "$input" | awk -F ',' '{print $2}')
name=$(echo "$input" | awk -F ',' '{print $3}')

echo "$ip $type $name"


This prints,

10.0.0.1
10.2.4.5
10.3.5.6
gateway
server
PC
name_of_device
name_of_device
name_of_device


but the expected output should be like,

10.0.0.1 gateway name_of_device
10.2.4.5 server name_of_device
10.3.5.6 PC name_of_device


Tried different options like,

assigning output to another variable and echo'ing it,

# output="$source_ip $tag_text"
# echo -e $output


printf statements:

# printf "%-20s | %-20s" "$source_ip" "$tag_text"


# printf "$source_ip" "$tag_text"

Answer

awk is certainly the correct tool to use for this, but you can also do:

curl http://example.com/1.txt |
while IFS=, read ip type name rest; do
  echo $ip $type $name
done

The variables will lose their value after the while loop finishes, since it is in a sub-shell.