I have a bash script with an ipaddress.txt input file, that I want to use against a csv file. I want find the IP and then display the values of Column A (for now). I may want to display two or three columns. The script im using finds the IP but is not printing the value in Column A. To note: if it helps with the answer, IP address location is the 16th Column or Column P of csv file.


# Read input file with IP addresses
while IFS= read -r ip_address; do
    # Search for IP address in CSV file
    grep -q "$ip_address" "$csv_file"

    # Print result
    if [ $? -eq 0 ]; then
        echo "$ip_address found in $csv_file"
        awk -F, -v "$ip_address"= '$16 == ip { print $1 }' "$csv_file"
        echo "$ip_address not found in $csv_file"
done < "$input_file"

Here's a sample of the output found in network-data.csv
awk: fatal: `' is not a legal variable name found in network-data.csv
awk: fatal: `' is not a legal variable name not found in network-data.csv not found in network-data.csv found in network-data.csv
awk: fatal: `' is not a legal variable name
  • Presumably you meant something more like awk -F, -v ip="$ip_address" '$16 == ip { print $1 }' "$csv_file" however you really don't need a shell loop here at all, all the processing could be done by reading both files into awk Dec 18, 2023 at 18:10
  • @steeldriver I swapped that line in, thanks. I no longer get an error. But still not getting the value in Column A. No value at all is being displayed
    – burownidl
    Dec 18, 2023 at 18:14
  • 2
    Please edit your question and show us an example of your input and then the output you would expect from that example. If you have made a change after what steeldriver told you, please also edit to show the new script since that is very relevant. We cannot know why it doesn't work if we can't see the exact code you are executing. I suspect the entire script can be replaced by a simple awk one-liner.
    – terdon
    Dec 18, 2023 at 19:03
  • I gave up on the bash script and tried awk. I got what I needed.
    – burownidl
    Dec 18, 2023 at 19:54
  • You posted some sample output but post sample input and expected output so we can help you. You posted an answer that can fail given various inputs and there's no reason why the script in your answer would work if, as you say, the correction provided in @steeldriver's comment doesn't so you may want to debug this further and learn about more robust alternatives.
    – Ed Morton
    Dec 19, 2023 at 11:25

1 Answer 1


I used AWK instead of Bash. awk -f awkscript.awk ipaddresses.txt csvfile.csv

    FS = ","; # Assuming CSV fields are separated by commas

# Load the IP addresses from file1 into an array
NR == FNR {
    ips[$0] = 1;

# Now we're working with the CSV file
    # The IP address is in the 16th field of the CSV
    if ($16 in ips) {
        print $1, $2; # Print columns 1 and 2
  • The = 1 in ips[$0] = 1 is doing nothing useful, just eating up CPU cycles and memory. The ;s aren't doing anything useful either but they aren't doing any harm - in awk a statement is terminated by a semi-colon OR a newline, you don't need both. You might want to change FS = "," to FS = OFS = "," so the output continues to be CSV, otherwise it'll be ambiguous if any of the output fields contain blanks.
    – Ed Morton
    Dec 19, 2023 at 12:11
  • See whats-the-most-robust-way-to-efficiently-parse-csv-using-awk for information on working with CSVs using awk.
    – Ed Morton
    Dec 19, 2023 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .