I'm trying to run:

cat s_f_d_TMT161023_1306.fits.coo.1 | sed -e '/^#/d'| sort -n -k 3 | head -n 50 >> inputcoo2.list


cat s_f_d_TMT161023_1307.fits.coo.1 | sed -e '/^#/d'| sort -n -k 3 | head -n 50 >> inputcoo3.list

and so on.

The last four characters on which I'm using the cat varies from 1305 and goes till 1440. I also want the name of the output be increased by one like inputcoo3.list and inputcoo4.list and so on.

I can guess that I must run some kind of loop, but I'm new to bash and don't have any idea on how to implement it.

How do I go about it?



for i in $(seq 1305 1440)
  j=$((i - 1304))
  sed -e '/^#/d' "s_f_d_TMT161023_${i}.fits.coo.1" | 
    sort -n -k 3 | 
    head -n 50 >> "inputcoo${j}.list"
  • this save a call to cat
  • note that destination file will be appended
  • if you want leading 0 (numerical zero, not character o) in filename, use j="$(printf '%03d' $i)"
  • 'seq' has a bash builtin of {$start..$end} - You could replace the first line with 'for i in {1305..1440}'
    – xrobau
    Jul 25 '17 at 14:09
  • 1
    @xrobau thanks, I didn't know it. seq didn't exists when I start using unix.
    – Archemar
    Jul 25 '17 at 14:11
  • No worries. Bash has so many useful builtins that people don't know exist 8)
    – xrobau
    Jul 25 '17 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.