AWK的数组
awk的數(shù)組很神奇,我的感覺。
awk的數(shù)組可以使用數(shù)字當(dāng)索引,可以使用字符串來當(dāng)索引
1、使用數(shù)字做下標(biāo)
info[1]="one"
info[2]="two"
info[3]="three"
[xiaomo@ArchLinux ~]$ echo 'one two three' | awk '{info[1]=$1;info[2]=$2;info[3]=$3;print info[1],info[2],info[3]}'
one two three
[xiaomo@ArchLinux ~]$?
2、使用字符串(下標(biāo)不加引號(hào))做下標(biāo)
info[one]="awk"
info[two]="sed"
info[three]="grep"
[xiaomo@ArchLinux ~]$ echo "awk sed grep" | awk '{info[one]=$1;info[two]=$2;info[three]=$3;print info[one],info[two],info[three]}'
grep grep grep
[xiaomo@ArchLinux ~]$?
看到它們之間的不同了嗎?如果使用字符串(下標(biāo)不加引號(hào))做下標(biāo),只會(huì)保留最后一個(gè)下標(biāo)的值。
3、使用字符串(下標(biāo)加引號(hào))做下標(biāo)
info["one"]="awk"
info["two"]="sed"
info["three"]="grep"
[xiaomo@ArchLinux ~]$ echo "awk sed grep" |awk '{info["one"]=$1;info["two"]=$2;info["three"]=$3;print info["one"],info["two"],info["three"]}'
awk sed grep
[xiaomo@ArchLinux ~]$?
這次又不同了, 如果使用字符串(加上引號(hào))做下標(biāo),結(jié)果都是單獨(dú)保存下來了。
那么來看看下面的吧。
[xiaomo@ArchLinux ~]$ echo "awk sed grep ls" \
awk '{info["one"]=$1;info["two"]=$2;info["three"]=$3;info["three"]=$4;print info["one"],info["two"],info["three"],info["three"]}'
awk sed ls ls
[xiaomo@ArchLinux ~]$?
你是否已經(jīng)暈了呢?
別著急,其實(shí)很簡單而已。
使用字符串(不加引號(hào))當(dāng)下標(biāo),awk會(huì)把他們識(shí)別為相同的下標(biāo),所以只會(huì)保留最后一個(gè)下標(biāo)的值。
使用字符串(加引號(hào))當(dāng)下標(biāo)則相反,不會(huì)識(shí)別為相同的下標(biāo)。但是呢,如果字符串(加引號(hào))當(dāng)下標(biāo),遇到相同的下標(biāo)一樣會(huì)只保留最后一個(gè)下標(biāo)的值。
你是否看懂了?
不懂必須來找我。
轉(zhuǎn)載于:https://blog.51cto.com/xiaofengmo/1750093
總結(jié)
- 上一篇: HBase 6、用Phoenix Jav
- 下一篇: Error:(108) No resou