LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to delete the contents of a file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-the-contents-of-a-file-637184/)

Ericchiu 04-22-2008 10:06 PM

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.

Simon Bridge 04-23-2008 12:00 AM

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.

Ericchiu 04-23-2008 02:26 AM

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

elthox 04-23-2008 02:59 AM

Quote:

Originally Posted by Ericchiu (Post 3129962)
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

Ericchiu 04-23-2008 03:08 AM

Quote:

Originally Posted by elthox (Post 3129982)
Try this:

fgrep -xf file1 file2

Yeah, I just need this command
thank you and thank Simon Bridge for helping solve the problem.

Simon Bridge 04-23-2008 08:26 AM

Ah - so you wanted to find what files text1 and text2 had in common?

Ericchiu 04-23-2008 08:16 PM

Quote:

Originally Posted by Simon Bridge (Post 3130244)
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.


All times are GMT -5. The time now is 09:21 PM.