Weird sed behaviour in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird sed behaviour in script
# 1  
Old 08-30-2008
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  
Old 08-30-2008
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  
Old 08-30-2008
Thank you. It works!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inconsistent behaviour of sed - UNIX AIX 5.3

Hi there. I'm facing a strange & an intriguing behaviour with sed while replacing the tab character with a space reading from a file. It randomly works sometimes but mostly doesn't work. Below is what's happening- <tab> here is the actual literal tab. user1> cat temp2 1<tab>2<tab>3 4... (2 Replies)
Discussion started by: Deepak.Dhami
2 Replies

2. Shell Programming and Scripting

Weird redirection behaviour

Linux. Bash 4.2.10. Let's say that i want the stderr of a command redirected to one file (err), and both stdout and stderr redirected to another file (outanderr). Easy. Many ways to do it. But may times the file outanderr will be messed up (with the output and the error mixed toghether). ... (3 Replies)
Discussion started by: Lem
3 Replies

3. Ubuntu

Weird rm behaviour

I am little bit confused by the behaviour of rm in Ubuntu. It seems that as a regular user I can delete files owned by another user even when the permissions are set to 644. Here is an example: cjohnson@carbon:~/test$ sudo touch testfile cjohnson@carbon:~/test$ ls -al total 8 drwxr-xr-x... (2 Replies)
Discussion started by: ccj4467
2 Replies

4. Shell Programming and Scripting

find: "weird" regex behaviour

I have these two files in current dir: oos.txt oos_(copy).txt I execute this find command:find . -regex './oos*.txt'And this outputs only the first file (oos.txt)! :confused: Only if I add another asterisk to the find find . -regex './oos*.*txt' do I also get the second file... (7 Replies)
Discussion started by: courteous
7 Replies

5. Shell Programming and Scripting

Weird script behaviour !

Hello, I am getting an infinite loop from a script in Linux. Here is the last version of the script in question. As you can see I tried to define everything properly: #!/bin/ksh # Script to loop over a series of dates set -ex typeset -i start_date=20090701 typeset -i... (2 Replies)
Discussion started by: stavros
2 Replies

6. Shell Programming and Scripting

Explanation for interesting sed behaviour?

This is my first post so hi to you all. I have browsed these forums in the past and what a great community and resource this is! Thanks to all the contributors ... I look forward to being able to give something back. In the meantime, I have a little conundrum concerning sed. My very simple... (6 Replies)
Discussion started by: Gavster
6 Replies

7. Shell Programming and Scripting

strange behaviour from sed???

Hi all, I want to do a very simple thing with sed. I want to print out the line number of a disk I have defined in /etc/exports, so I do: It's all good, but here's the problem. When I define md0 in a variable, I get nothing from sed: Why is that? can anybody please help? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

8. Shell Programming and Scripting

weird issue about h, g, x in SED

I have a file called merge2.t: Hi Hello how are you. </Endtag> <New> I am fine.</New> This is a test. freelong how Here is the SED: sed -n ' /<\/Endtag>/ !{ H } /<\/Endtag>/ { x p } (4 Replies)
Discussion started by: freelong
4 Replies

9. UNIX for Advanced & Expert Users

Strange sed behaviour

$ echo a.bc | sed -e "s/\|/\\|/g" |a|.|b|c| $ Is the behavior of the sed statement expected ? Or is this a bug in sed ? OS details Linux 2.6.9-55.0.0.0.2.ELsmp #1 SMP Wed May 2 14:59:56 PDT 2007 i686 i686 i386 GNU/Linux (8 Replies)
Discussion started by: vino
8 Replies

10. UNIX for Advanced & Expert Users

Weird sudo behaviour

Hi gurus. I implemented sudo and have the following in my sudo config file *************** # User alias specification User_Alias VENDOR = user1 # User privilege specification VENDOR ALL = NOPASSWD: /bin/, /sbin/, /usr/local/bin/, \ !/bin/su,... (1 Reply)
Discussion started by: geomonap
1 Replies
Login or Register to Ask a Question