パターンマッチングでファイルを特定の出力ファイル名に分割する

パターンマッチングでファイルを特定の出力ファイル名に分割する

次の内容を含むファイルがあります。

# new file
text in file 1
# new file
text in file 2
# new file
text in file 3

ここでのパターンはです# new file

各ファイルをxx00、xx01、xx02に保存する代わりに、特定のファイル、、、にanother file保存file newしましたlast one

この3つのファイルが現在のディレクトリに存在するので、それを配列として提供して上書きしたいと思います。

csplit -z infile '/# new file/' "${array[*]}"

配列を直接提供できます

array=('another file' 'file new' 'last one')
echo ${array[*]}
another file file new last one

または、現在のディレクトリを一覧表示します。

array=($(find . -type f))
echo ${array[*]}
./another file ./file new ./last one

修正このスクリプト考えられる解決策:

awk -v file="1" -v occur="2" '
{
  print > (file".txt")
}
/^\$\$\$\$$/{
  count++
  if(count%occur==0){
    if(file){
      close(file".txt")
      ++file
    }
  }
}
'  Input_file

ベストアンサー1

私はまだを使用することを検討し、生成されたcsplitファイルの名前を変更します。

#!/bin/sh
mkdir ".tmp.$$" || exit 2
csplit -f ".tmp.$$/tmp_" -zk -n 4 "$1" '/# new file/' '{*}'

for file in ".tmp.$$"/tmp_*
do
    shift
    mv -f "$file" "$1"
done
if ! rmdir ".tmp.$$" 2>/dev/null
then
    echo "Warning: not all file parts were assigned" >&2
    rm -rf ".tmp.$$"
    exit 1
fi
exit 0

使用法

mysplit <source_file> <target_names...>

おすすめ記事