Read and edit multiple files using a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read and edit multiple files using a while loop
# 1  
Old 08-27-2010
Read and edit multiple files using a while loop

Hi all,
I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands:

Code:
:1,$s/search_str/replace_str/g<return>


but I am not sure how to automate the <return> of these vis commands when I am putting this in a while loop.

i.e. if a loop the first time I cannot actually initiate the above command without pressing the return button, is there a special command for this?

Thanks

Last edited by Scott; 08-27-2010 at 07:29 AM.. Reason: Code tags
# 2  
Old 08-27-2010
The syntax you use can be also used in sed. If your sed has the -i option (perl has an option like this too), you can just edit the files directly. Else you might want to write the output of sed to a tmp file and mv it back to the original file.
If you can print the files with a find and hand them over to sed -i with -exec or xargs for example.
If you feed the filenames from a file, then a while loop may be a good idea.
# 3  
Old 08-27-2010
Sorry not too familiar with SED commands so breaking this down so I have tried this comamnd:

Code:
sed -e '1,$s/old string/new string/g' myfirstfileinloop.txt

now when I execute this it prints to the terminal including the new strings that were replaced yet when I open the actual file it still remains the old same and nothing has changed?

Last edited by Scott; 08-27-2010 at 07:29 AM.. Reason: Code tags
# 4  
Old 08-27-2010
Ok!

We have two options:

1. As zaxxon commented, if your sed supports the "-i" option, in place change, use it.
Code:
sed -i -e '1,$s/old string/new string/g' myfirstfileinloop.txt

2. If it does not, you must redirect the output, like the following:
Code:
ls -1 | while read fname
do
      myFileTmp=${fname}.tmp
      sed -e '1,$s/old string/new string/g' "${fname}" > "${myFileTmp}"
      # If you want to remove the original file uncomment the line below
      [ $? -eq 0 ] && rm -f "${fname}"
done

# 5  
Old 08-27-2010
The second option will change all files in a directory, you want one file, just:
Code:
sed -e '1,$s/old string/new string/g' myfirstfileinloop.txt > myfirstfileinloop.txt.new

Also, in the second option you can rename the tmp file to the original one:
Code:
ls -1 | while read fname
do
      myFileTmp=${fname}.tmp
      sed -e '1,$s/old string/new string/g' "${fname}" > "${myFileTmp}"
      # If you want to change back to the original file uncomment the line below
      # [ $? -eq 0 ] && mv "${myFileTmp}" "${fname}"
done

# 6  
Old 08-27-2010
It supports sed but it seems to throw this illegal message:

Code:
sed: illegal option -- i
Usage: sed [-n] [-e script] [-f source_file] [file...]


Last edited by Scott; 08-27-2010 at 07:32 AM.. Reason: Code tags, please...
# 7  
Old 08-27-2010
It is because sed does not support the: "-i" option. Try the second option.

Also, in what platform are you executing it?

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to edit multiple files

Created a shell script to invoke sed to edit multiple files, but am missing something. Here's the shell script: oracle:$ cat edit_scripts.sh #!/bin/sh #------------------------------------------------------------------------------ # edit_scripts.sh # # This script executes sed to make global... (4 Replies)
Discussion started by: edstevens
4 Replies

2. Programming

Read .xlsx and text files in a loop using openpyxl

I have a list of .xlsx files (names 1.xlsx, 2.xlsx etc) in a directory, on which I need to insert data from its corresponding text file (named 1.txt, 2.txt etc) on the second worksheet named 'Filtered' and save it. The code I am trying is #!/usr/bin/python import os from... (8 Replies)
Discussion started by: nans
8 Replies

3. Shell Programming and Scripting

Help require to edit multiple files

I have 6 different pipe delimiter files. My loads failing due to missing company code. File1: 31 st field is company code. 402660076310|2014-12-10 17:22:39|2280361|MRYKI|1||CA|92507|US||1|1|0|0|0||N|A1|ONT|1001891771660009250700402660076310|WM|0201|RALA |2014-12-12|5|2014-12-12|5||FRI - 12... (4 Replies)
Discussion started by: srikanth38
4 Replies

4. Shell Programming and Scripting

Read loop from two files using descriptors

What I would like to do is read each line in the atdinfile: A sample atdinfile would look like this: 651 652 653 654 655 656 657 658 659 660 661 664 665 666 667 668 (5 Replies)
Discussion started by: woodson2
5 Replies

5. Shell Programming and Scripting

[SOLVED] UNIX FOR loop to read a variable with multiple values

Hi, I have a variable which stores file names as a result of find command. I need to delete all these files one by one, i.e. by a loop. Can anyone tell me how can it be done? The variable f2d has the file names like these abc.txt bcd.txt fff.txt gef.txt Now I have used a loop as... (12 Replies)
Discussion started by: jhilmil
12 Replies

6. Shell Programming and Scripting

loop to read multiple username and password

Hello all, I am i am trying to read username password. Bassicaly, i have file called sidlist and it has my database name, username and password.... looks something like this.... db1, user1, pass1 db2, user2, pass2 db3, user3, pass4 but i dont know how to make it work, until i get... (4 Replies)
Discussion started by: abdul.irfan2
4 Replies

7. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

8. Shell Programming and Scripting

Read multiple arguments in for loop each time

Hi, Guys I am new to shell programming and just get stuck with one simple question. please kindly help. According to the tutorial here, we can do something like for NODE in "ABC 10" "EFG 20" do set -- $NODE echo "letter is $1, number is $2" done And the result will... (3 Replies)
Discussion started by: yuanli
3 Replies

9. Shell Programming and Scripting

help with a 'while read' loop to change the names of files

Hi, I am new to scripting, so any help on this would be much appreciated. I am trying to rename a bunch of files, taking the names sequentially from a list read in another file... # ls oldnames file_1 file_2 file_3 # cat names red yellow green I want the files to take on the... (6 Replies)
Discussion started by: starsky
6 Replies

10. UNIX for Dummies Questions & Answers

Edit Multiple Files in VI

Here's what I have... $ vi foo1 - open foo1 and work around for a while. I yank a few lines into a buffer and then :w to save. Next I :e foo2 to open foo2 and paste my buffer. I :w to save, but I would like to then be able to go directly back into foo1 where I was before I opened foo2. ... (4 Replies)
Discussion started by: djschmitt
4 Replies
Login or Register to Ask a Question