Delete file2.txt from file1.txt using scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete file2.txt from file1.txt using scripting
# 1  
Old 11-22-2012
Delete file2.txt from file1.txt using scripting

Hi,

I`m a total newbie, well my requirement is that i have 2 files


I want to identify which countries i do not currently have in db..

how can i use the grep or another command to find this file ..

i want to match all-countries.txt with countries-in-db.txt so the output is equal to countries which are not currently in my db?

any command that i can use to achieve this would be helpful

Thanks
# 2  
Old 11-22-2012
Hi Beanbaby,

I think you can use diff file1.txt file2.txt. It will show you the differnece between the two files.
# 3  
Old 11-22-2012
Code:
$ cat db.txt
US
SG
AU
$ cat country.txt 
US
IN
GB
JP
SG
AU
$ awk 'NR==FNR{a[$0]++;next}{if (!($1 in a))print}' db.txt country.txt
IN
GB
JP

# 4  
Old 11-22-2012
Thank You so much...

However my db has 127 countries... and thewholeworld db has 260 countries.. the result from the above command

Code:
awk 'NR==FNR{a[$0]++;next}{if (!($1 in a))print}' db.txt country.txt

IN is 219.

Should it not be close to 130 (the countries that are not listed in db.txt?

Thanks!

Last edited by Franklin52; 11-22-2012 at 04:16 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 11-22-2012
post some contents of your files and expected output.

---------- Post updated at 12:02 PM ---------- Previous update was at 12:00 PM ----------

if your country name has something like "Saudi Arabia" ( with space ), then you need to try out this

Code:
awk 'NR==FNR{a[$0]++;next}{if (!($0 in a))print}' db.txt country.txt

# 6  
Old 11-22-2012
Hello,

Yes there are countries like 2 spaces and 3 spaces, like the example below

allcountries.txt
Code:
Uganda
United Arab Emirates
United Kingdom
United States
Uruguay

db.txt
Code:
Uganda
United States
Uruguay

result.txt

should have countries not listed in my database like the below
Code:
United Arab Emirates
United Kingdom

Many Thanks!

Last edited by Franklin52; 11-22-2012 at 04:17 AM.. Reason: Please use code tags for data and code samples
# 7  
Old 11-22-2012
Code:
 
 $ awk 'NR==FNR{a[$0]++;next}{if (!($0 in a))print}' db.txt country.txt 
United Arab Emirates
United Kingdom

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

2. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

3. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. Shell Programming and Scripting

[Solved] delete line from file1 by reading from file2

Hi All, I have to arrange one of the text file by deleting specific lines. cat file1.txt 3595 3595 -0.00842773 -0.0085077 0.00368851 12815 12815 -0.00929239 0.00439785 0.0291697 3747 3747 -0.00974353 0.00228922 0.0225058 3574 3574 -0.00711399 -0.00315748 0.0141206 .... 12734... (7 Replies)
Discussion started by: senayasma
7 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. Shell Programming and Scripting

awk '{print $ from file1.txt}'

Hi All, I have a file1.txt where the index of the columns are placed. I want to get the columns from file2.txt corresponding to these index numbers. I was usually using awk '{print $5, $6, $2, $3, ...}' file2.txt > output.txt However, this list is very long. So, i want to read the... (4 Replies)
Discussion started by: senayasma
4 Replies

7. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

8. Shell Programming and Scripting

sed to cp lines x->y from 1.txt into lines a->b in file2.txt

I have one base file, and multiple target files-- each have uniform line structure so no need to use grep to find things-- can just define sections by line number. My question is quite simple-- can I use sed to copy a defined block of lines (say lines 5-10) from filename1.txt to overwrite an... (3 Replies)
Discussion started by: czar21
3 Replies

9. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

10. Shell Programming and Scripting

delete lines from file2 beginning w/file1

I've been searching around here and other places, but can't put this together... I've got a unique list of words in file 1 (one word on each line). I need to delete each line in file2 that begins with the word in file1. I started this way, but want to know how to use file1 words instead... (13 Replies)
Discussion started by: michieka
13 Replies
Login or Register to Ask a Question