Extracting IP address from a text and store it in a variable











up vote
5
down vote

favorite
3












I have a text file named abd shown below.



48878 128.206.6.136
34782 128.206.6.137
12817 23.234.22.106


I want to extract only IP address from the text and store it in a variable and use for other purpose.



I have tried this.



for line in `cat abd`
do

ip=`grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' $line`

echo $ip

done


I am getting an error as follows



grep: 34782: No such file or directory

grep: 128.206.6.137: No such file or directory

grep: 12817: No such file or directory

grep: 23.234.22.106: No such file or directory


I don't know what is going wrong here.
Any help would be appreciated.










share|improve this question
























  • Will the input file follow the same pattern?
    – heemayl
    Nov 15 '15 at 1:13










  • @heemayl Yes. There are loads of other IPs.
    – Swatesh Pakhare
    Nov 15 '15 at 1:14






  • 1




    Change the first line of your loop to while read line and add < abd after the done
    – Jeff Schaller
    Nov 15 '15 at 1:20










  • If there are tons of other IPs, then I think my answer best answers what it appeared as if you were actually trying to do, despite other users' negative votes and comments toward my answer. Can you clarify your question? Are you wanting to go through each IP in order and say something about it or do something with it, or are you going to reference each IP individually with a separate variable? If you are wanting to go in order (within the loop) you only need a single $ip variable per iteration, and there is no need for an array or to reference a specific IP address outside the loop.
    – rubynorails
    Nov 17 '15 at 1:31















up vote
5
down vote

favorite
3












I have a text file named abd shown below.



48878 128.206.6.136
34782 128.206.6.137
12817 23.234.22.106


I want to extract only IP address from the text and store it in a variable and use for other purpose.



I have tried this.



for line in `cat abd`
do

ip=`grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' $line`

echo $ip

done


I am getting an error as follows



grep: 34782: No such file or directory

grep: 128.206.6.137: No such file or directory

grep: 12817: No such file or directory

grep: 23.234.22.106: No such file or directory


I don't know what is going wrong here.
Any help would be appreciated.










share|improve this question
























  • Will the input file follow the same pattern?
    – heemayl
    Nov 15 '15 at 1:13










  • @heemayl Yes. There are loads of other IPs.
    – Swatesh Pakhare
    Nov 15 '15 at 1:14






  • 1




    Change the first line of your loop to while read line and add < abd after the done
    – Jeff Schaller
    Nov 15 '15 at 1:20










  • If there are tons of other IPs, then I think my answer best answers what it appeared as if you were actually trying to do, despite other users' negative votes and comments toward my answer. Can you clarify your question? Are you wanting to go through each IP in order and say something about it or do something with it, or are you going to reference each IP individually with a separate variable? If you are wanting to go in order (within the loop) you only need a single $ip variable per iteration, and there is no need for an array or to reference a specific IP address outside the loop.
    – rubynorails
    Nov 17 '15 at 1:31













up vote
5
down vote

favorite
3









up vote
5
down vote

favorite
3






3





I have a text file named abd shown below.



48878 128.206.6.136
34782 128.206.6.137
12817 23.234.22.106


I want to extract only IP address from the text and store it in a variable and use for other purpose.



I have tried this.



for line in `cat abd`
do

ip=`grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' $line`

echo $ip

done


I am getting an error as follows



grep: 34782: No such file or directory

grep: 128.206.6.137: No such file or directory

grep: 12817: No such file or directory

grep: 23.234.22.106: No such file or directory


I don't know what is going wrong here.
Any help would be appreciated.










share|improve this question















I have a text file named abd shown below.



48878 128.206.6.136
34782 128.206.6.137
12817 23.234.22.106


I want to extract only IP address from the text and store it in a variable and use for other purpose.



I have tried this.



for line in `cat abd`
do

ip=`grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' $line`

echo $ip

done


I am getting an error as follows



grep: 34782: No such file or directory

grep: 128.206.6.137: No such file or directory

grep: 12817: No such file or directory

grep: 23.234.22.106: No such file or directory


I don't know what is going wrong here.
Any help would be appreciated.







shell-script text-processing grep regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '15 at 13:45









vonbrand

14.1k22444




14.1k22444










asked Nov 15 '15 at 1:11









Swatesh Pakhare

113119




113119












  • Will the input file follow the same pattern?
    – heemayl
    Nov 15 '15 at 1:13










  • @heemayl Yes. There are loads of other IPs.
    – Swatesh Pakhare
    Nov 15 '15 at 1:14






  • 1




    Change the first line of your loop to while read line and add < abd after the done
    – Jeff Schaller
    Nov 15 '15 at 1:20










  • If there are tons of other IPs, then I think my answer best answers what it appeared as if you were actually trying to do, despite other users' negative votes and comments toward my answer. Can you clarify your question? Are you wanting to go through each IP in order and say something about it or do something with it, or are you going to reference each IP individually with a separate variable? If you are wanting to go in order (within the loop) you only need a single $ip variable per iteration, and there is no need for an array or to reference a specific IP address outside the loop.
    – rubynorails
    Nov 17 '15 at 1:31


















  • Will the input file follow the same pattern?
    – heemayl
    Nov 15 '15 at 1:13










  • @heemayl Yes. There are loads of other IPs.
    – Swatesh Pakhare
    Nov 15 '15 at 1:14






  • 1




    Change the first line of your loop to while read line and add < abd after the done
    – Jeff Schaller
    Nov 15 '15 at 1:20










  • If there are tons of other IPs, then I think my answer best answers what it appeared as if you were actually trying to do, despite other users' negative votes and comments toward my answer. Can you clarify your question? Are you wanting to go through each IP in order and say something about it or do something with it, or are you going to reference each IP individually with a separate variable? If you are wanting to go in order (within the loop) you only need a single $ip variable per iteration, and there is no need for an array or to reference a specific IP address outside the loop.
    – rubynorails
    Nov 17 '15 at 1:31
















Will the input file follow the same pattern?
– heemayl
Nov 15 '15 at 1:13




Will the input file follow the same pattern?
– heemayl
Nov 15 '15 at 1:13












@heemayl Yes. There are loads of other IPs.
– Swatesh Pakhare
Nov 15 '15 at 1:14




@heemayl Yes. There are loads of other IPs.
– Swatesh Pakhare
Nov 15 '15 at 1:14




1




1




Change the first line of your loop to while read line and add < abd after the done
– Jeff Schaller
Nov 15 '15 at 1:20




