Is there any mistake in this code:


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there any mistake in this code:
# 1  
Old 05-28-2010
Is there any mistake in this code:

Code:
cat $1 | sort -n | uniq | $1

in other words, I sort the content of the file and put the ouput in the same file, is there any mistakes in this cshell code ???

Last edited by Scott; 05-29-2010 at 06:05 PM..
# 2  
Old 05-28-2010
First off, you really shouldn't be using c-shell.

Second, you start right off the bat with a useless use of cat.

Code:
# cat is not needed here
sort -n < $1 | uniq | $1

Third, $1 isn't a command name so you can't pipe into it. Pipes are for commands, files use redirection.

Code:
# Use redirection instead of pipe
sort -n < $1 | uniq > $1

Fourth, this doesn't work in general. It's only working here because of special properties of the sort command: It reads the entire file before writing any output, hence reads the entire file before uniq is allowed to truncate it. This will NOT work:

Code:
sort -n < file > file

So in general, redirecting to the same file you're reading from is a bad idea. It only works in very special circumstances.
# 3  
Old 05-28-2010
Additionally you could check in the sort man page, if your sort command supports the -u switch, in that case you could abandon the uniq command, e.g.
Code:
sort -nu file > newfile

# 4  
Old 05-29-2010
what happens is:

the shell reads the command line, substitutes parameters
then it will spot the redirect which says
"truncate the file and send ouput to it".
so it truncates the file.
only then does it run the command, but by then the
file's been trashed.


see?
# 5  
Old 05-29-2010
Quote:
Originally Posted by pseudocoder
Additionally you could check in the sort man page, if your sort command supports the -u switch, in that case you could abandon the uniq command, e.g.
Code:
sort -nu file > newfile

And if the -o option is supported, you can do :
Code:
sort -nuo $1 $1

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Archive script spot a mistake?

#!/bin/bash source=/to_be_archived dest=/archived echo "is this archive for an audio tar press (t) or an audio directory press (d)" read option case $option in d) cd "$source" echo "please specify full path to directory you want to be... (6 Replies)
Discussion started by: robertkwild
6 Replies

2. Shell Programming and Scripting

Strange type mistake?!

Hi, I want to start MY_PROGRAM in a bash script with additional parameters given in the CONFIGURATION_ARRAY. IFS="'" CONFIGURATION_ARRAY=( '-N 0 -m 0' '-N 0 -m 1' ) for configuration in ${CONFIGURATION_ARRAY} do //DEBUG N=${configuration%-*} //-N 0 M=-${configuration##*-} //-m 0... (5 Replies)
Discussion started by: xraystorm
5 Replies

3. UNIX for Dummies Questions & Answers

Can anyone help me to spot my mistake?

Hi there can anyone help me to spot my mistake and please explain why it appears My code : #!/usr/bin/gawk -f BEGIN { bytes =0} { temp=$(grep "datafeed\.php" | cut -d" " -f8) bytes += temp} END { printf "Number of bytes: %d\n", bytes } when I am running ./q411 an411 an411: ... (6 Replies)
Discussion started by: FUTURE_EINSTEIN
6 Replies

4. UNIX for Dummies Questions & Answers

Probably some stupid mistake...

Hi everyone ! I have a file wich look like this : >Sis01 > Sis02 ... >Sis44 I want to separe each paragraphe in a different file, so I decide to use the "FOR" loop + sed. for f in {01..44} do (5 Replies)
Discussion started by: sluvah
5 Replies

5. UNIX for Dummies Questions & Answers

Deleted the scripts in Crontab by mistake

hi, instead of typing crontab -e i gave crontab -r and hit enter. So i lost all my scripts. Is there any way to restore the deleted scripts? Please help me out Thanks Ajay (3 Replies)
Discussion started by: ajayakunuri
3 Replies

6. Solaris

Renamed lib directory by mistake

Let's say someone accidentally renamed the lib directory in Solaris 8, and now they cannot get into the terminal or even rename the folder via file manager.What would one do? (37 Replies)
Discussion started by: jetjaguar
37 Replies

7. Shell Programming and Scripting

Can't find the mistake in sed expression

Hi there, Can anyone help me find the correct expression for sed. I want to repace iface eth0 inet wathever with iface eth0 inet static Thanks for your help Santiago (5 Replies)
Discussion started by: chebarbudo
5 Replies

8. AIX

Did a Mistake with HACMP

Hi, I needed space on a FS, and when I've added the space on the filesystem, I did it trough the regular smitty fs inteface and not with smitty cl_lvm. Can someone help me to repair the situat before a faileover happen ? Thanks for your help,:mad: (13 Replies)
Discussion started by: azzed27
13 Replies

9. UNIX for Dummies Questions & Answers

Crontab Mistake!!!

Hi. I hope someone can help me with this problem. Being a novice to Unix, I editted my crontab directly by typing " crontab -e ". Well, I needed to make some changes so, I typed " crontab -r ". Now I have no crontab, and I can't seem to get crontab to write a new file. I' ve tried: vi... (4 Replies)
Discussion started by: cstovall
4 Replies
Login or Register to Ask a Question