1、Shell字符串分割(cut命令)
string="1,2,3"str1=echo $string | cut -d ',' -f 1 #将输入的字符串根据空格分割,取第一个字段(field)
str2=echo $string | cut -d ',' -f 2 #将输入的字符串根据空格分割,取第二个字段(field)
str3=echo $string | cut -d ',' -f 3 #将输入的字符串根据空格分割,取第三个字段(field)
echo $str1 #显示第一个字段
echo $str2 #显示第二个字段
echo $str3 #显示第三个字段
cut命令,-d选项指示使用的分隔符,-f选项指示取用的是第几个字段
2、Shell逐行读文件
方法一:while read line
do
echo $line #读取行后在此处理
done < filename
方法二:
cat $filename | while read line
do
echo $line #读取行后在此处理
done
例如
cat $file_in | while read line
do
echo "$line"
done
当然最好的逐行处理工具还是awk