「Bash」awkコマンドで文字列の長さを計算する

2021年12月3日

書式
変数名 =値
echo $文字列変数名 |awk '{print length}
awkコマンドで文字列の長さを計算します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
#Bash script to find the length of a string
target="Welcome to arkgame.com"
length=`echo $target |awk '{print length}'`
echo "文字列'$target'の長さ $length"
#!/bin/bash #Bash script to find the length of a string target="Welcome to arkgame.com" length=`echo $target |awk '{print length}'` echo "文字列'$target'の長さ $length"
#!/bin/bash  
#Bash script to find the length of a string  

target="Welcome to arkgame.com"  
length=`echo $target |awk '{print length}'`  

echo "文字列'$target'の長さ $length"

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# sh test07.sh
文字列'Welcome to arkgame.com'の長さ 22
# sh test07.sh 文字列'Welcome to arkgame.com'の長さ 22
# sh test07.sh
文字列'Welcome to arkgame.com'の長さ 22

 

batch

Posted by arkgame