[HELP] - Delete rows on a CSV file

Hello to all members,

I am very new in unix stuff (shell scripting), but a want to learn a lot. I am a ex windows user but now i am absolutely Linux super user... :smiley:

So i am tryng to made a function to do this:

I have two csv files only with numbers, the first one a have:

1
2
3
4
5

in the second file i have:

2
3
5

So the result i want in my new file is:

1
4

I compare the two files and all the numbers that are not equal it will be written in a new file.

Until now i can only grep the numbers that are the same in both files.

for number in `cat <File1.csv>; do
grep $number <File2.csv>
done

Can anyone help me with this?

Thanks

There are several ways to do this, but this follows the example you gave. You have to use the file with the fewer lines to grep the file with more. This isn't 100% accurate though. Suppose you have 6 in file2 and not in file1, then it would not be printed. You can create two loops and reverse the files on the second one to catch this.

for num in `cat File2.csv`
do 
    strng=${strng:-"($num"}${strng:+"|$num"} 
done

strng=${strng}${strng:+")"}  # creates '(2|3|5)'

egrep -v $strng File1.csv

You could also do this with a simple diff, but then you have to format the output properly.

diff num1.txt num2.txt
1d0
< 1
4d2
< 4

These are not CSV files.

When you post code, please wrap it in

 tags.



for number in `cat <File1.csv>; do
 grep $number <File2.csv>
done

[/quote]

[indent]
This prints the numbers that are in file1 but not in file2:

grep -vf file2  file1
# awk 'FNR==NR{a[$0];next}!($0 in a)' file2 file1
1
4

So i made this...

# awk 'FNR==NR{a[$0];next}!($0 in a)' file2 file1 > file3

I get all the numbers that are not repeaded between file2 and file1 and i write them in a new file.
But i didn't urderstand what this means "'FNR==NR...".

Thanks a lot.

read the manual

Assuming that the numbers are sorted in each file, the following command prints numbers from either file that are not common:

join -a1 -a2 -o1.1,2.1 file1 file2 | awk 'NF==1'

tyler_durden