Change the first line of your loop to while read line and add < abd after the done
– Jeff Schaller
Nov 15 '15 at 1:20












If there are tons of other IPs, then I think my answer best answers what it appeared as if you were actually trying to do, despite other users' negative votes and comments toward my answer. Can you clarify your question? Are you wanting to go through each IP in order and say something about it or do something with it, or are you going to reference each IP individually with a separate variable? If you are wanting to go in order (within the loop) you only need a single $ip variable per iteration, and there is no need for an array or to reference a specific IP address outside the loop.
– rubynorails
Nov 17 '15 at 1:31




If there are tons of other IPs, then I think my answer best answers what it appeared as if you were actually trying to do, despite other users' negative votes and comments toward my answer. Can you clarify your question? Are you wanting to go through each IP in order and say something about it or do something with it, or are you going to reference each IP individually with a separate variable? If you are wanting to go in order (within the loop) you only need a single $ip variable per iteration, and there is no need for an array or to reference a specific IP address outside the loop.
– rubynorails
Nov 17 '15 at 1:31










5 Answers
5






active

oldest

votes

















up vote
5
down vote



accepted










You almost had it right the first time. The awk answer is good for your specific case, but the reason you were receiving an error is because you were trying to use grep as if it were searching for a file instead of a variable.



Also, when using regular expressions, I always use grep -E just to be safe. I have also heard that backticks are deprecated and should be replaced with $().



The correct way to grep a variable with on shells that support herestrings is using input redirection with 3 of these guys: <, so your grep command ($ip variable) should actually read as follows:



ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"


If it is a file you are searching, I always use a while loop, since it is guaranteed to go line-by-line, whereas for loops often get thrown off if there is any weird spacing. You are also implementing a useless use of cat which could be replace by input redirection as well. Try this:



while read line; do
ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
echo "$ip"
done < "abd"


Also, I don't know what OS or version of grep you are using, but the escape character you had before the curly braces is usually not required whenever I have used this command in the past. It could be from using grep -E or because I use it in quotes and without backticks -- I don't know. You can try it with or without and just see what happens.



Whether you use a for loop or a while loop, that is based on which one works for you in your specific situation and if execution time is of utmost importance. It doesn't appear to me as if OP is trying to assign separate variables to each IP address, but that he wants to assign a variable to each IP address within the line so that he can use it within the loop itself -- in which case he only needs a single $ip variable per iteration. I'm sticking to my guns on this one.






share|improve this answer























  • Can you explain me the second line of the code? What does that $ before grep means?
    – Swatesh Pakhare
    Nov 15 '15 at 4:11










  • @SwateshPakhare It is basically the same thing as the backticks. It sets the $ip variable to the output of the command inside $(). You could actually even say echo "$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")" instead of setting it as a variable beforehand.
    – rubynorails
    Nov 15 '15 at 4:16










  • <<< is a shell extension and won't work under many shells, for example Debian based systems, unless the script is run by bash or zsh or etc. The default system shell on these systems is POSIX compliant and does not recognize <<<.
    – RobertL
    Nov 15 '15 at 8:20












  • The loop in this answer executes a separate grep process for each line of the input file. Even with files of moderate size this loop will take seconds, instead of fractions of seconds, to execute. The larger the file the bigger the performance hit.
    – RobertL
    Nov 15 '15 at 8:24






  • 1




    Don't use answers to take potshots at another user. If you must, take it to chat.
    – terdon
    Nov 16 '15 at 13:46


















up vote
9
down vote













If the IP address is always the second field of that file, you can use awk or cut to extract it.



awk '{print $2}' abd


or



cut -d' ' -f2 abd


If you need to iterate through the IP addresses, the usual for or while loops can be used. For example:



for ip in $(cut -d' ' -f2 abd) ; do ... ; done


or



awk '{print $2}' abd | while read ip ; do ... ; done


Or you can read all the IP addresses into an array:



$ IPAddresses=($(awk '{print $2}' abd))
$ echo "${IPAddresses[@]}"
128.206.6.136 128.206.6.137 23.234.22.106





share|improve this answer





















  • I second the awk, seems much more intuitive in Unix
    – Rui F Ribeiro
    Nov 15 '15 at 8:51


















up vote
6
down vote













grep searches files or standard input for the patterns. You cannot pass data strings to match on the grep command line. Try this:



grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd


If you need to get each IP address in a variable:



grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd |
while read IP
do
echo "$IP"
done


Comparative Performance Testing of @rubynorails's Code



The answer recommends executing a separate invocation of grep on each line of the input file. Let's see how that works out with files of 1000 to 5000 lines. The files abd.1000 and abd.5000 were created by simply replicating the original example file in the question. The original code was changed only to take the filename as a command line argument (${1:?}) instead of the hardcoded "abd".



$ wc -l abd.1000 abd.5000
1000 abd.1000
5000 abd.5000
6000 total


Test the example code in this answer on a 1000 line file:



$ cat ip-example.sh
#!/bin/sh
grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' "${1:?}" |
while read IP
do
echo "$IP"
done

$ time sh ip-example.sh abd.1000 > /dev/null

real 0m0.021s
user 0m0.007s
sys 0m0.017s
$


The above shows that the example in this answer processed a 1000 line file in less than 1/4 second. Now let's see how @rubynorails's example performs:



$ cat rubynorails.sh
#!/bin/bash
while read line; do
ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
echo "$ip"
done < "${1:?}"

$ time bash rubynorails.sh abd.1000 > /dev/null

real 0m3.565s
user 0m0.739s
sys 0m2.936s
$


Hmmm. @rubynorails example executes in 3 1/2 seconds, about 7-8 times slower than the 1/4 second in example for this answer.



Let's up the ante and test with 5000 lines:



$ time sh ip-example.sh abd.5000 > /dev/null

real 0m0.052s
user 0m0.051s
sys 0m0.029s


About twice as long to process 5 times more data.



$ time bash rubynorails.sh abd.5000 > /dev/null

real 0m17.561s
user 0m3.817s
sys 0m14.333s


@rubynorails code takes almost 5 times as long to process 5 times more data than to process 1000 lines of data.



Conclusions



@rubynorails example takes 17 seconds, 34 times longer to process a 5000 line file than the ip-example.sh code in this answer (the other answers on this page should perform similarly to ip-example.h).






share|improve this answer























  • Yaa man it worked. Thank you. Appreciated.
    – Swatesh Pakhare
    Nov 15 '15 at 1:25










  • You can absolutely pass strings (and variables) to grep by using <<< input redirection.
    – rubynorails
    Nov 15 '15 at 4:10










  • There's always a way. However "grep command line" usually means what you see from grep --help, or the grep man page. <<< is a shell extension and extra syntax, so I prefer not to mention it on a question at this technical level.
    – RobertL
    Nov 15 '15 at 4:26










  • @RobertL - my apologies about escaping the pipe. I had seen this behavior in Bash scripts on Debian-based systems and had since made a habit of escaping the ends of all of my lines that had pipes on the next line to avoid errors. Maybe it only occurs in multiple pipes. However, I realize my comment was in error, and I have deleted it. You are correct in the fact that I was making assumptions instead of testing the code in that particular moment. I don't want to put misleading info on this site...such as how it's impossible to grep a variable....just sayin'. I'm sorry. I had to.
    – rubynorails
    Nov 16 '15 at 2:14






  • 1




    Don't use answers to take potshots at another user. If you must, take it to chat. @RobertL follow your own advice. Keep your answers technical and be nice.
    – terdon
    Nov 16 '15 at 13:46




















up vote
4
down vote













I suggest you use AWK for that purpose. It's much more appropriate tool for processing columns.



xieerqi:$ vi ipAddresses

xieerqi:$ awk '{printf $2" "}' ipAddresses
128.206.6.136 128.206.6.137 23.234.22.106
xieerqi:$ ARRAY=($(awk '{printf $2" "}' ipAddresses))

xieerqi:$ echo ${ARRAY[@]}
128.206.6.136 128.206.6.137 23.234.22.106

xieerqi:$ echo ${ARRAY[1]} ${ARRAY[2]}
128.206.6.137 23.234.22.106

xieerqi:$ cat ipAddresses
48878 128.206.6.136
34782 128.206.6.137
12817 23.234.22.106





