Issue with files when there is no data in a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with files when there is no data in a file.
# 1  
Old 07-29-2014
Issue with files when there is no data in a file.

I have two files

Code:
File1.txt
 
000199458
000199463
000200442
000200831
000200866
000201009
000201050
000201405
000201666
000201682



Code:
 
File2.txt
 
DHP5104   1BWEL9                             000199458
DHP5104   1BWELD                             000199463
DHP5104   1BWEK3                             000200442
DHP5104   1BWEK3                             000200831
DHP5104   1C1FZD                             000200866
DHP5104   1BWEK3                             000202405
DHP5104   1C1FXX                             000202707
DHP5104   1C1FZD                             000203202
DHP5104   1BWEL9                             000204450
DHP5104   1BWEL9                             000204453
XVP5104   1BWEL9                             000199458

I want to check each row in File1.txt with file2.txt. If match is found then the record needs to be deleted in file2.txt



Code:
 
Output.txt
DHP5104   1BWEK3                             000202405
DHP5104   1C1FXX                             000202707
DHP5104   1C1FZD                             000203202
DHP5104   1BWEL9                             000204450
DHP5104   1BWEL9                             000204453


I below code is used to get the above output.

Code:
awk 'FNR==NR {f1[$1];next} !($3 in f1)' file1.txt file2.txt

But this is not working when the File1.txt is empty. Can someone from this group tell me how to handle if there are no rows in File1.txt. i.e when the file1.txt is empty all the rows from file2.txt should be loaded.

Any help would be appriciated!!

Thanks,
Arjun
# 2  
Old 07-29-2014
Try this instead:

Code:
awk 'FILENAME==ARGV[1]{f1[$1];next} !($3 in f1)' file1.txt file2.txt

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 07-30-2014
Or
Code:
awk 'NF==1 {f1[$1];next} !($3 in f1)' file1 file2

This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-30-2014
Quote:
Originally Posted by Chubler_XL
Try this instead:

Code:
awk 'FILENAME==ARGV[1]{f1[$1];next} !($3 in f1)' file1.txt file2.txt


Thanks buddy this is working.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

3. Shell Programming and Scripting

Combine data out of 3 files into one new file

Hi, How can I combine the data of of three files into one new file? I try to give as much informations as possible. The three existing files are called file1 file2 and file3 the new file should named output_combined. The size of the files will be around 900 words/lines each .. but always... (5 Replies)
Discussion started by: MyMemberName
5 Replies

4. Shell Programming and Scripting

Output all data of files into one file

I have three files (can be more than 3): cat Katty => d4r4ff5rf5 123.44.32.4 =>cat Manu ffrs44frfrf 123.33.44.3 =>cat Chris derfe5rgrg 134.4.55.4 So basically, these files are in /Users/Unknown/files/*. Files can be... (11 Replies)
Discussion started by: Manu1234567
11 Replies

5. Windows & DOS: Issues & Discussions

Issue: Unzipping file containing files/folders with a similar name

Hi, I have a zip file created on a Linxux server that I need to extract on a Windows machine... The zip file containing folders with the same name but they each have a different case, one if camel case and the other is just capitalised. When I extract using 7zip, I get prompted if I want to... (3 Replies)
Discussion started by: muay_tb
3 Replies

6. UNIX for Dummies Questions & Answers

Large file data handling issue

I have a single record large file, semicolon ';' and pipe '|' separated. I am doing a vi on the file. It is throwing an error "File to long" I need to actually remove the last | symbol from this file. sed -e 's/\|*$//' filename is working fine for small files. But not working on this big... (13 Replies)
Discussion started by: Gurkamal83
13 Replies

7. Shell Programming and Scripting

Split a file into several files using a data

Hi All, I have file(File1) with data like below: 102100|LName|Gender|Company|Branch|Bday|Salary|Age 102100|bbbb|male|cccc|dddd|19900814|15000|20| 102101|asdg|male|gggg|ksgu|19911216||| 102102|bdbm|male|kkkk|acke|19931018||23| 102102|kfjg|male|kkkc|gkgg|19921213|14000|24|... (2 Replies)
Discussion started by: sarav.shan
2 Replies

8. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

9. Shell Programming and Scripting

Data Splitting into two files from one file

I have a file as: I/P File: Ground Car 2009 Lib 2008 Lib 2003 Ground Car 2009 Ground Car 2003 Car 2005 Car 2003 Car 2005 Sita 2900 2006 Car 2007 I have to split the file into two: - one for names and second for years. O/p1 (Names): Ground Car (3 Replies)
Discussion started by: karumudi7
3 Replies

10. Shell Programming and Scripting

Issue faced while accessing data from files on different servers.

I have two log files of same name and structure which resides on different servers with ssh-keygen installed. I want to search for a list of strings available in an array on last 15 minutes logs of both files and calculate the total count of occurence of each string. Sample format of both file... (0 Replies)
Discussion started by: poweroflinux
0 Replies
Login or Register to Ask a Question