![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strange behaviour from script in crontab | PilotGoose | Shell Programming and Scripting | 1 | 06-26-2008 10:54 AM |
| Help with my weird script! | kdyzsa | Shell Programming and Scripting | 1 | 06-15-2008 11:39 PM |
| Weird sudo behaviour | geomonap | UNIX for Advanced & Expert Users | 1 | 02-03-2006 05:08 PM |
| any explanation for thsi shell script behaviour | xiamin | Shell Programming and Scripting | 9 | 11-09-2001 01:13 PM |
| Weird script | Duckman | UNIX for Dummies Questions & Answers | 2 | 03-14-2001 01:53 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Weird sed behaviour in script
I've written a small script to replace certain words in all the the files in a directory. Code:
#!/bin/sh #Get list of files to be edited file_list=`ls -p` for i in $file_list do echo "Processing $i" alteredi=`echo "$i" | sed -e 's/\//d/'` if [ $i = $alteredi ] then if [ $i != "maketest" ] then #actual altering cat $i | sed -e "s/login\//login.tst\//" > $i cat $i | sed -e "s/cyberkd\//cyberkd.tst\//" > $i cat $i | sed -e "s/\/db_connect.inc.php/\/testdb_connect.inc.php/" > $i echo " $i has been altered" else echo " Not altering myself" fi else echo " Not altering directories" fi done Now, when I run this script as a normal user, only the first 4kB of the file is processed. So all files larger than 4kB are cut in half. The remaining bytes are just left out of the new file. When I ran the script as root, 8kB were processed. Is there a way to process the entire files? When I cat a large text file the entire file gets printed on my screen. Thanks in advance. |
|
||||
|
Don't read and write to the same file and using cat with sed is redundant, replace these lines: Code:
cat $i | sed -e "s/login\//login.tst\//" > $i cat $i | sed -e "s/cyberkd\//cyberkd.tst\//" > $i cat $i | sed -e "s/\/db_connect.inc.php/\/testdb_connect.inc.php/" > $i with: Code:
sed -e "s/login\//login.tst\//" -e "s/cyberkd\//cyberkd.tst\//" -e "s/\/db_connect.inc.php/\/testdb_connect.inc.php/" "$1" > temp.file mv temp.file "$1" If you're sed version supports the -i flag you can edit the file in place without using a temporary file. Code:
sed -i -e "s/login\//login.tst\//" -e "s/cyberkd\//cyberkd.tst\//" -e "s/\/db_connect.inc.php/\/testdb_connect.inc.php/" "$1" Regards Last edited by Franklin52; 08-30-2008 at 08:52 AM.. |
![]() |
| Bookmarks |
| Tags |
| cat, sed, shell |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|