ダッシュで区切られたトラック タイトルとアーティストを含むプレーン テキスト ファイルがあります。すべてのタイトルを 1 つの配列 ( ) に入れたいTRK_TITLE[]
。同時に、アーティストの配列を作成します ( ARTIST[]
)。以下は私が使用しているコードです:
CTR=0
# Read in the track title file
while read line
do
# Add to the counter
CTR=$((CTR + 1))
# Get the track number
TRK_NUM[$CTR]=$CTR
# VARIOUS is set by command line parameter
if [ $VARIOUS = "FALSE" ]
then
# -- THIS BIT WORKS! ------------------------------
TRK_TITLE[$CTR]=${line}
# ARTISTS determined by grandparent directory name.
ARTIST[$CTR]="$ARTISTS"
# THE BIT THAT DOESN'T WORK AS IT APPEARS ---------
else
# VARIOUS has been set to TRUE
# Get the track title
# 1st, Make sure I'm dealing with something sensible
echo "$line"
# Get the length of the line,
# just for information
total_len=${#line}
# Find the position of the "-"
dash_pos=`expr index "$line" -`
# These lines prove that the syntax works
echo "${line:0:$dash_pos - 2}"
echo "${line:$dash_pos + 1}"
echo $total_len "--" $dash_pos
# Now add to arrays
TRK_TITLE[$CTR]="${line:0:$dash_pos -2}"
#TRK_TITLE="${line:0:$dash_pos -2}"
ARTIST[$CTR]="${line:$dash_pos + 1}"
#ARTIST="${line:$dash_pos + 1}"
#Now to see the output
echo $TRK_TITLE[$CTR] "is Track"
#echo "$TRK_TITLE is Track"
echo $ARTIST[$CTR] "is Artist"
#echo "$ARTIST is Artist"
fi
# keep going until the end
# Variable name used for input file
done < "$FYL_2_USE"
ハッシュがある場所にある場合、出力は次のようになります。
愛する人に捧ぐ - ママスとパパス
愛する人に捧げる
ママスとパパス
53 -- 29
[19] はトラック
[19] はアーティスト
variable ステートメントと echo ステートメントでハッシュが交換された場合、出力は次のようになります。
愛する人に捧ぐ - ママスとパパス
愛する人に捧げる
ママスとパパス
53 -- 29
One I Love is Track に捧げる
ママス&パパスはアーティスト
シェルは Gnu Bash V4.1.0(2)