Text-Processing
文件中的行範圍
我有一個包含幾行的文件:
C 1.31729690416596 -0.05166770188922 0.05026556004853 C 2.15676687192808 -0.87605403673920 0.93010807487406 C 1.45516592682388 -0.49804136618225 -1.24011085742027 C 2.67489108786199 -2.06116694592319 0.09667460087481 C 2.24626744432148 -1.81826783081380 -1.24626795859707 C 0.12278380551129 -0.78127661791244 -1.79035714840462 C 0.11627012096698 -2.05095769577445 -2.31049614254010 C 1.41908665901196 -2.77247309785857 -1.91624359298119
我需要在一次操作中列印一系列複雜的行,例如第 1 行,多行 4 到 7 行,這是 1,4-7。如何獲取這些行而不是搜尋每行。
ed
行編輯器:printf '%s\n' '1p' {4..7}p 'Q' | ed -s - file
awk
:printf 'NR==%d\n' 1 {4..7} | awk -f - file
sed
流編輯器:printf '%sp\n' 1 4,7 | sed -nf - file
perl
:perl -lne 'print if grep { $_ == $. } (1, 4..7)' file
使用
awk
varNR
:awk 'NR == 1 || NR >= 4 && NR <= 7' file C 1.31729690416596 -0.05166770188922 0.05026556004853 C 2.67489108786199 -2.06116694592319 0.09667460087481 C 2.24626744432148 -1.81826783081380 -1.24626795859707 C 0.12278380551129 -0.78127661791244 -1.79035714840462 C 0.11627012096698 -2.05095769577445 -2.31049614254010