How to save sorted content of a inside the same file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to save sorted content of a inside the same file?
# 1  
Old 09-23-2014
How to save sorted content of a inside the same file?

Hi All,

When i use sort Test, here is the output:
[ha@cs ~]
Code:
$ sort Test
a
b
b
c
d
e
f
g
h
i

But the contents in the file remain unsorted, how to do that?

]
Code:
$ cat Test
g
i
h
a
c
b
d
f
e

Thanks for any input.

Regards,
Chandu

Last edited by Don Cragun; 09-23-2014 at 05:22 AM.. Reason: Add CODE tags.
# 2  
Old 09-23-2014
Hi,

You should use;

Code:
sort < Test > sorted.txt

Regards

Dave
# 3  
Old 09-23-2014
Or, more simply:
Code:
sort -o Text Text

The sort utility is one of the very few standard utilities that provides an option to non-destructively overwrite one of its input files.

But, I have no idea how you got two lines in your sorted output that contained b when that text only appeared on one line in your input file???
# 4  
Old 09-23-2014
Blade

Hi Dave,
1. I don't want to create another file, i just want to sort the contents of the Test file itself.

Thanks for your help.

Hi Don,

But, I have no idea how you got two lines in your sorted output that contained b when that text only appeared on one line in your input file???
>> Actually the files has content of alphabets from a-z. In order to shorten the text i edited the output and input.

Between, this command erased the contents of my file.
Code:
sort -o Text Text

Code:
[ha@cs ~]$ cat Tested
a
b
b
c
d
e
f
g
h
i
j
k
l
m
m
n
n
o
p
q
r
s
t
u
v
v
w
x
y
z
[ha@cs ~]$ sort -o Tested Test
[ha@cs ~]$ cat Tested
[ha@cs ~]$ cat Test

Regards,
Chandu

Last edited by rbatte1; 09-23-2014 at 07:30 AM.. Reason: Added CODE tags
# 5  
Old 09-23-2014
This is a common problem. When you run the command, the output file is opened (and overwritten) before you start to read the input, which by now is null. You command then exists as it finishes reading no input.

You will need to read the input and write to a separate output file. If need be, then rename (and overwrite) the input file thus:-
Code:
sort -o output input
mv output input

It does mean that you have to consider space for having the two files at the same time, even if just for a moment.



Robin
# 6  
Old 09-23-2014
Quote:
Originally Posted by chandrakanth
Hi Dave,
1. I don't want to create another file, i just want to sort the contents of the Test file itself.

Thanks for your help.

Hi Don,

... ... ...

Between, this command erased the contents of my file.
Code:
sort -o Text Text

Code:
[ha@cs ~]$ cat Tested
a
b
b
c
d
e
f
g
h
i
j
k
l
m
m
n
n
o
p
q
r
s
t
u
v
v
w
x
y
z
[ha@cs ~]$ sort -o Tested Test
[ha@cs ~]$ cat Tested
[ha@cs ~]$ cat Test

Regards,
Chandu
You did not show us what was in the file Test before the sort. I suggested using:
Code:
sort -o Tested Tested

which will require temp file space to sort the input file, but overwrite the input file if the sort succeeds. If there isn't enough space, sort will fail but the original data in the input file will not be disturbed. (However, if there is enough space to sort the input file into another file and some other process is filling up the filesystem while sort is trying to overwrite the input file with the sorted results, there is no guarantee that data won't be lost.)

Using the command you used:
Code:
sort -o Tested Test

tells sort to sort the contents of the file Test into a temp file and if the sort completes successfully, overwrite the contents of the file Tested with the sorted results. In any case, the original contents of Test will be unchanged. So, since Test was an empty file, Tester was successfully overwritten by the sorted contents of the empty file Test.
# 7  
Old 09-23-2014
Thank You Both.

Understood.

Regards,
Chandu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to get script to create a new file that lists folder content sorted by size?

I have a script that sorts and processes unsorted files to newly created directories. Its working great, but I am trying to understand the leanest method to get the script to create an additional file within each newly created directory that: Contains a list of all files in the directory... (4 Replies)
Discussion started by: Braveheart
4 Replies

2. Shell Programming and Scripting

Replace content from a file and save

Hi, Right now there is a file called 'qm.ini' which is owned by mqm:mqm and I am trying to replace a line from this file with something else and save. I am using the below perl command to replace and save within a shell script with a different user called 'mqadm' which is also part of mqm... (1 Reply)
Discussion started by: bdpl
1 Replies

3. Shell Programming and Scripting

Problem while displaying(cat) file content inside telnet loop .

Hi Team, Not getting the file output inside my email which i am sending from unix box. . Please refer the below code : #!/bin/sh { sleep 5 echo ehlo 10.56.185.13 sleep 3 echo mail from: oraairtel@CNDBMUREAPZP02.localdomain sleep 3 echo rcpt to: saurabhtripathi@anniksystems.com... (1 Reply)
Discussion started by: tripathi1990
1 Replies

4. Shell Programming and Scripting

awk : split file and rename and save in path according to content

Hello, I'm using Windows 7 ; sed, awk and gnuwin32 are installed. I have a big text file I need to manipulate. In short, I will have to split it in thousands of short files, then rename and save in a folder which name is based upon filename. Here is a snippet of my big input.txt file (this... (4 Replies)
Discussion started by: sellig
4 Replies

5. Shell Programming and Scripting

sed - delete content inside tags multiline

I need that a certain part of the content below excluded ==Image Gallery== followed by <gallery> and the content until </gallery> test SED1 ==Image Gallery== <gallery> Image:car1.jpg| Car 1<sup>1</sup> Imagem: car2.jpg| Car2<sup>2</sup> </gallery> test SED2 ==Image... (5 Replies)
Discussion started by: dperboni
5 Replies

6. Shell Programming and Scripting

Help - delete content inside square brackets under conditions

I have the file sed1.txt and I need to strip the brackets (]) and content inside them only when I have two or three letters followed by a colon. for example,it may be any letter, not just abc ] ] #-- cat sed1.txt 1 ] FISICA 2 ]PORTUGUES 3 ] ]MATEMATICA 4 ]]INGLES ] 5 ]QUIMICA 6... (2 Replies)
Discussion started by: dperboni
2 Replies

7. Shell Programming and Scripting

Save output to file - inside a script ?

I'm using the following script to check cisco router health and I'd like to save output to a file, vty_runcmd.sh > /check/check-cisco-health script works and output is saved to a file. However using it in crontab file is created but output is not printed inside it. In crontab, */5 * * * *... (4 Replies)
Discussion started by: marmellata
4 Replies

8. Shell Programming and Scripting

how to add string inside the file's content

Hi, How to add string inside the file. In this case adding 63 in the beginning of each line. Thnks. e.g. what is inside the file Mobile Number Portability 9051151234 9051261345 9051393245 9051412345 9051507654 should like this as output: Mobile Number Portability (7 Replies)
Discussion started by: shtobias
7 Replies

9. Solaris

Content of /var/sadm/pkc/<pkg>/save directory

Hi, What are the contents of /var/sadm/pkg/<pkg>/save directory. - It contains various patches with their id's. What does the "pspool" directory inside it contain? (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

10. UNIX for Dummies Questions & Answers

grep content of files sorted by time stamp

egrep Date: *.html > out.htm I would like to grep the match as sorted by time stamp of the html files. how do I do that? (1 Reply)
Discussion started by: zer0
1 Replies
Login or Register to Ask a Question