Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

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
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:

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
LQ Addict
Registered: Oct 2003
Location: Australia
Distribution: MX 16
Posts: 5,296

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
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
sort ip.txt > ip2.txt && mv ip2.txt ip.txt
Old 06-05-2008, 03:17 AM   #5
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 336

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


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:26 PM.

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