LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-23-2005, 02:04 PM   #1
Prasun1
Member
 
Registered: Jun 2005
Posts: 41

Rep: Reputation: 15
erase all the lines of a file


Hi all,


I am trying to design a shell script that will take as an input the name of file.It will then go through each lines inside the file and remove each line ..I mean erase all the lines once it finishes scanning the file.

Please help me with the programming concepts.


Thanks and Regards
Prasun
 
Old 09-23-2005, 02:32 PM   #2
JamieBrown
Member
 
Registered: Jan 2005
Location: Great Ayton, North Yorkshire, UK
Distribution: Gentoo, Mandriva, RHES, Debian
Posts: 61

Rep: Reputation: 15
Hi,

If you do mean erase all of the lines from the file *after* it has finished reading *all* of the lines, then you could just use this command at the end of your script.

echo "" > [filename]

That will replace the full file with an empty file.

If you mean you want to remove each line one at a time after you've read each line then I'm not sure.

Cheers,

Jamie.
 
Old 09-23-2005, 05:04 PM   #3
Prasun1
Member
 
Registered: Jun 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks Jamie...I wanted just that as u have shown


Regards
Prasun
 
Old 10-06-2005, 05:37 AM   #4
izghitu
Member
 
Registered: Oct 2005
Location: localhost.localdomain
Distribution: CentOS
Posts: 108

Rep: Reputation: 16
for example i have a file containing the folowing lines:
123123123234324
qweasfsadf
asdfgasdga
dfgsadfgdfg
123
aesfsdf
------------

how can i remove freom the file the line:123
with bash commands without using: sed /123/d filename > outputfilename

I just need to remove a line without using an intermediate file

Thank you
 
Old 11-06-2005, 09:46 AM   #5
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 492

Rep: Reputation: 75
Don't forget the ' in you sed script
Code:
sed '/123/d' filename > outputfilename
 
Old 11-06-2005, 10:24 AM   #6
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
Quote:
Originally posted by izghitu
for example i have a file containing the folowing lines:
123123123234324
qweasfsadf
asdfgasdga
dfgsadfgdfg
123
aesfsdf
------------

how can i remove freom the file the line:123
with bash commands without using: sed /123/d filename > outputfilename

I just need to remove a line without using an intermediate file

Thank you
Well, that would be simply solved with this if you dont want to use sed:
Quote:
cat file | grep -v "123" > file
but it will take out the whole line, not just the excercept. After that you may want to remove the empty lines so the full line would be
Code:
cat file | grep -v "123" | grep -vx "" > file
edit: if you want to take out just the line with the _exact_ stuff as 123 and not a line with 123123 you could apply the x parameter also to the first grep..

Last edited by Artanicus; 11-06-2005 at 10:26 AM.
 
Old 11-06-2005, 10:45 AM   #7
makuyl
Senior Member
 
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 53
Try: grep -vx "123" file > file2 && mv -f file2 file
BTW: for sed to replace you can use "sed -i '/123/d' file" but that removes the first line as well
 
Old 11-06-2005, 10:50 AM   #8
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
Quote:
Originally posted by makuyl
Try: grep -vx "123" file > file2 && mv -f file2 file
BTW: for sed to replace you can use "sed -i '/123/d' file" but that removes the first line as well
basicly the same as mine except you need to use the mv trick if you grep it directly. if you first cat it out you can direct it straightr over the initial file. (tried and tested, had a real brainteaser with some tail sessions which lead me to believe there is a slight bug in grep / tail but enough of that.. (: )
 
Old 11-06-2005, 10:52 AM   #9
makuyl
Senior Member
 
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 53
I know it's basically yours. Just thought I'd post another way of using it.
 
Old 11-06-2005, 10:53 AM   #10
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
Quote:
Originally posted by makuyl
I know it's basically yours. Just thought I'd post another way of using it.
ah (: Well, sometimes throwng thru a seperate file can be impossible (with very strict dir permissions) but on the otherhand it perhaps clarifies the idea.. In anycase, ill shut up now.. (;
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recent File History: how do you erase it? Yoozer Linux - Newbie 2 09-16-2005 05:34 PM
Command erase lines paraiso Linux - Newbie 4 05-12-2005 10:48 AM
erase file ust Linux - General 1 03-10-2005 08:46 AM
Probleme to erase a file os2 Programming 2 02-15-2005 03:17 PM
Is there a way to modify/edit/erase a file in a WinXP partition from within Linux? aixarat Linux - Newbie 8 09-25-2004 11:07 PM


All times are GMT -5. The time now is 06:43 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration