The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-30-2008
Peetrus Peetrus is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
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.
  #2 (permalink)  
Old 08-30-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,333
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..
  #3 (permalink)  
Old 08-30-2008
Peetrus Peetrus is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
Thank you. It works!
Closed Thread

Bookmarks

Tags
cat, sed, shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0