1

次のようなテキストファイルがあります。

[1] Header information Timestamp Text begins
Text continues
Text continues
Text continues

[2] Header information Timestamp Text begins
Text continues
Text continues

[3] Header information Timestamp Text begins
Text continues
Text continues
Text continues

そのファイルを読み込んで、各エントリ(1〜20行または30行)を個別に処理したいと思います。

空白行を区切り文字として使用してファイルをループする方法はありますか?

4

2 に答える 2

4

答えはイエスです!

awk -vRS="" '...'空白行を行スプリッターとして扱います。

例を挙げてテストします。

kent$  cat b.txt
[1] Header information Timestamp Text begins
Text continues
Text continues
Text continues

[2] Header information Timestamp Text begins
Text continues
Text continues

[3] Header information Timestamp Text begins
Text continues
Text continues
Text continues

kent$  awk -vRS="" '{print "==== new block here ===="}1' b.txt  
==== new block here ====
[1] Header information Timestamp Text begins
Text continues
Text continues
Text continues
==== new block here ====
[2] Header information Timestamp Text begins
Text continues
Text continues
==== new block here ====
[3] Header information Timestamp Text begins
Text continues
Text continues
Text continues
于 2013-02-06T17:51:22.013 に答える
0

これが純粋なbash解決策です:

{
    read -r block    # First line only
    while read -r; do
        if [[ $REPLY =~ ^$ ]]; then
            # Do what you like with the contents 
            # $block here, then ...
            block=""
        else
            block+="$REPLY"
        fi
    done
 } < file.txt
于 2013-02-06T18:24:35.347 に答える