开发者

Have echo and grep replace each time for-loop changes n

my text file "testnum" looks like this:

xxx1abc
xxx2abc
xxx3abc
xxx4abc
xxx5abc
xxx6abc
xxx7abc
xxx8abc
xxx9abc
xxx10abc..etc

this gives my desired output in cmdline

for n in {1..10}
do
o=`sed -n "${n}p" < testnum`
clear
echo -e "you read ${o}\r"
grep  -n -C 2 ${o} testnum 
sleep 2s
done

and that looks like this which开发者_JS百科 is what i want

you read xxx10abc
8-xxx8abc
9-xxx9abc
10:xxx10abc
11-xxx11abc
12-xxx12abc

just that it clears the cmd line everytime it iterates what i want is this

nikola@nikola-desktop:~/Downloads$bash my_skirpt
you read xxx1abc
1:xxx1abc
2-xxx2abc
3-xxx3abc

last iteration should be

nikola@nikola-desktop:~/Downloads$bash my_skirpt
    you read xxx10abc
    8-xxx8abc
    9-xxx9abc
    10:xxx10abc
    11-xxx11abc
    12-xxx12abc
nikola@nikola-desktop:~/Downloads$


I'm not entirely certain, but I think you want it to overwrite all the lines it printed the last iteration? In that case, you want to use tput:

#!/bin/bash
tput sc
for n in {1..10}
do
o=`sed -n "${n}p" < testnum.txt`
echo -e "you read ${o}\r"
grep  -n -C 2 ${o} testnum.txt
sleep 2s
tput rc
tput el1
done;
tput sc

tput sc saves the current cursor position, so we can later recall this position with tput rc. tput el1 clears the line we're on so that the changing number of lines (first and last outputs are shorter than the other ones) doesn't destroy anything.

There is a lot more you can do with tput, google will for instance give you this page, which I find quite good.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