Bash'de, bir değişken tarafından tutulan dizenin N. kelimesini almak istiyorum.
Örneğin:
STRING="one two three four"
N=3
Sonuç:
"three"
Bunu hangi Bash komutu / betiği yapabilir?
Bash'de, bir değişken tarafından tutulan dizenin N. kelimesini almak istiyorum.
Örneğin:
STRING="one two three four"
N=3
Sonuç:
"three"
Bunu hangi Bash komutu / betiği yapabilir?
Yanıtlar:
echo $STRING | cut -d " " -f $N
Bir alternatif
N=3
STRING="one two three four"
arr=($STRING)
echo ${arr[N-1]}
IFS(dahili alan ayırıcı) ':' veya beyaz boşluk yerine başka bir şeye ayarladıysanız , bunu denemeden önce tekrar değiştirin.
Bazı ifadeler içeren bir dosya:
cat test.txt
Sonuç:
This is the 1st Statement
This is the 2nd Statement
This is the 3rd Statement
This is the 4th Statement
This is the 5th Statement
Dolayısıyla, bu ifadenin 4. kelimesini yazdırmak için:
cat test.txt |awk '{print $4}'
Çıktı :
1st
2nd
3rd
4th
5th
STRING=(one two three four)
echo "${STRING[n]}"