awk引用外部變量問題

在awk中引用外部變量會沒法生效,但使用雙引號時候,內置變量會不識別;解決方法是使用三引號;

localhost ~]$ more awkfile
this is one
this is two
this is three
this is four
this is five
this is six
this is seven

bash

$ awk '{print $3}' awkfile                             $ awk "{print $3}" awkfile   
one                                                            this is one                  
two                                                            this is two                  
three                                                          this is three                
four                                                            this is four                 
five                                                             this is five                 
six                                                               this is six                  
seven                                                          this is seven
ide


[@localhost ~]$ more awk.sh
#!/bin/bash
echo "-----1111111111------------"
awk 'NR==2 {print $3}' awkfile
echo "-----222222222-------------"
awk "NR==2 {print $3}" awkfile
echo "-----333333333-------------"
i=2
awk 'NR==$i {print $3}' awkfile
echo "-----444444444-------------"
awk "NR==$i {print $3}" awkfile
echo "-----555555555-------------"
awk "NR==$i "'{print $3}'"" awkfile
[@localhost ~]$
[@localhost ~]$ sh awk.sh
-----1111111111------------
two
-----222222222-------------
this is two
-----333333333-------------
-----444444444-------------
this is two
-----555555555-------------
two
[@localhost ~]$this


以上是我的淺薄經驗,以此進行記錄。
three

相關文章
相關標籤/搜索