0

Hello: I have a lot of files called test-MR3000-1.txt to test-MR4000-1.nt, where the number in the name changes by 100 (i.e. I have 11 files),

$ ls test-MR*
test-MR3000-1.nt  test-MR3300-1.nt  test-MR3600-1.nt  test-MR3900-1.nt
test-MR3100-1.nt  test-MR3400-1.nt  test-MR3700-1.nt  test-MR4000-1.nt
test-MR3200-1.nt  test-MR3500-1.nt  test-MR3800-1.nt

and also a file called resonancia.kumac which in a couple on lines contains the string XXXX.

$ head resonancia.kumac
close 0
hist/delete 0
vect/delete *
h/file 1 test-MRXXXX-1.nt
sigma MR=XXXX

I want to execute a bash file which substitutes the strig XXXX in a file by a set of numbers obtained from the command ls *MR* | cut -b 8-11.

I found a post in which there are some suggestions. I try my own code

for i in `ls *MR* | cut -b 8-11`; do
    sed -e "s/XXXX/$i/" resonancia.kumac >> proof.kumac
done

however, in the substitution the numbers are surrounded by sigle qoutes (e.g. '3000').

Q: What should I do to avoid the single quote in the set of numbers? Thank you.

Community
  • 1
  • 1
Dox
  • 187
  • 1
  • 8
  • 1
    Don't ever use ls this way. See http://mywiki.wooledge.org/ParsingLs – Charles Duffy Jun 18 '13 at 16:28
  • ...also, see http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29 – Charles Duffy Jun 18 '13 at 16:29
  • @CharlesDuffy: Thank you for the advise! I didn't know – Dox Jun 18 '13 at 16:30
  • 1
    ...also, you haven't described your problem in a way that lets anyone else reproduce it, so I don't see how anyone could help you with it. Someone needs to know the filenames you're using, the actual contents of the input files, the actual output you're getting, etc. If you can provide a standalone script that creates the necessary files and inputs and then reproduces the problem with those inputs it creates -- thus, something that reproduces the problem starting from nothing -- that would be helpful. – Charles Duffy Jun 18 '13 at 16:30
  • As it stands, the script is wasting effort because it edits the original file over the new file with the same pattern XXXX being replaced by the MR number. So, the net result of the loop is that the last substitution is the only effective substitution. – Jonathan Leffler Jun 18 '13 at 16:35
  • @JonathanLeffler In fact, the `bash` substitute all the numbers, but it writes `MR='3000'` instead of `MR=3000`. – Dox Jun 18 '13 at 16:39
  • @Dox So you want to make 11 copies of `resonance.kumac`? One for each MR type file? – jaypal singh Jun 18 '13 at 16:42
  • @JonathanLeffler: Yes, but each copy have a different value of `XXXX`, and they are saved into a single file `proof.kumac` – Dox Jun 18 '13 at 16:45
  • 1
    The code you have provided would not insert single quotes, and it works fine for me. Please copy-paste this code and try it again, without any ad hoc changes from that post you mentioned. – that other guy Jun 18 '13 at 17:00
  • ...by the way -- the reproducer I gave at the beginning of my answer is what you _should_ have included in your question. – Charles Duffy Jun 18 '13 at 17:04
  • So the `proof.kumac` will just be the lines `h/file 1 test-MRXXXX-1.nt` and `sigma MR=XXXX` repeated for different numbers? You should post what `proof.kumac` should look like in the end. – doubleDown Jun 18 '13 at 17:25
  • @thatotherguy don't know why, but you where right! I try the code once again and worked like a charm! Thank you! – Dox Jun 18 '13 at 19:10
  • 1
    @Dox While the original code didn't have the bug you asked here about, it _did_ have other bugs. Do consider taking advantage of the improvements offered here. – Charles Duffy Jun 19 '13 at 11:55

2 Answers2

3

This is a reproducer for the environment described:

for ((i=3000; i<=4000; i+=100)); do
  touch test-MR${i}-1.nt
done
cat >resonancia.kumac <<'EOF'
close 0
hist/delete 0
vect/delete *
h/file 1 test-MRXXXX-1.nt
sigma MR=XXXX
EOF

This is a script which will run inside that environment:

content="$(<resonancia.kumac)"
for f in *MR*; do
    substring=${f:7:3}
    echo "${content//XXXX/$substring}"
done >proof.kumac

...and the output looks like so:

close 0
hist/delete 0
vect/delete *
h/file 1 test-MR300-1.nt
sigma MR=300

There are no quotes anywhere in this output; the problem described is not reproduced.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1

or if it could be perl:

#!/usr/bin/perl
@ls = glob('*MR*');

open (FILE, 'resonancia.kumac') || die("not good\n");

@cont = <FILE>;

$f = shift(@ls);
$f =~ /test-MR([0-9]*)-1\.nt/;
$nr = $1;

@out = ();
foreach $l (@cont){
    if($l =~ s/XXXX/$nr/){
        $f = shift(@ls);
        $f =~ /test-MR([0-9]*)-1\.nt/;
        $nr = $1;
    }
    push @out, $l;
}

close FILE;
open FILE, '>resonancia.kumac' || die("not good\n");
print FILE @out;

That would replace the first XXXX with the first filename, what seemed to be the question before change.

flaschenpost
  • 2,205
  • 1
  • 14
  • 29