-1

I have a file having row

file1 -int sch1.inp -HOST all.q:1 -NJOBS 1 -TMPLAUNCHDIR

I want to repeat this row 100 times with this change (sch2, sch3, sch4 etc)

file1 -int sch2.inp -HOST all.q:1 -NJOBS 1 -TMPLAUNCHDIR
file1 -int sch3.inp -HOST all.q:1 -NJOBS 1 -TMPLAUNCHDIR
file1 -int sch4.inp -HOST all.q:1 -NJOBS 1 -TMPLAUNCHDIR

how can i do this.

Thank you so much in advance. Rehan

ray
  • 3
  • 2
  • 3
    What have you tried? You'll need to give an effort first. – Nasir Riley Oct 29 '21 at 21:16
  • Vaguely related: https://unix.stackexchange.com/questions/672996/how-can-i-see-if-a-consecutive-number-name-file-is-missing-some-file/ – Jim L. Oct 29 '21 at 21:55
  • Are the sch*.inp things files that exist in the current directory? If so, would you want to run the command for each such file? – Kusalananda Mar 14 '22 at 08:24

2 Answers2

1
printf 'file1 -int sch%s.inp -HOST all.q:1 -NJOBS 1 -TMPLAUNCHDIR\n' {1..100}

Or:

seq -f 'file1 -int sch%g.inp -HOST all.q:1 -NJOBS 1 -TMPLAUNCHDIR' 100

Or:

jot -w 'file1 -int sch%d.inp -HOST all.q:1 -NJOBS 1 -TMPLAUNCHDIR' 100

Would produce the whole output.

If you wanted to start with that:

file1 -int sch1.inp -HOST all.q:1 -NJOBS 1 -TMPLAUNCHDIR

line in vim, and reproduce it with the second number of the line increased 99 times, you could do qayypw^Aq98@a with the cursor positioned on that line.

Where:

  • qa: starts recording the a macro
  • yy: yanks (copies) the whole line.
  • p: pastes it underneath
  • w: moves to the next word (to skip the first which does also contain a number).
  • ^A (Ctrl+A): increments the number under the cursor or if no number under the cursor, the next one found to the right of it.
  • q: finish recording the macro
  • 98@a: run the a macro 98 times.

Or whenever file1 -int <something><number>.inp is found at the start of a line, reproduce the line 100 times with the number increased:

perl -pe 'if (m{^file1 -int \S*?\K\d+(?=\.inp)}) {
            for my $i ($& .. $& + 99) {
              print;
              s//$i/;
            }
          }' < your-file
-2

This answer is based on the current question:

#!/bin/bash

string=$(cat infile) echo "$string" > outfile string=${string/sch1/sch%d}

for ((i=2; i<101; i++)) do printf "$string\n" "$i" >> outfile done

uncomment to overwrite input file

mv outfile infile

The following answer is based on the OP's original question - not the current question. The OP accepted this answer as correct.

#!/bin/bash

string=$(cat infile) echo "$string" > outfile

for ((i=2; i<101; i++)) do echo "${string/sch1/sch"$i"}" >> outfile done

uncomment to overwrite input file

mv outfile infile

fpmurphy
  • 4,636