Problem with sed in for loop, would appreciate some help


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Problem with sed in for loop, would appreciate some help
# 1  
Problem with sed in for loop, would appreciate some help

Dear expert users,

I am having trouble with this for loop, where I am editing a design file, replacing the subject, session and another parameter in the template design file.

When implementing the for loop, it did not do what I wanted it to, for example editing to {2014}/CAT_Run0[5] instead of my hoped-for 2014/CAT_Run05.

When I tried to run it line by line, replacing the first parameter (using $subj) worked fine (2013 -> 2014), but I got an error on the second (using $run), to me mysterious:
sed: -i may not be used with stdin

Code:
for subj in `cat subjlist.txt`
cd $subj
for run in `cat stimruns.txt`; do
		cd CAT_Run0$run.ica
		block=`cat block.txt`
		cp /Users/template.fsf .	
		sed -i '' "s|2013|$subj|g" template.fsf
		sed -i '' "s|CAT_Run01.ica|CAT_Run0{$run}.ica|g" template.fsf
		sed -i '' "s|Block_5|Block_{$block}|g" template.fsf
done
cd ..
done

Any clues here would be highly appreciated.

Last edited by rbatte1; 12-05-2019 at 10:31 AM.. Reason: Added ICODE tags
# 2  
1/
The first thing I observed is that you're doing
Code:
for var in `cat filename`
do
  ...
done

instead of
Code:
while read line
do
  ...
done < filename


2/
Next I see that in the inner loop you are copying /Users/template.fsf in each run of the loop. I think you don't intend this?


3/
Can you please give us sample of what the file contains, expected output, shell you're working on....
# 3  
1. Thank you so much, that is immediately helpful! Smilie Didn't know while loops in UNIX.
2. The copying was intended, since I need a changed version of the template.fsf in each folder (which is then run later in the script). This is the only way I know so far of doing this.
3. Yeah, the template.fsf is a text file generated by FSL specifying details of a fMRI analysis, and I'm hoping to edit it in the loop. The stimruns.txt file for each subject only contains a column vector of numbers I want to loop over within a folder of a subject, and the subjlist.txt a column vector of foldernames (subject folders) I want to loop over.

--- Post updated at 10:13 AM ---

Oh, it's bash shell Smilie
# 4  
On top of what balajesuri already said, I don't think {$run} will yield what you expect it to yield, but it also would not yield what you posted ( [5] ). I'd expect something like {5}, rather. Same for {$block}. And, if a {2014} is inserted, I'd guess it's taken as read from the input file, which would imply you need to remove the {} first before using the data. Sample input data would help...


Howsoever, guessing your data structures etc. from what I see in post #1, how far would (untested)
Code:
while read subj
  do    while read run
          do    TMP=CAT_Run0${run}.ica
                read block < ${subj}${TMP}block.txt
                sed "s|2013|${subj}|g; s|CAT_Run01.ica|${TMP}|g; s|Block_5|Block_${block}|g" /Users/template.fsf > ${subj}${TMP}template.fsf
          done < ${subj}/stimruns.txt
  done < subjlist.txt

get you?
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with loop within loop

Hi, I work on Ab-initio ETL tool which is based on Unix. I made a small script which has two loop's one with in another. All the functionality is working for the first line of outer loop but when it comes to other lines of outer loop it is throwing error as command not found. Below is the... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

2. Shell Programming and Scripting

For loop with sed command

Hi Expert, I am working on one script which needs to read the input from the file and replace the words using sed command. For each line in the input file It should create one file. Below is the code. I can see the for loop is working fine but instead of three files it is creating one file ... (3 Replies)
Discussion started by: sharsour
3 Replies

3. UNIX for Dummies Questions & Answers

Using sed in a for loop

Hi, I have a file called 1.txt, I want to create 2.txt through 100.txt using a for loop. In each instance of the loop, I want to change all "1"'s in the text file to "2"s and so on... I tried the code below to do this: for i in {2..100}; do sed 's/1/$i/g' 1.txt > $i.txt; done but it... (1 Reply)
Discussion started by: evelibertine
1 Replies

4. UNIX for Dummies Questions & Answers

Using sed in a for loop

Hello I have a group of files a1.profile a2.profile a3.profile a4.profile b1.profile b2.profile b3.profile b4.profile These files all have the same first line with a value s1 atop the columns s1_context s1_ref s1_sample s1_% etc I am trying to use sed in a for loop to replace the s1 in the... (2 Replies)
Discussion started by: plumb_r
2 Replies

5. Shell Programming and Scripting

If with sed loop question

Hi, $list_A contains a list of user ID's. $pwdfile_new is the unix password file. $useralias is a report file I'm trying to search through the password file with the list in list_A and dumping what ever name it matches into the useralias file. But I want to the code to put "Unable to... (2 Replies)
Discussion started by: Jazmania
2 Replies

6. Shell Programming and Scripting

Using sed in for loop

I have a file MAT.txt which contains the following data: mat1.txt mat2.txt . . . . mat100.txt I want to remove the '.txt' from every line and have an output file with the following data: mat1 mat2 . . . . mat100 I know this can be done with sed easily for each line, but I do... (8 Replies)
Discussion started by: ursaan
8 Replies

7. Shell Programming and Scripting

For loop with sed

Hi there, I have 1 file with different 144 lines and 144 files that I want to change with a sed. What I want to do is to go trough the file with the 144 different lines take the line a replace a certain pattern with said in the first file of the folder where the 144 files are. Take the second... (3 Replies)
Discussion started by: sickboy
3 Replies

8. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

9. UNIX for Dummies Questions & Answers

sed in a for loop

dear guys, I want to replace the beginning of some lines in a file by (,) The sequence number of lines is located in a file named numbers so, i used this simple script to do so, but it didn't work. it does'nt recognize the $j in the command sed #! /bin/sh for j in `cat numbers` do sed '$j... (6 Replies)
Discussion started by: marwan
6 Replies

10. Shell Programming and Scripting

Problem with for loop/sed ?

I have a hostnames file which has: $ cat hostnames.txt serverxx1 serverxx2 serverxx3 My script: #!/bin/sh fileA=build.xml for i in ./hostnames.txt ; do sed 's/createConfig machine="Machine"/createConfig machine=" '$i' "/g' "$fileA" > ./tmpfile done FileA has: createConfig... (2 Replies)
Discussion started by: chiru_h
2 Replies