Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Subtracting two files


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Subtracting two files

Hi,

I want to subtract 2 files and save the remaining text in another file. Lets say,


Code:
Hello
Happy  //
Hi
*
Hungry

File2

Code:
Happy
Hi


Output

Code:
Hello
 //
*
Hungry

I do not want to remove the whole line. Just need the exact subtraction. When I used grep -vf file2 file1 It removes whole line. How to get the exact subtraction? Thanks

Last edited by Don Cragun; 08-11-2016 at 11:15 PM.. Reason: Add CODE AND ICODE tags.
# 2  
What operating system and shell are you using?

What have you tried to solve this problem on your own?
# 3  
I'm using opensuse 13.2. Yeah I figured it out using,

Code:
grep -Fvf file2 file1

This removes the exact text needed to be subtracted. By the way can you help me to differentiate between pointers and multiplications of a C cod. I need to eliminate lines, containing pointers from a C code and save the rest to a text file using a shell script

Last edited by zaxxon; 08-12-2016 at 04:38 AM..
# 4  
how about using sed ?

try with test data, before applying this command to the real data.

here, we are using sed -i

Code:
while read pattern; do sed -i "s/$pattern//g" file1 ; done < file2

# 5  
Try also
Code:
awk 'FNR == NR {T[$1]; next} {for (t in T) sub (t, _)} 1' file2 file1
Hello
  //

*
Hungry

This User Gave Thanks to RudiC For This Post:
# 6  
Hi beginner_99,
Note that the command:
Code:
grep -Fvf file2 file1

doesn't just remove the strings found in file2 from file1; it removes every line from file1 that contains any string found in file2.

And, assuming that the strings contained in file2 only consist of characters that are not "special" in a regular expression (RE), the code suggested by itkamaraj will not only remove the strings Happy and Hi from file1, it will also remove the string HHappyi (by removing Happy in the 1st invocation of sed and by removing the remaining Hi in the 2nd invocation of sed after removing Happy from HHappyi in the 1st invocation of sed).

The awk script suggested by RudiC will not only remove the strings Happy and Hi, but will also either remove the string HHappyi as in itkamaraj's sed script OR the strings HHiappy, HaHippy, HapHipy, and HappHiy depending on which of the two possible random orders awk uses to process the two lines found in file2 (there are 2**(N-1) possible random orders to process the N lines in file2 for the more general case).

If the strings you are processing might contain characters that are "special" in a regex used by awk or sed, you need to use awk instead of sed and use match() instead of sub() to find the matching strings or preprocess the REs in file2 to escape "special" characters.

If you want to remove all remaining matching strings after removing strings on the first pass, you'll need to use awk or sed commands to delete matched strings in a loop until no more matches are found.

Knowing these options, can you tell us if any of these issue matter with the real data you will be processing? And, if so, do you need help in figuring out how to make the needed changes to the suggestions you have received so far?
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Changing CSV files with date . Subtracting date by values
arunkumar_mca
Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever...... UNIX for Beginners Questions & Answers
8
UNIX for Beginners Questions & Answers
Subtracting two files by string
a_bahreini
Hi, I have file with a list of names like this: dfdsf ddvc dsfgf gfdg dgfdgt gfdgdf I have another file with three columns like this (tab delimited): wwrwe rgdfg sgfd dgfd sdgdg dfg fsss dfgdf sdgfd Now I want the lines whose 2nd column is not similar to any of the strings in the...... Shell Programming and Scripting
1
Shell Programming and Scripting
Subtracting columns against each other
Fredrick
Hi All, I have a file of 100 lines of each having 1000 columns. I need to find the difference of each column against each other. That means, Col1-Col1; Col1-Col2; Col1-Col3;......Col1-Col1000; Col2-Col1; Col2-Col2; Col2-Col3;.... and so on ....up to Col1000-Col1000. Lets say the file is...... Shell Programming and Scripting
6
Shell Programming and Scripting
Subtracting with awk?
fugitive
i have a small awk script which prints the 5 columns of different o/p i want the 5th column subtracted from 100 and then display the result .. but i do not get the desired result .. I 'm using following script awk ' BEGIN { FS="" RS="us" } { ...... Shell Programming and Scripting
3
Shell Programming and Scripting
comparing files - adding/subtracting/formating columns
oabdalla
I have two files: file1.txt: FS Total Used Free Used% /u01 10000 8000 2000 80% /u02 10000 8000 2000 80% /u03 10000 8000 2000 80% /u04 10000 8000 2000 80% /u05 10000 8000 2000 80% /u06 10000 8000 2000 80% /u07 10000 8000 2000 80% /u10 10000 5000 5000 50% file2.txt:...... Shell Programming and Scripting
7
Shell Programming and Scripting

Featured Tech Videos