我已经通过这些方式使用这些来帮助读取文本文件:Split string into an array in Bash和How do I split a string on a delimiter in Bash?并尝试拆分字符串,但我只获得第一行代码:(我已粘贴了我正在阅读的“文件”中的相同代码。)
#!/bin/bash
i=0
for file in $(ls -l)
do
if [ -f $file ] #checking for regular file
then
if [ $file == Q1 ] || [ $file == Q1~ ]
then
continue
else
if [ -f $file ]
then
echo $file
v=$(<$file)
# echo ${v[0]} # prints all
#------------------ split string-----------------------
OIFS="$IFS"
IFS='' # split by spaces
read -a array <<< "${v}"
IFS="$OIFS"
echo ${array[0]} #
#printf "\n"
# sed -i 's/$1/$2/g' "$file"
else
echo "Error: Not found"
fi
#i=$i+1
#echo $i
fi
fi
done
exit 0
但是当使用循环时,则没有分裂(第二代码)
#!/bin/bash
i=0
for file in $(ls -l)
do
if [ -f $file ] #checking for regular file
then
if [ $file == Q1 ] || [ $file == Q1~ ]
then
continue
else
if [ -f $file ]
then
echo $file
v=$(<$file)
# echo ${v[0]} # prints all
#------------------ split string-----------------------
OIFS="$IFS"
while IFS='' read -a array;
do
for i in "${array[@]}";do
echo ${array[1]} #
done
done <<< "$v"
#printf "\n"
# sed -i 's/$1/$2/g' "$file"
else
echo "Error: Not found"
fi
#i=$i+1
#echo $i
fi
fi
done
exit 0