LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-05-2008, 12:32 AM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Rep: Reputation: 69
piping question


I"m trying to make this command work. I wanna take the contents in this .txt file, sort it, then overwrite the contents with the newly sorted contents.

I've been trying

cat ip.txt | sort > ip.txt

but then it just clears the whole file.
It works when I create a whole separate file.

cat ip.txt | sort > ip2.txt

How do I make it work to where it just overwrites the original file?
 
Old 06-05-2008, 12:53 AM   #2
charlie0313
Member
 
Registered: Jan 2008
Location: Dunn Loring, VA
Distribution: Slackware 12.0, Ubuntu 8.04
Posts: 47

Rep: Reputation: 15
Instead of doing that try this:

Quote:
cat ip.txt | sort > new-file
rm ip.txt && mv new-file ip.txt
I tried what you did and it just overwrote the origanal and left it blank. The obove will work though.
 
Old 06-05-2008, 12:58 AM   #3
aus9
LQ Guru
 
Registered: Oct 2003
Posts: 5,124

Rep: Reputation: Disabled
I am not a guru but this worked

sort 1.txt > 2.txt && mv -f 2.txt 1.txt
 
Old 06-05-2008, 02:39 AM   #4
ischi
Member
 
Registered: Apr 2008
Location: Tübingen
Distribution: Fedora 9 (Thinkpad T60), Debian 3.1 (Server)
Posts: 51

Rep: Reputation: 15
You cant actually do this in one command because as soon as you write to the file again there is new input for sort, this kind of screws sort up (not gonna explain this in detail) just do it like suggested, in 2 commands
Code:
sort ip.txt > ip2.txt && mv ip2.txt ip.txt
 
Old 06-05-2008, 03:17 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
The redirection is setup before the first command runs so you will be zeroing out the file you want to use as the input.

Some commands like sed have an option that allows you to process the file in-place. A temp file is created in the background.

Last edited by jschiwal; 06-05-2008 at 07:17 PM. Reason: inline -> in-place edit
 
Old 06-05-2008, 08:28 PM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
In Bash this seems to work:
Code:
echo "$(sort ip.txt)" > ip.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
piping | jezz Linux - General 2 05-04-2008 05:28 AM
Piping ls Kcghost Linux - General 9 03-05-2008 08:24 PM
Piping... disruptive Programming 3 02-12-2008 02:09 PM
Very difficult piping question... 3saul Linux - Software 1 02-28-2006 01:49 AM
Piping question OtisLinux Linux - Software 1 02-12-2004 02:38 PM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration