简体   繁体   English

bash上的命令变量内的变量

[英]Variable inside a command variable on bash

I'm trying to extract reference numbers for ip addresses which are not updated using BASH on centos, I have a CSV with not updated addresses (IPs_withoutrefn.csv) where I extract the second column, and then we compare this value against another CSV (IP-references.csv) to extract the reference number. 我正在尝试为未在centos上使用BASH更新的IP地址提取参考号,我有一个未更新地址的CSV(IPs_withoutrefn.csv),我在其中提取了第二列,然后将该值与另一个CSV比较( IP-references.csv)以提取参考号。

For this I was trying to do a variable in this way, but the output is always empty, if I try this command on shell without using a variable its working properly. 为此,我试图以这种方式执行变量,但是如果我在shell上尝试使用此命令而不使用变量,则输出始终为空,它的工作正常。

REFNO=$(grep "$IP" ../IP-references.csv | awk 'BEGIN{FS=OFS=";"}{sub(/\r$/,"");print $2"\r"}')

I already tried to convert this grep in a function, and to change the "$IP" inside the grep on different ways I read around here like ("^$IP;" or "$IP;" or ${IP} ... ) but the result its always same. 我已经尝试过将这个grep转换为函数,并以我在此处阅读的不同方式(例如“ ^ $ IP;”或“ $ IP;”或$ {IP})来更改grep内部的“ $ IP”。 ),但结果始终相同。

And this is the full code I'm using. 这是我正在使用的完整代码。

while IFS=";" read -r col1 col2

do

  #IP to variable#

        IP=$col2

        echo "$IP"

  #We search the IP and we extract the REFNO##

        REFNO=$(grep "$IP" ../IP-references.csv | awk 'BEGIN{FS=OFS=";"}{sub(/\r$/,"");print $2"\r"}')

        echo "$REFNO"

        echo "$IP"

done < "../IPs_withoutrefn.csv"

I hope its clearly enough for understanding, if not, please let me now. 我希望它足够清晰,足以理解,如果没有,请让我现在。

IPs_withoutrefn.csv sample: IPs_withoutrefn.csv示例:

  • ;1.1.1.1 ; 1.1.1.1
  • ; ;
  • ;1.1.1.2 ; 1.1.1.2

IP-references.csv sample IP-references.csv示例

Client1;89345013745;modelno;status;node1;sect1;node2;sect2;;1.1.1.1;1.1.1.1;;;datexpiration; 客户端1; 89345013745; modelno;状态;节点1; SECT1;节点2; sect2的;; 1.1.1.1; 1.1.1.1 ;;; datexpiration;

Client2;89345013746;modelno;status;node1;sect1;node2;sect2;;1.1.1.2;1.1.1.2;;;datexpiration; 客户机2; 89345013746; modelno;状态;节点1; SECT1;节点2; sect2的;; 1.1.1.2; 1.1.1.2 ;;; datexpiration;

Client3;89345013747;modelno;status;node1;sect1;node2;sect2;;1.1.1.3;1.1.1.3;;;datexpiration; Client3; 89345013747; modelno;状态;节点1; SECT1;节点2; sect2的;; 1.1.1.3; 1.1.1.3 ;;; datexpiration;

Thank you. 谢谢。

cut -d ";" -f 2 IPs_withoutrefn.csv | xargs -I {} grep {} IP-references.csv

This would print all lines in IP-references that match IP adresses from the first file. 这将在IP引用中打印与第一个文件中的IP地址匹配的所有行。

You can further improve the output by adding | cut -d ";" -f 1,11 您可以通过添加| cut -d ";" -f 1,11来进一步改善输出| cut -d ";" -f 1,11 | cut -d ";" -f 1,11 | cut -d ";" -f 1,11 to list in format | cut -d ";" -f 1,11以格式列出

cut -d ";" -f 2 IPs_withoutrefn.csv  | xargs -I {} grep {} IP-references.csv | cut -d ";" -f 1,11

Client1;1.1.1.1 客户端1; 1.1.1.1

Client2;1.1.1.2 客户端2; 1.1.1.2

-d is for delimiter, -f for filed numbers -d用于分隔符,-f用于域号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM