The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Query:just need to remove the contents of the file without deleting that namishtiwari Shell Programming and Scripting 3 02-04-2008 05:41 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 10:17 PM
remove the contents of a file surjyap SUN Solaris 2 03-02-2006 04:02 PM
Creating file contents using contents of another file ReV Shell Programming and Scripting 21 02-24-2006 10:25 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 05:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 11-28-2006
sumesh.abraham
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Remove the contents of a file without deleting the file

Hi All,

I want to delete the contents of few files which are really huge in size.
How the same can be done by using sed. Is there any other alternative to sed.

Thanks in advance
  #2 (permalink)  
Old 11-28-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Without using sed.

Code:
> yourfile.txt
  #3 (permalink)  
Old 11-28-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed '1q' inputfile >tmp
mv tmp inputfile
or

Code:
sed '' ipfile > ipfile
  #4 (permalink)  
Old 11-28-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
for huge files,
this would be faster

Code:
cp /dev/null file
  #5 (permalink)  
Old 11-28-2006
sumesh.abraham
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
sed '1q' inputfile >tmp
mv tmp inputfile

will not remove the first line.

sed '' filename > filename works!!

Madhan,
Can u pls explain cp /dev/null.. What is the advantage it brings over sed/ others...

Thanks
Sumesh
  #6 (permalink)  
Old 11-28-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by sumesh.abraham
sed '1q' inputfile >tmp
mv tmp inputfile

will not remove the first line.

sed '' filename > filename works!!

Madhan,
Can u pls explain cp /dev/null.. What is the advantage it brings over sed/ others...

Thanks
Sumesh

Forgot to add -n option
Code:
sed -n '1q' inputfile >tmp
mv tmp inputfile
  #7 (permalink)  
Old 11-28-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
FWIW -
Code:
> file
is the fastest call, as it truncates the file. And any command, like the sed example above, that has "sed ' ' file > file" in it already sets the file size to zero before running sed. So the sed call (or any call ) is doing nothing because the file is already empty before sed executes.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0