3

文字列 (contents1) には以下が含まれます

755572 ZR66_op/Res7.fcp 755676 ZR66_op/Res-Edited-MP3-Files 755677 ZR66_op/Res-Files 756876 ZR66_op/Res-Edited-WAV-Files 758228 ZR67_op/Res5.fcp 758224 ZR66_opinal/Res-Audio-Files 75825 /Res-Edited-ファイル

以下のみ文字列にまとめたい(contents2)

755572 ZR66_op/Res7.fcp 755676 ZR66_op/Res-Edited-MP3-Files 755677 ZR66_op/Res-Files 756876 ZR66_op/Res-Edited-WAV-Files 758224 ZR66_op/Res-Original-Audio-Files

これZR66_opが検索要素になります

誰でもこれで私を助けることができますか

4

2 に答える 2

2

パターン マッチングを使用できます。

#! /bin/bash
search=ZR66_op

contents1=755572\ ZR66_op/Res7.fcp\ \
755676\ ZR66_op/Res-Edited-MP3-Files\ \
755677\ ZR66_op/Res-Files\ \
756876\ ZR66_op/Res-Edited-WAV-Files\ \
758228\ ZR67_op/Res5.fcp\ \
758224\ ZR66_op/Res-Original-Audio-Files\ \
758225\ ZR67_op/Res-Edited-Files

ar=($contents1)

for (( i=0; i/2<=${#ar}; i+=2 )) ; do
    if [[ ${ar[i+1]} == "$search"* ]] ; then
        contents2+="${ar[i]} ${ar[i+1]} "
    fi
done

contents2=${contents2% } # Remove the extra space
echo "$contents2"
于 2013-01-06T10:29:20.530 に答える
0

空白で区切られた文字列を操作していて、固定サイズのセット (ペア、トリプルなど) で文字列からトークンを引き出す場合、「読み取り」を使用してトークンを変数にロードできます。

#! /bin/bash
contents1='755572 ZR66_op/Res7.fcp 755676 ZR66_op/Res-Edited-MP3-Files 755677 ZR66_op/Res-Files 756876 ZR66_op/Res-Edited-WAV-Files 758228 ZR67_op/Res5.fcp 758224 ZR66_op/Res-Original-Audio-Files 758225 ZR67_op/Res-Edited-Files'

search=ZR66_op

contents2=""
while read number filename
do
    if [[ $filename == "$search"* ]]
    then
        contents2="$contents2 $number $filename "
    fi
done <<< $contents1

echo $contents2
于 2013-01-06T17:01:20.167 に答える