次のように構成されたタブ区切りのテキストファイルがあります。
word0 word1 word2 word3 word4 word5 word6
Linuxの共通行から私はしたい:
- word6のみを取得
- word6の文字ord6のみを取得するにはどうすればよいですか?
AWKはそれを行うことができます:
$ echo "word0 word1 word2 word3 word4 word5 word6" | awk '{ print $(NF) }'
word6
これを試して
awk -F "\t" '{print $NF}' Input.txt
必要に応じて区切り文字を変更してください。
次のような簡単なcut
コマンドを使用できます。
$cut -d'Press CTRL+v then TAB key to insert tab as delim here' -f6 textfile|cut -c2-
ord6
$
詳細については、cutコマンドのマニュアルページを確認してください。
もちろん、同じ仕事をする方法は他にもたくさんあります。
string=$(echo "word0 word1 word2 word3 word4 word5 word6" | awk '{ print $(NF) }')
echo ${string:3:4} # echo substring starting at postion3, 4 characters long i.e. ord6
また
echo ${string[@]#w} # removes the shortest possible match for the regex
# (i.e. w) at the start of the string
##最長一致の場合、%&%%は文字列の後ろを削除します