-1

I'm trying to grep from file with similar text:

1.1.1.1-Text1
2.2.2.2-Text2
3.3.3.3-Text3

and put each grep result as different variable to get this:

ip=$(cat nodes.txt | grep -o '^[^-]*')
curl -k -sL -w 'Response Code: %{http_code} Time: %{time_total}' $ip -o /dev/null # variable IP with value of 1.1.1.1
curl -k -sL -w 'Response Code: %{http_code} Time: %{time_total}' $ip -o /dev/null # variable IP with value of 2.2.2.2
curl -k -sL -w 'Response Code: %{http_code} Time: %{time_total}' $ip -o /dev/null # variable IP with value of 3.3.3.3

so my test results would look like this:

Test from 1.1.1.1-Text1 Response Code: 200 Time: 0.000
Test from 2.2.2.2-Text2 Response Code: 200 Time: 0.000
Test from 3.3.3.3-Text3 Response Code: 200 Time: 0.000
sQpas
  • 1
  • try at first split line, `tr` or replace `-` with space using `sed` then use `awk` to get ip address. should work: `ip=$(cat nodes.txt | sed 's/-/ /g' | awk '{print $1}' ` and you get: 1.1.1.1 2.2.2.2 – darvark Apr 06 '17 at 05:27
  • I do not understand what the problem is. Please, read [this guide](http://stackoverflow.com/help/mcve) – Jdamian Apr 06 '17 at 06:02

1 Answers1

0

Try sth like this:

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
  ip=`echo $line | grep -o '^[^-]*'`
  echo $ip
  # now you can use $ip as variable with proper value from file
  # ...
done < nodes.txt
henio180
  • 156
  • 1
  • 8