Modify different files at the same time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify different files at the same time
# 1  
Old 09-21-2011
Modify different files at the same time

Hi, I'm having the following problem.
I have some files to modify, between a large number of files.
I thought the following code

PHP Code:
aux2=`grep -l 2.2.17 *`
print 
"$aux2"
aux3=`ls -l *.ksh | wc -l`
print 
"$aux3"

while [  $aux3 -ne 0 ]; do
        print 
"The counter is $aux3"
        
#Add the sed here
        
aux3=$((aux3-1))
done 
I also thought the basic code for the modify. I have to use a sed :P
But I don't know how to go between the different files in the while. Use the first file in the first loop, the second file in the following loop and so on.
The files are stored in the aux2 variable.

Could you give me any idea

Thanks
# 2  
Old 09-21-2011
What's your system? What's your shell?

You don't need -l if you're just counting the lines. ls always prints single-column if you're piping its output.

I'm confused what that count is for or why you get it in such a manner.

If you have linux, you can sed many files in-place with -i:

Code:
aux3=`ls *.ksh | wc -l`
# Only split on newlines.  Otherwise, spaces in filenames
# may mess up aux2.
IFS="
"
aux2=`grep -l 2.2.17 *`
whille [ "$aux3" -gt 0 ]
do
        sed -i "s/a/b/g" $aux2 # DO NOT QUOTE aux2.  It will split on newlines.
done

If you don't have sed -i:

Code:
aux3=`ls *.ksh | wc -l`
# Only split on newlines.  Otherwise, spaces in filenames
# may mess up aux2.
IFS="
"
aux2=`grep -l 2.2.17 *`
whille [ "$aux3" -gt 0 ]
do
        for FILE in $aux2
        do
                sed 's/a/b/g' < "$FILE" > /tmp/$$
                cat /tmp/$$ > "$FILE"
        done
done

If you explain exactly what you're trying to accomplish instead of the manner you wish to do it, there's probably much better ways than running sed n*m times for each file.
# 3  
Old 09-22-2011
Thanks for the quick answer.
I'm working on a RedHat.
I think I miss some explanation, inside aux2 you have all the filename of the files I need to change. And inside aux3 you have the number of files.
For example:
Inside aux2 you have:
log_cleanup.ksh
logs_gzip.ksh
logs_move.ksh
Inside aux3 you have: 3 (the number of file)

---------- Post updated 09-22-11 at 12:19 AM ---------- Previous update was 09-21-11 at 09:31 PM ----------

The script works perfect, but there is one thing. Instead of replace, I need to append to the end of each file
PHP Code:
aux3=`ls *.ksh | wc -l`
# Only split on newlines.  Otherwise, spaces in filenames
# may mess up aux2.
IFS="
"
aux2=`grep -l 2.2.17 *`

print 
"AUX3 --> $aux3"
print "AUX2 --> $aux2"

while [ "$aux3-gt 0 ];
do
        
sed -"s/2.2.17/2.2.21/g" $aux2 # DO NOT QUOTE aux2.  It will split on newlines.
        
aux3=$((aux3-1))
done 
I clear it a little to work with my script
# 4  
Old 09-22-2011
Quote:
I think I miss some explanation, inside aux2 you have all the filename of the files I need to change. And inside aux3 you have the number of files.
I think the loop's pointless, then, since you edit the same files the same way every single loop. That also makes it a useless use of backticks and a useless use of ls *.

You can loop over the files without backticks, ls, or arithmetic:

Code:
for FILE in *.ksh
do
        # Append the line 'something' to the file
        echo "something" >> "$FILE"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modify csv-files with awk

Hello everyone! I have thousands of csv files I have to import into a Database table. As usually the files aren't perfect. For example they have a different number of columns and some weird columns. The second problem is, that I have to add 3 parts of the filename into 3 rows in the... (6 Replies)
Discussion started by: elRonaldo
6 Replies

2. Shell Programming and Scripting

Find modify and delete files

hi every one. one of my friends has writen this script and send it to me. this script can find files that add-delete-modify and also send an alert by email i'm not catch all part of it. can anyone explain me how this work #!/bin/bash START="a.txt" END="b.txt" DIFF="c.txt" mv ${START}... (4 Replies)
Discussion started by: nimafire
4 Replies

3. Shell Programming and Scripting

Need to modify a lot of html files

Hello, I have about 3400 files in a tree structure (about 80% are html files). 1. I need to modify every html file to remove <p> style and old things like font attribute and add another style. 2. I need to change the root of all links that are in the html. e.g. change /old/path/ to /new/path... (1 Reply)
Discussion started by: Yaazkal
1 Replies

4. Shell Programming and Scripting

Help with the idea of a script to modify time

Hello friends, I need an idea or a ready solution for a problem i have the following lines in text file: 1 20100920140122 object4 MOVE IN 2 20100920150012 object4 MOVE OUT -- cut -- the second column is the date and time: 20100920140122 = 2010 09 20 14:01.22 what I need to do is to add 40... (8 Replies)
Discussion started by: peetteerr
8 Replies

5. Shell Programming and Scripting

List files and display last modify time in a particular format

hi everyone, can someone suggest how i can list the contents of a directory and display their corresponding last modify time in the format yyyymmddhhmm? thanks in advance! (16 Replies)
Discussion started by: Deanne
16 Replies

6. Shell Programming and Scripting

sed: How to modify files in a complex way

Hello, I am new to sed and hope that someone can help me with the following task. I need to modify a txt file which has format like this: xy=CreateDB|head.queue|head.source|head.definition|rtf.edit|rtf.task|rft.cut abc|source|divine|line4|5|true into something like: head.queue=abc... (19 Replies)
Discussion started by: pinkypunky
19 Replies

7. Shell Programming and Scripting

looking for files and modify if size matches

hi there. I'm at SunOS 5.9 At my new job i'm using UNIX, and it's my first time. i'm trying to make a script for: -find files with a name passed to it as parameter -compare results with file size passed as parameter too -when comparison's true --> move file -if not--> make nothing ... (3 Replies)
Discussion started by: viko
3 Replies

8. UNIX for Advanced & Expert Users

Modify files through scripts

Hello, all I want to implement some actions on the specified files, to modify some contents in the files, as follows: File1: **** name carol birthday 830319 ******* name billy birthday 831001 *************** ____________________________ The... (3 Replies)
Discussion started by: tpltp
3 Replies

9. UNIX for Dummies Questions & Answers

how to permanently modify the open files

hi all, any ideas how we can permanently modify the open files? bash-2.03$ ulimit -a core file size (blocks) 0 data seg size (kbytes) unlimited file size (blocks) unlimited open files 256 <--------------- pipe size (512 bytes) 10 stack size... (2 Replies)
Discussion started by: 3rr0r_3rr0r
2 Replies

10. UNIX for Advanced & Expert Users

Modify files

Hi everybody, I have a certine file with lots of number, Which I want to add a " in the begging and at the and of each line. Could anyone tell me how can I do it? Cheers (7 Replies)
Discussion started by: amgo
7 Replies
Login or Register to Ask a Question