share|improve this answer




























    up vote
    3
    down vote













    See the first question in the Bash FAQ:



    while read -r _ ip; do printf "%sn" "${ip[@]}"; done < abd
    128.206.6.136
    128.206.6.137
    23.234.22.106





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f243083%2fextracting-ip-address-from-a-text-and-store-it-in-a-variable%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote



      accepted










      You almost had it right the first time. The awk answer is good for your specific case, but the reason you were receiving an error is because you were trying to use grep as if it were searching for a file instead of a variable.



      Also, when using regular expressions, I always use grep -E just to be safe. I have also heard that backticks are deprecated and should be replaced with $().



      The correct way to grep a variable with on shells that support herestrings is using input redirection with 3 of these guys: <, so your grep command ($ip variable) should actually read as follows:



      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"


      If it is a file you are searching, I always use a while loop, since it is guaranteed to go line-by-line, whereas for loops often get thrown off if there is any weird spacing. You are also implementing a useless use of cat which could be replace by input redirection as well. Try this:



      while read line; do
      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
      echo "$ip"
      done < "abd"


      Also, I don't know what OS or version of grep you are using, but the escape character you had before the curly braces is usually not required whenever I have used this command in the past. It could be from using grep -E or because I use it in quotes and without backticks -- I don't know. You can try it with or without and just see what happens.



      Whether you use a for loop or a while loop, that is based on which one works for you in your specific situation and if execution time is of utmost importance. It doesn't appear to me as if OP is trying to assign separate variables to each IP address, but that he wants to assign a variable to each IP address within the line so that he can use it within the loop itself -- in which case he only needs a single $ip variable per iteration. I'm sticking to my guns on this one.






      share|improve this answer























      • Can you explain me the second line of the code? What does that $ before grep means?
        – Swatesh Pakhare
        Nov 15 '15 at 4:11










      • @SwateshPakhare It is basically the same thing as the backticks. It sets the $ip variable to the output of the command inside $(). You could actually even say echo "$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")" instead of setting it as a variable beforehand.
        – rubynorails
        Nov 15 '15 at 4:16










      • <<< is a shell extension and won't work under many shells, for example Debian based systems, unless the script is run by bash or zsh or etc. The default system shell on these systems is POSIX compliant and does not recognize <<<.
        – RobertL
        Nov 15 '15 at 8:20












      • The loop in this answer executes a separate grep process for each line of the input file. Even with files of moderate size this loop will take seconds, instead of fractions of seconds, to execute. The larger the file the bigger the performance hit.
        – RobertL
        Nov 15 '15 at 8:24






      • 1




        Don't use answers to take potshots at another user. If you must, take it to chat.
        – terdon
        Nov 16 '15 at 13:46















      up vote
      5
      down vote



      accepted










      You almost had it right the first time. The awk answer is good for your specific case, but the reason you were receiving an error is because you were trying to use grep as if it were searching for a file instead of a variable.



      Also, when using regular expressions, I always use grep -E just to be safe. I have also heard that backticks are deprecated and should be replaced with $().



      The correct way to grep a variable with on shells that support herestrings is using input redirection with 3 of these guys: <, so your grep command ($ip variable) should actually read as follows:



      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"


      If it is a file you are searching, I always use a while loop, since it is guaranteed to go line-by-line, whereas for loops often get thrown off if there is any weird spacing. You are also implementing a useless use of cat which could be replace by input redirection as well. Try this:



      while read line; do
      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
      echo "$ip"
      done < "abd"


      Also, I don't know what OS or version of grep you are using, but the escape character you had before the curly braces is usually not required whenever I have used this command in the past. It could be from using grep -E or because I use it in quotes and without backticks -- I don't know. You can try it with or without and just see what happens.



      Whether you use a for loop or a while loop, that is based on which one works for you in your specific situation and if execution time is of utmost importance. It doesn't appear to me as if OP is trying to assign separate variables to each IP address, but that he wants to assign a variable to each IP address within the line so that he can use it within the loop itself -- in which case he only needs a single $ip variable per iteration. I'm sticking to my guns on this one.






      share|improve this answer























      • Can you explain me the second line of the code? What does that $ before grep means?
        – Swatesh Pakhare
        Nov 15 '15 at 4:11










      • @SwateshPakhare It is basically the same thing as the backticks. It sets the $ip variable to the output of the command inside $(). You could actually even say echo "$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")" instead of setting it as a variable beforehand.
        – rubynorails
        Nov 15 '15 at 4:16










      • <<< is a shell extension and won't work under many shells, for example Debian based systems, unless the script is run by bash or zsh or etc. The default system shell on these systems is POSIX compliant and does not recognize <<<.
        – RobertL
        Nov 15 '15 at 8:20












      • The loop in this answer executes a separate grep process for each line of the input file. Even with files of moderate size this loop will take seconds, instead of fractions of seconds, to execute. The larger the file the bigger the performance hit.
        – RobertL
        Nov 15 '15 at 8:24






      • 1




        Don't use answers to take potshots at another user. If you must, take it to chat.
        – terdon
        Nov 16 '15 at 13:46













      up vote
      5
      down vote



      accepted







      up vote
      5
      down vote



      accepted






      You almost had it right the first time. The awk answer is good for your specific case, but the reason you were receiving an error is because you were trying to use grep as if it were searching for a file instead of a variable.



      Also, when using regular expressions, I always use grep -E just to be safe. I have also heard that backticks are deprecated and should be replaced with $().



      The correct way to grep a variable with on shells that support herestrings is using input redirection with 3 of these guys: <, so your grep command ($ip variable) should actually read as follows:



      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"


      If it is a file you are searching, I always use a while loop, since it is guaranteed to go line-by-line, whereas for loops often get thrown off if there is any weird spacing. You are also implementing a useless use of cat which could be replace by input redirection as well. Try this:



      while read line; do
      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
      echo "$ip"
      done < "abd"


      Also, I don't know what OS or version of grep you are using, but the escape character you had before the curly braces is usually not required whenever I have used this command in the past. It could be from using grep -E or because I use it in quotes and without backticks -- I don't know. You can try it with or without and just see what happens.



      Whether you use a for loop or a while loop, that is based on which one works for you in your specific situation and if execution time is of utmost importance. It doesn't appear to me as if OP is trying to assign separate variables to each IP address, but that he wants to assign a variable to each IP address within the line so that he can use it within the loop itself -- in which case he only needs a single $ip variable per iteration. I'm sticking to my guns on this one.






      share|improve this answer














      You almost had it right the first time. The awk answer is good for your specific case, but the reason you were receiving an error is because you were trying to use grep as if it were searching for a file instead of a variable.



      Also, when using regular expressions, I always use grep -E just to be safe. I have also heard that backticks are deprecated and should be replaced with $().



      The correct way to grep a variable with on shells that support herestrings is using input redirection with 3 of these guys: <, so your grep command ($ip variable) should actually read as follows:



      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"


      If it is a file you are searching, I always use a while loop, since it is guaranteed to go line-by-line, whereas for loops often get thrown off if there is any weird spacing. You are also implementing a useless use of cat which could be replace by input redirection as well. Try this:



      while read line; do
      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
      echo "$ip"
      done < "abd"


      Also, I don't know what OS or version of grep you are using, but the escape character you had before the curly braces is usually not required whenever I have used this command in the past. It could be from using grep -E or because I use it in quotes and without backticks -- I don't know. You can try it with or without and just see what happens.



      Whether you use a for loop or a while loop, that is based on which one works for you in your specific situation and if execution time is of utmost importance. It doesn't appear to me as if OP is trying to assign separate variables to each IP address, but that he wants to assign a variable to each IP address within the line so that he can use it within the loop itself -- in which case he only needs a single $ip variable per iteration. I'm sticking to my guns on this one.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 16 '15 at 13:45









      terdon

      126k31242418




      126k31242418










      answered Nov 15 '15 at 3:58









      rubynorails

      1,227516




      1,227516












      • Can you explain me the second line of the code? What does that $ before grep means?
        – Swatesh Pakhare
        Nov 15 '15 at 4:11










      • @SwateshPakhare It is basically the same thing as the backticks. It sets the $ip variable to the output of the command inside $(). You could actually even say echo "$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")" instead of setting it as a variable beforehand.
        – rubynorails
        Nov 15 '15 at 4:16










      • <<< is a shell extension and won't work under many shells, for example Debian based systems, unless the script is run by bash or zsh or etc. The default system shell on these systems is POSIX compliant and does not recognize <<<.
        – RobertL
        Nov 15 '15 at 8:20












      • The loop in this answer executes a separate grep process for each line of the input file. Even with files of moderate size this loop will take seconds, instead of fractions of seconds, to execute. The larger the file the bigger the performance hit.
        – RobertL
        Nov 15 '15 at 8:24






      • 1




        Don't use answers to take potshots at another user. If you must, take it to chat.
        – terdon
        Nov 16 '15 at 13:46


















      • Can you explain me the second line of the code? What does that $ before grep means?
        – Swatesh Pakhare
        Nov 15 '15 at 4:11










      • @SwateshPakhare It is basically the same thing as the backticks. It sets the $ip variable to the output of the command inside $(). You could actually even say echo "$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")" instead of setting it as a variable beforehand.
        – rubynorails
        Nov 15 '15 at 4:16










      • <<< is a shell extension and won't work under many shells, for example Debian based systems, unless the script is run by bash or zsh or etc. The default system shell on these systems is POSIX compliant and does not recognize <<<.
        – RobertL
        Nov 15 '15 at 8:20












      • The loop in this answer executes a separate grep process for each line of the input file. Even with files of moderate size this loop will take seconds, instead of fractions of seconds, to execute. The larger the file the bigger the performance hit.
        – RobertL
        Nov 15 '15 at 8:24






      • 1




        Don't use answers to take potshots at another user. If you must, take it to chat.
        – terdon
        Nov 16 '15 at 13:46
















      Can you explain me the second line of the code? What does that $ before grep means?
      – Swatesh Pakhare
      Nov 15 '15 at 4:11




      Can you explain me the second line of the code? What does that $ before grep means?
      – Swatesh Pakhare
      Nov 15 '15 at 4:11












      @SwateshPakhare It is basically the same thing as the backticks. It sets the $ip variable to the output of the command inside $(). You could actually even say echo "$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")" instead of setting it as a variable beforehand.
      – rubynorails
      Nov 15 '15 at 4:16




      @SwateshPakhare It is basically the same thing as the backticks. It sets the $ip variable to the output of the command inside $(). You could actually even say echo "$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")" instead of setting it as a variable beforehand.
      – rubynorails
      Nov 15 '15 at 4:16












      <<< is a shell extension and won't work under many shells, for example Debian based systems, unless the script is run by bash or zsh or etc. The default system shell on these systems is POSIX compliant and does not recognize <<<.
      – RobertL
      Nov 15 '15 at 8:20






      <<< is a shell extension and won't work under many shells, for example Debian based systems, unless the script is run by bash or zsh or etc. The default system shell on these systems is POSIX compliant and does not recognize <<<.
      – RobertL
      Nov 15 '15 at 8:20














      The loop in this answer executes a separate grep process for each line of the input file. Even with files of moderate size this loop will take seconds, instead of fractions of seconds, to execute. The larger the file the bigger the performance hit.
      – RobertL
      Nov 15 '15 at 8:24




      The loop in this answer executes a separate grep process for each line of the input file. Even with files of moderate size this loop will take seconds, instead of fractions of seconds, to execute. The larger the file the bigger the performance hit.
      – RobertL
      Nov 15 '15 at 8:24




      1




      1




      Don't use answers to take potshots at another user. If you must, take it to chat.
      – terdon
      Nov 16 '15 at 13:46




      Don't use answers to take potshots at another user. If you must, take it to chat.
      – terdon
      Nov 16 '15 at 13:46












      up vote
      9
      down vote













      If the IP address is always the second field of that file, you can use awk or cut to extract it.



      awk '{print $2}' abd


      or



      cut -d' ' -f2 abd


      If you need to iterate through the IP addresses, the usual for or while loops can be used. For example:



      for ip in $(cut -d' ' -f2 abd) ; do ... ; done


      or



      awk '{print $2}' abd | while read ip ; do ... ; done


      Or you can read all the IP addresses into an array:



      $ IPAddresses=($(awk '{print $2}' abd))
      $ echo "${IPAddresses[@]}"
      128.206.6.136 128.206.6.137 23.234.22.106





      share|improve this answer





















      • I second the awk, seems much more intuitive in Unix
        – Rui F Ribeiro
        Nov 15 '15 at 8:51















      up vote
      9
      down vote













      If the IP address is always the second field of that file, you can use awk or cut to extract it.



      awk '{print $2}' abd


      or



      cut -d' ' -f2 abd


      If you need to iterate through the IP addresses, the usual for or while loops can be used. For example:



      for ip in $(cut -d' ' -f2 abd) ; do ... ; done


      or



      awk '{print $2}' abd | while read ip ; do ... ; done


      Or you can read all the IP addresses into an array:



      $ IPAddresses=($(awk '{print $2}' abd))
      $ echo "${IPAddresses[@]}"
      128.206.6.136 128.206.6.137 23.234.22.106





      share|improve this answer





















      • I second the awk, seems much more intuitive in Unix
        – Rui F Ribeiro
        Nov 15 '15 at 8:51













      up vote
      9
      down vote










      up vote
      9
      down vote









      If the IP address is always the second field of that file, you can use awk or cut to extract it.



      awk '{print $2}' abd


      or



      cut -d' ' -f2 abd


      If you need to iterate through the IP addresses, the usual for or while loops can be used. For example:



      for ip in $(cut -d' ' -f2 abd) ; do ... ; done


      or



      awk '{print $2}' abd | while read ip ; do ... ; done


      Or you can read all the IP addresses into an array:



      $ IPAddresses=($(awk '{print $2}' abd))
      $ echo "${IPAddresses[@]}"
      128.206.6.136 128.206.6.137 23.234.22.106





      share|improve this answer












      If the IP address is always the second field of that file, you can use awk or cut to extract it.



      awk '{print $2}' abd


      or



      cut -d' ' -f2 abd


      If you need to iterate through the IP addresses, the usual for or while loops can be used. For example:



      for ip in $(cut -d' ' -f2 abd) ; do ... ; done


      or



      awk '{print $2}' abd | while read ip ; do ... ; done


      Or you can read all the IP addresses into an array:



      $ IPAddresses=($(awk '{print $2}' abd))
      $ echo "${IPAddresses[@]}"
      128.206.6.136 128.206.6.137 23.234.22.106






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 15 '15 at 2:53









      cas

      38.3k44898




      38.3k44898












      • I second the awk, seems much more intuitive in Unix
        – Rui F Ribeiro
        Nov 15 '15 at 8:51


















      • I second the awk, seems much more intuitive in Unix
        – Rui F Ribeiro
        Nov 15 '15 at 8:51
















      I second the awk, seems much more intuitive in Unix
      – Rui F Ribeiro
      Nov 15 '15 at 8:51




      I second the awk, seems much more intuitive in Unix
      – Rui F Ribeiro
      Nov 15 '15 at 8:51










      up vote
      6
      down vote













      grep searches files or standard input for the patterns. You cannot pass data strings to match on the grep command line. Try this:



      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd


      If you need to get each IP address in a variable:



      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd |
      while read IP
      do
      echo "$IP"
      done


      Comparative Performance Testing of @rubynorails's Code



      The answer recommends executing a separate invocation of grep on each line of the input file. Let's see how that works out with files of 1000 to 5000 lines. The files abd.1000 and abd.5000 were created by simply replicating the original example file in the question. The original code was changed only to take the filename as a command line argument (${1:?}) instead of the hardcoded "abd".



      $ wc -l abd.1000 abd.5000
      1000 abd.1000
      5000 abd.5000
      6000 total


      Test the example code in this answer on a 1000 line file:



      $ cat ip-example.sh
      #!/bin/sh
      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' "${1:?}" |
      while read IP
      do
      echo "$IP"
      done

      $ time sh ip-example.sh abd.1000 > /dev/null

      real 0m0.021s
      user 0m0.007s
      sys 0m0.017s
      $


      The above shows that the example in this answer processed a 1000 line file in less than 1/4 second. Now let's see how @rubynorails's example performs:



      $ cat rubynorails.sh
      #!/bin/bash
      while read line; do
      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
      echo "$ip"
      done < "${1:?}"

      $ time bash rubynorails.sh abd.1000 > /dev/null

      real 0m3.565s
      user 0m0.739s
      sys 0m2.936s
      $


      Hmmm. @rubynorails example executes in 3 1/2 seconds, about 7-8 times slower than the 1/4 second in example for this answer.



      Let's up the ante and test with 5000 lines:



      $ time sh ip-example.sh abd.5000 > /dev/null

      real 0m0.052s
      user 0m0.051s
      sys 0m0.029s


      About twice as long to process 5 times more data.



      $ time bash rubynorails.sh abd.5000 > /dev/null

      real 0m17.561s
      user 0m3.817s
      sys 0m14.333s


      @rubynorails code takes almost 5 times as long to process 5 times more data than to process 1000 lines of data.



      Conclusions



      @rubynorails example takes 17 seconds, 34 times longer to process a 5000 line file than the ip-example.sh code in this answer (the other answers on this page should perform similarly to ip-example.h).






      share|improve this answer























      • Yaa man it worked. Thank you. Appreciated.
        – Swatesh Pakhare
        Nov 15 '15 at 1:25










      • You can absolutely pass strings (and variables) to grep by using <<< input redirection.
        – rubynorails
        Nov 15 '15 at 4:10










      • There's always a way. However "grep command line" usually means what you see from grep --help, or the grep man page. <<< is a shell extension and extra syntax, so I prefer not to mention it on a question at this technical level.
        – RobertL
        Nov 15 '15 at 4:26










      • @RobertL - my apologies about escaping the pipe. I had seen this behavior in Bash scripts on Debian-based systems and had since made a habit of escaping the ends of all of my lines that had pipes on the next line to avoid errors. Maybe it only occurs in multiple pipes. However, I realize my comment was in error, and I have deleted it. You are correct in the fact that I was making assumptions instead of testing the code in that particular moment. I don't want to put misleading info on this site...such as how it's impossible to grep a variable....just sayin'. I'm sorry. I had to.
        – rubynorails
        Nov 16 '15 at 2:14






      • 1




        Don't use answers to take potshots at another user. If you must, take it to chat. @RobertL follow your own advice. Keep your answers technical and be nice.
        – terdon
        Nov 16 '15 at 13:46

















      up vote
      6
      down vote













      grep searches files or standard input for the patterns. You cannot pass data strings to match on the grep command line. Try this:



      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd


      If you need to get each IP address in a variable:



      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd |
      while read IP
      do
      echo "$IP"
      done


      Comparative Performance Testing of @rubynorails's Code



      The answer recommends executing a separate invocation of grep on each line of the input file. Let's see how that works out with files of 1000 to 5000 lines. The files abd.1000 and abd.5000 were created by simply replicating the original example file in the question. The original code was changed only to take the filename as a command line argument (${1:?}) instead of the hardcoded "abd".



      $ wc -l abd.1000 abd.5000
      1000 abd.1000
      5000 abd.5000
      6000 total


      Test the example code in this answer on a 1000 line file:



      $ cat ip-example.sh
      #!/bin/sh
      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' "${1:?}" |
      while read IP
      do
      echo "$IP"
      done

      $ time sh ip-example.sh abd.1000 > /dev/null

      real 0m0.021s
      user 0m0.007s
      sys 0m0.017s
      $


      The above shows that the example in this answer processed a 1000 line file in less than 1/4 second. Now let's see how @rubynorails's example performs:



      $ cat rubynorails.sh
      #!/bin/bash
      while read line; do
      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
      echo "$ip"
      done < "${1:?}"

      $ time bash rubynorails.sh abd.1000 > /dev/null

      real 0m3.565s
      user 0m0.739s
      sys 0m2.936s
      $


      Hmmm. @rubynorails example executes in 3 1/2 seconds, about 7-8 times slower than the 1/4 second in example for this answer.



      Let's up the ante and test with 5000 lines:



      $ time sh ip-example.sh abd.5000 > /dev/null

      real 0m0.052s
      user 0m0.051s
      sys 0m0.029s


      About twice as long to process 5 times more data.



      $ time bash rubynorails.sh abd.5000 > /dev/null

      real 0m17.561s
      user 0m3.817s
      sys 0m14.333s


      @rubynorails code takes almost 5 times as long to process 5 times more data than to process 1000 lines of data.



      Conclusions



      @rubynorails example takes 17 seconds, 34 times longer to process a 5000 line file than the ip-example.sh code in this answer (the other answers on this page should perform similarly to ip-example.h).






      share|improve this answer























      • Yaa man it worked. Thank you. Appreciated.
        – Swatesh Pakhare
        Nov 15 '15 at 1:25










      • You can absolutely pass strings (and variables) to grep by using <<< input redirection.
        – rubynorails
        Nov 15 '15 at 4:10










      • There's always a way. However "grep command line" usually means what you see from grep --help, or the grep man page. <<< is a shell extension and extra syntax, so I prefer not to mention it on a question at this technical level.
        – RobertL
        Nov 15 '15 at 4:26










      • @RobertL - my apologies about escaping the pipe. I had seen this behavior in Bash scripts on Debian-based systems and had since made a habit of escaping the ends of all of my lines that had pipes on the next line to avoid errors. Maybe it only occurs in multiple pipes. However, I realize my comment was in error, and I have deleted it. You are correct in the fact that I was making assumptions instead of testing the code in that particular moment. I don't want to put misleading info on this site...such as how it's impossible to grep a variable....just sayin'. I'm sorry. I had to.
        – rubynorails
        Nov 16 '15 at 2:14






      • 1




        Don't use answers to take potshots at another user. If you must, take it to chat. @RobertL follow your own advice. Keep your answers technical and be nice.
        – terdon
        Nov 16 '15 at 13:46















      up vote
      6
      down vote










      up vote
      6
      down vote









      grep searches files or standard input for the patterns. You cannot pass data strings to match on the grep command line. Try this:



      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd


      If you need to get each IP address in a variable:



      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd |
      while read IP
      do
      echo "$IP"
      done


      Comparative Performance Testing of @rubynorails's Code



      The answer recommends executing a separate invocation of grep on each line of the input file. Let's see how that works out with files of 1000 to 5000 lines. The files abd.1000 and abd.5000 were created by simply replicating the original example file in the question. The original code was changed only to take the filename as a command line argument (${1:?}) instead of the hardcoded "abd".



      $ wc -l abd.1000 abd.5000
      1000 abd.1000
      5000 abd.5000
      6000 total


      Test the example code in this answer on a 1000 line file:



      $ cat ip-example.sh
      #!/bin/sh
      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' "${1:?}" |
      while read IP
      do
      echo "$IP"
      done

      $ time sh ip-example.sh abd.1000 > /dev/null

      real 0m0.021s
      user 0m0.007s
      sys 0m0.017s
      $


      The above shows that the example in this answer processed a 1000 line file in less than 1/4 second. Now let's see how @rubynorails's example performs:



      $ cat rubynorails.sh
      #!/bin/bash
      while read line; do
      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
      echo "$ip"
      done < "${1:?}"

      $ time bash rubynorails.sh abd.1000 > /dev/null

      real 0m3.565s
      user 0m0.739s
      sys 0m2.936s
      $


      Hmmm. @rubynorails example executes in 3 1/2 seconds, about 7-8 times slower than the 1/4 second in example for this answer.



      Let's up the ante and test with 5000 lines:



      $ time sh ip-example.sh abd.5000 > /dev/null

      real 0m0.052s
      user 0m0.051s
      sys 0m0.029s


      About twice as long to process 5 times more data.



      $ time bash rubynorails.sh abd.5000 > /dev/null

      real 0m17.561s
      user 0m3.817s
      sys 0m14.333s


      @rubynorails code takes almost 5 times as long to process 5 times more data than to process 1000 lines of data.



      Conclusions



      @rubynorails example takes 17 seconds, 34 times longer to process a 5000 line file than the ip-example.sh code in this answer (the other answers on this page should perform similarly to ip-example.h).






      share|improve this answer














      grep searches files or standard input for the patterns. You cannot pass data strings to match on the grep command line. Try this:



      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd


      If you need to get each IP address in a variable:



      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' abd |
      while read IP
      do
      echo "$IP"
      done


      Comparative Performance Testing of @rubynorails's Code



      The answer recommends executing a separate invocation of grep on each line of the input file. Let's see how that works out with files of 1000 to 5000 lines. The files abd.1000 and abd.5000 were created by simply replicating the original example file in the question. The original code was changed only to take the filename as a command line argument (${1:?}) instead of the hardcoded "abd".



      $ wc -l abd.1000 abd.5000
      1000 abd.1000
      5000 abd.5000
      6000 total


      Test the example code in this answer on a 1000 line file:



      $ cat ip-example.sh
      #!/bin/sh
      grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' "${1:?}" |
      while read IP
      do
      echo "$IP"
      done

      $ time sh ip-example.sh abd.1000 > /dev/null

      real 0m0.021s
      user 0m0.007s
      sys 0m0.017s
      $


      The above shows that the example in this answer processed a 1000 line file in less than 1/4 second. Now let's see how @rubynorails's example performs:



      $ cat rubynorails.sh
      #!/bin/bash
      while read line; do
      ip="$(grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' <<< "$line")"
      echo "$ip"
      done < "${1:?}"

      $ time bash rubynorails.sh abd.1000 > /dev/null

      real 0m3.565s
      user 0m0.739s
      sys 0m2.936s
      $


      Hmmm. @rubynorails example executes in 3 1/2 seconds, about 7-8 times slower than the 1/4 second in example for this answer.



      Let's up the ante and test with 5000 lines:



      $ time sh ip-example.sh abd.5000 > /dev/null

      real 0m0.052s
      user 0m0.051s
      sys 0m0.029s


      About twice as long to process 5 times more data.



      $ time bash rubynorails.sh abd.5000 > /dev/null

      real 0m17.561s
      user 0m3.817s
      sys 0m14.333s


      @rubynorails code takes almost 5 times as long to process 5 times more data than to process 1000 lines of data.



      Conclusions



      @rubynorails example takes 17 seconds, 34 times longer to process a 5000 line file than the ip-example.sh code in this answer (the other answers on this page should perform similarly to ip-example.h).







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 13 '17 at 12:36









      Community

      1




      1










      answered Nov 15 '15 at 1:17









      RobertL

      4,788624




      4,788624












      • Yaa man it worked. Thank you. Appreciated.
        – Swatesh Pakhare
        Nov 15 '15 at 1:25










      • You can absolutely pass strings (and variables) to grep by using <<< input redirection.
        – rubynorails
        Nov 15 '15 at 4:10










      • There's always a way. However "grep command line" usually means what you see from grep --help, or the grep man page. <<< is a shell extension and extra syntax, so I prefer not to mention it on a question at this technical level.
        – RobertL
        Nov 15 '15 at 4:26










      • @RobertL - my apologies about escaping the pipe. I had seen this behavior in Bash scripts on Debian-based systems and had since made a habit of escaping the ends of all of my lines that had pipes on the next line to avoid errors. Maybe it only occurs in multiple pipes. However, I realize my comment was in error, and I have deleted it. You are correct in the fact that I was making assumptions instead of testing the code in that particular moment. I don't want to put misleading info on this site...such as how it's impossible to grep a variable....just sayin'. I'm sorry. I had to.
        – rubynorails
        Nov 16 '15 at 2:14






      • 1




        Don't use answers to take potshots at another user. If you must, take it to chat. @RobertL follow your own advice. Keep your answers technical and be nice.
        – terdon
        Nov 16 '15 at 13:46




















      • Yaa man it worked. Thank you. Appreciated.
        – Swatesh Pakhare
        Nov 15 '15 at 1:25










      • You can absolutely pass strings (and variables) to grep by using <<< input redirection.
        – rubynorails
        Nov 15 '15 at 4:10










      • There's always a way. However "grep command line" usually means what you see from grep --help, or the grep man page. <<< is a shell extension and extra syntax, so I prefer not to mention it on a question at this technical level.
        – RobertL
        Nov 15 '15 at 4:26










      • @RobertL - my apologies about escaping the pipe. I had seen this behavior in Bash scripts on Debian-based systems and had since made a habit of escaping the ends of all of my lines that had pipes on the next line to avoid errors. Maybe it only occurs in multiple pipes. However, I realize my comment was in error, and I have deleted it. You are correct in the fact that I was making assumptions instead of testing the code in that particular moment. I don't want to put misleading info on this site...such as how it's impossible to grep a variable....just sayin'. I'm sorry. I had to.
        – rubynorails
        Nov 16 '15 at 2:14






      • 1




        Don't use answers to take potshots at another user. If you must, take it to chat. @RobertL follow your own advice. Keep your answers technical and be nice.
        – terdon
        Nov 16 '15 at 13:46


















      Yaa man it worked. Thank you. Appreciated.
      – Swatesh Pakhare
      Nov 15 '15 at 1:25




      Yaa man it worked. Thank you. Appreciated.
      – Swatesh Pakhare
      Nov 15 '15 at 1:25












      You can absolutely pass strings (and variables) to grep by using <<< input redirection.
      – rubynorails
      Nov 15 '15 at 4:10




      You can absolutely pass strings (and variables) to grep by using <<< input redirection.
      – rubynorails
      Nov 15 '15 at 4:10












      There's always a way. However "grep command line" usually means what you see from grep --help, or the grep man page. <<< is a shell extension and extra syntax, so I prefer not to mention it on a question at this technical level.
      – RobertL
      Nov 15 '15 at 4:26




      There's always a way. However "grep command line" usually means what you see from grep --help, or the grep man page. <<< is a shell extension and extra syntax, so I prefer not to mention it on a question at this technical level.
      – RobertL
      Nov 15 '15 at 4:26












      @RobertL - my apologies about escaping the pipe. I had seen this behavior in Bash scripts on Debian-based systems and had since made a habit of escaping the ends of all of my lines that had pipes on the next line to avoid errors. Maybe it only occurs in multiple pipes. However, I realize my comment was in error, and I have deleted it. You are correct in the fact that I was making assumptions instead of testing the code in that particular moment. I don't want to put misleading info on this site...such as how it's impossible to grep a variable....just sayin'. I'm sorry. I had to.
      – rubynorails
      Nov 16 '15 at 2:14




      @RobertL - my apologies about escaping the pipe. I had seen this behavior in Bash scripts on Debian-based systems and had since made a habit of escaping the ends of all of my lines that had pipes on the next line to avoid errors. Maybe it only occurs in multiple pipes. However, I realize my comment was in error, and I have deleted it. You are correct in the fact that I was making assumptions instead of testing the code in that particular moment. I don't want to put misleading info on this site...such as how it's impossible to grep a variable....just sayin'. I'm sorry. I had to.
      – rubynorails
      Nov 16 '15 at 2:14




      1




      1




      Don't use answers to take potshots at another user. If you must, take it to chat. @RobertL follow your own advice. Keep your answers technical and be nice.
      – terdon
      Nov 16 '15 at 13:46






      Don't use answers to take potshots at another user. If you must, take it to chat. @RobertL follow your own advice. Keep your answers technical and be nice.
      – terdon
      Nov 16 '15 at 13:46












      up vote
      4
      down vote













      I suggest you use AWK for that purpose. It's much more appropriate tool for processing columns.



      xieerqi:$ vi ipAddresses

      xieerqi:$ awk '{printf $2" "}' ipAddresses
      128.206.6.136 128.206.6.137 23.234.22.106
      xieerqi:$ ARRAY=($(awk '{printf $2" "}' ipAddresses))

      xieerqi:$ echo ${ARRAY[@]}
      128.206.6.136 128.206.6.137 23.234.22.106

      xieerqi:$ echo ${ARRAY[1]} ${ARRAY[2]}
      128.206.6.137 23.234.22.106

      xieerqi:$ cat ipAddresses
      48878 128.206.6.136
      34782 128.206.6.137
      12817 23.234.22.106





      share|improve this answer

























        up vote
        4
        down vote













        I suggest you use AWK for that purpose. It's much more appropriate tool for processing columns.



        xieerqi:$ vi ipAddresses

        xieerqi:$ awk '{printf $2" "}' ipAddresses
        128.206.6.136 128.206.6.137 23.234.22.106
        xieerqi:$ ARRAY=($(awk '{printf $2" "}' ipAddresses))

        xieerqi:$ echo ${ARRAY[@]}
        128.206.6.136 128.206.6.137 23.234.22.106

        xieerqi:$ echo ${ARRAY[1]} ${ARRAY[2]}
        128.206.6.137 23.234.22.106

        xieerqi:$ cat ipAddresses
        48878 128.206.6.136
        34782 128.206.6.137
        12817 23.234.22.106





        share|improve this answer























          up vote
          4
          down vote










          up vote
          4
          down vote









          I suggest you use AWK for that purpose. It's much more appropriate tool for processing columns.



          xieerqi:$ vi ipAddresses

          xieerqi:$ awk '{printf $2" "}' ipAddresses
          128.206.6.136 128.206.6.137 23.234.22.106
          xieerqi:$ ARRAY=($(awk '{printf $2" "}' ipAddresses))

          xieerqi:$ echo ${ARRAY[@]}
          128.206.6.136 128.206.6.137 23.234.22.106

          xieerqi:$ echo ${ARRAY[1]} ${ARRAY[2]}
          128.206.6.137 23.234.22.106

          xieerqi:$ cat ipAddresses
          48878 128.206.6.136
          34782 128.206.6.137
          12817 23.234.22.106





          share|improve this answer












          I suggest you use AWK for that purpose. It's much more appropriate tool for processing columns.



          xieerqi:$ vi ipAddresses

          xieerqi:$ awk '{printf $2" "}' ipAddresses
          128.206.6.136 128.206.6.137 23.234.22.106
          xieerqi:$ ARRAY=($(awk '{printf $2" "}' ipAddresses))

          xieerqi:$ echo ${ARRAY[@]}
          128.206.6.136 128.206.6.137 23.234.22.106

          xieerqi:$ echo ${ARRAY[1]} ${ARRAY[2]}
          128.206.6.137 23.234.22.106

          xieerqi:$ cat ipAddresses
          48878 128.206.6.136
          34782 128.206.6.137
          12817 23.234.22.106






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '15 at 1:22









          Sergiy Kolodyazhnyy

          8,11212051




          8,11212051






















              up vote
              3
              down vote













              See the first question in the Bash FAQ:



              while read -r _ ip; do printf "%sn" "${ip[@]}"; done < abd
              128.206.6.136
              128.206.6.137
              23.234.22.106





              share|improve this answer

























                up vote
                3
                down vote













                See the first question in the Bash FAQ:



                while read -r _ ip; do printf "%sn" "${ip[@]}"; done < abd
                128.206.6.136
                128.206.6.137
                23.234.22.106





                share|improve this answer























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  See the first question in the Bash FAQ:



                  while read -r _ ip; do printf "%sn" "${ip[@]}"; done < abd
                  128.206.6.136
                  128.206.6.137
                  23.234.22.106





                  share|improve this answer












                  See the first question in the Bash FAQ:



                  while read -r _ ip; do printf "%sn" "${ip[@]}"; done < abd
                  128.206.6.136
                  128.206.6.137
                  23.234.22.106






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '15 at 2:02









                  jasonwryan

                  48.6k14133182




                  48.6k14133182






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f243083%2fextracting-ip-address-from-a-text-and-store-it-in-a-variable%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      サソリ

                      広島県道265号伴広島線

                      Accessing regular linux commands in Huawei's Dopra Linux