1

I have a CSV with each row 2 IPs. I want to combine 2 IPs separated by *** and populate in an array. I tried the code below, but I only get the last row.

#!/bin/bash

INPUT="IPPairs.csv"
array=()

while IFS="," read var1 var2 ; do
        echo $var1 $var2
        pairString="$var1***$var2"
        array+=($pairString)
done < $INPUT

echo "${array[@]}"
1
  • Work's fine for me. Are your ip addresses in IPPairs.csv comma delimited? Consider tossing some double quotes in there array+=("$pairString") for example. But with straight IP addresses, you should be fine. If there are domain names in there with backslashes you may want to use read -r to avoid mangling Commented May 6, 2019 at 13:08

1 Answer 1

1

Unless your input file contains something other than , as the delimiter, your code should work. You could make it shorter, though (requires Bash 4.0 or newer):

mapfile -t array < IPPairs.csv
array=("${array[@]/,/***}")

The first command reads the file into an array, the second command replaces , with *** in each array element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.