2016-06-13 7 views

答えて

2
head -n<SkipLines> <filename> | tail -n<TakeLines> 

あなたはファイルpippo.txtの10日から2本のラインを取りたい場合:

head -n10 pippo.txt | tail -n2 

EDIT:

だけで行う別のファイルにそれを追加する:

head -n<SkipLines> <filename> | tail -n<TakeLines> >> <OtherFile> 

head -n10 pippo.txt | tail -n2 >> pippo2.txt 
1

Bash≧4と仮定します。

# data 

input=inputfile 
output=outputfile 
linenb=42 

# get line number 
mapfile -t -s $((linenb-1)) -n 1 line < "$input" || exit 1 

# check that we got a line 
if ((${#line[@]}==0)); then 
    printf >&2 'Line %d not found in file %s\n' "$linenb" "$input" 
    exit 1 
fi 

# append it to output file 
printf '%s\n' "$line" >> "$output" 

ピュアバッシュ:

はファイルinputfileからライン42を抽出し、それをファイルに追加するにはoutputfileと同じくらい簡単です!

+0

ファイルからN行だけを使用するために、ファイル全体をメモリ内の配列にロードするオーバーヘッドのような音がします。 – andlrc

+0

@andlrc:ファイル全体を読み込むわけではありません。最初の行( '-s'オプションの機能)と_stops(' -n 1'オプションの機能)です。それは非常に効率的です! –

+0

わかりました:-)私は 'mapfile'を読んでいます – andlrc

関連する問題