How to print the required paragraph from a text file in unix shell -
i need print required paragraph text file having n number of paragraphs.
my script giving required output, still know more efficient , simpler code accomplish this,
awk 'match($0,/ /){print nr;}' para.txt > temp para=$1 para_start=`sed -n ${para}p temp`; next_para=`expr ${para} + 1`; next_para_start=`sed -n ${next_para}p temp`; para_end=`expr ${next_para_start} - 1`; sed -n ${para_start},${para_end}p para.txt paragraphs have tab space @ start of each paragraph. above script should run "para.sh 3".
all of logic can refactored single awk script.
awk -v p="$1" 'begin { r=1 } /^\t / { ++i } i>p { exit r } i==p { print; r=0 }' para.txt we increment i when see separator. if reached past desired paragraph, done (the exit code indicates whether in fact printed). if desired paragraph, print current line.
shell unix
No comments:
Post a Comment