小さい一致するヘッダーを使用して大きなファイルからテキストを取得します

小さい一致するヘッダーを使用して大きなファイルからテキストを取得します

最初のヘッダーがソース ファイルと一致する場合、検索するヘッダーを検索するために、2 つのヘッダー間のテキストを抽出する必要があります。例:

&Header1

1231241241313124123213123214124123213213124124123123212

1231231231231231231231231231232131242141241241231325552

2132141241232132132132141251232132142142132132132142412

&Header2

1231241241313124123213123214124123213213124124123123212

2132141241232132132132141251232132142142132132132142412

&Header3

1231241241313124123213123214124123213213124124123123212

1231231231231231231231231231232131242141241241231325552

213214124123213213213214125123213214

そして私のソースファイル:

&Header1

&Header3

したがって、以下の番号情報を使用してヘッダー 1 と 3 のみを取得します。

答え1

startheader=$(head -1 sourcefile)
endheader=$(tail -1 sourcefile)

# above lines assume your sourcefile has two lines in it and 
# each line contains the starting header and ending header

startlinenumber=$(grep -n "${startheader}" datafile|cut -d: -f1)
endlinenumber=$(grep -n "${endheader}" datafile|cut -d: -f1)

sed -n -e "${startlinenumber},${endlinenumber}p" datafile

awkまたは、あるいはperl1 行のコマンドを使用してこれを行うより複雑な方法があることは確かですsedが、ここではロジックを明示的に示したいだけです。これを試して、ニーズに合わせて調整してください。

関連情報