LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-22-2008, 11:06 PM   #1
Ericchiu
LQ Newbie
 
Registered: Apr 2008
Posts: 4

Rep: Reputation: 0
How to delete the contents of a file


I just learn the command "diff"
now I have 2 files named text1 and text2
and the contents are like this :
text1:
a
b
c
================
text2:
c
d
e
================
now I use the command :

diff -u text1 text2 > text 3
and the result will be :
--- text1
+++ text2
-a
-b
+d
+e
c
=================
I want to keep the same contents in 2 files
how should I do
please help me
thank you.
 
Old 04-23-2008, 01:00 AM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
You mean you want to apply the text3 file so text1 and text2 are the same?

It helps you think about it if you alter your filenames slightly:

diff file1 file2 > file12.diff

file12.diff contains instructions- to turn file1 into file2, it is called a "patch". (A whole lot of stuff probably just snapped into perspective?)

patch file1 file12.diff

The result will still be called file1 - you can use patch and diff with version numbers to help with this.

Last edited by Simon Bridge; 04-23-2008 at 01:04 AM.
 
Old 04-23-2008, 03:26 AM   #3
Ericchiu
LQ Newbie
 
Registered: Apr 2008
Posts: 4

Original Poster
Rep: Reputation: 0
I think I didn't explain my question clearly...

if i have a file named text3 and the contents are as below:
================================================================
+a
+b
c
-d
-e
================================================================

all i want to do is to delete the line being with "+" and "-"

then the file text3 will have the content:
==================================================================
c
==================================================================
Is there any linux command can help me delete the line in the content?

I have one more question here, how can I use "sort" to make the text3 contents like this:
=====================================================================
c
+a
+b
-d
-e
=====================================================================
I just want to pick up the lines without starting symbol "+" and "-"
 
Old 04-23-2008, 03:59 AM   #4
elthox
Member
 
Registered: Oct 2006
Posts: 41

Rep: Reputation: 15
Quote:
Originally Posted by Ericchiu View Post
I think I didn't explain my question clearly...

if i have a file named text3 and the contents are as below:
================================================================
+a
+b
c
-d
-e
================================================================

all i want to do is to delete the line being with "+" and "-"

then the file text3 will have the content:
==================================================================
c
==================================================================
Is there any linux command can help me delete the line in the content?

I have one more question here, how can I use "sort" to make the text3 contents like this:
=====================================================================
c
+a
+b
-d
-e
=====================================================================
I just want to pick up the lines without starting symbol "+" and "-"


Try this:

fgrep -xf file1 file2
 
Old 04-23-2008, 04:08 AM   #5
Ericchiu
LQ Newbie
 
Registered: Apr 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by elthox View Post
Try this:

fgrep -xf file1 file2
Yeah, I just need this command
thank you and thank Simon Bridge for helping solve the problem.
 
Old 04-23-2008, 09:26 AM   #6
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Ah - so you wanted to find what files text1 and text2 had in common?
 
Old 04-23-2008, 09:16 PM   #7
Ericchiu
LQ Newbie
 
Registered: Apr 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Talking

Quote:
Originally Posted by Simon Bridge View Post
Ah - so you wanted to find what files text1 and text2 had in common?
Yeah, I just wanted to find the same components in the 2 files.

However, You let me learn more about linux command, I am appreciated.
 
  


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
how to delete entire contents of file using vi editor once sunlinux Linux - General 1 08-17-2007 06:48 AM
how to delete the file contents using shell scripts sharad Linux - Software 3 05-26-2006 12:44 PM
how to delete the contents of a file using shell scripts sharad Linux - Software 5 05-22-2006 06:22 AM
Delete contents of the file, but not file? mikeshn Linux - General 9 05-28-2003 07:47 AM


All times are GMT -5. The time now is 03:07 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