shell編程:字符串string的相關處理*

注意:shell中的字符串拼接是不須要用+號的,直接在字符串後面繼續添加便可。如:${file}-`date +%s`.ok,最後出來的就是file名字+日期+.oklinux

1.批量去除.ok後綴的文件名,使用表達式${string%substring}shell

for file in `hdfs dfs -find $1 -name "*.ok"`
do
    echo `date`" move ok on file ${file}"
    hdfs dfs -mv ${file} ${file%.ok}
done測試

2.測試文件夾的大小,result包含幾個值,用空格分開,只有第一個是文件夾的大小。首先將空格用AAA代替,表達式:${result// /AAA}。而後取出第一個AAA的index,表達式:expr index ${result_rep} AAA。最後再截取字符串,表達式:expr substr ${result_rep} 1 ${index}spa

result=`hdfs dfs -du -s /user/hive/warehouse/splited_tmp_split`
result_rep=${result// /AAA}
index=$[`expr index ${result_rep} AAA` - 1]
size=`expr substr ${result_rep} 1 ${index}`blog

 

參考:linux shell 字符串操做詳解 (長度,讀取,替換,截取,鏈接,對比,刪除,位置 )ci

相關文章
相關標籤/搜索