Bashing of 2 Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bashing of 2 Files
# 1  
Old 06-07-2014
Bashing of 2 Files

Hi Everyone,

Seeking for your assistance on how to bash the file 1 to file 2 records and output the same records with specific field to display to file3.

Example: Imagine we have many Files and records in File 1 and the File2 is only 1 file but many records.

File1
Code:
A<tab>B<tab>C<tab>D<tab>E

File2
Code:
E<tab>F<tab>C<tab>G<tab>H

Expected Output:
Code:
A<tab>B<tab>C

Since the 3rd field "C" is the same in file 1 and file 2 it will output only the 3 fields into File3

I'm trying to use the awk to display the 1,2,3 Field but i don't know how to bash the file1 and file2 i think it will require loop

Code:
awk -F "\t" '{print $1}'
awk -F "\t" '{print $2}'
awk -F "\t" '{print $3}'

Please advise,

Thanks,
-nik
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input and output as well as code.

Last edited by Don Cragun; 06-07-2014 at 06:43 AM.. Reason: Add CODE tags.
# 2  
Old 06-07-2014
Are you looking for a bash solution or an awk solution? The standard approach with awk is to use a NR==FNR construct. You can use that as a search term on these forums. You will find many examples..
# 3  
Old 06-07-2014
Quote:
Originally Posted by znesotomayor
Hi Everyone,

Seeking for your assistance on how to bash the file 1 to file 2 records and output the same records with specific field to display to file3.

Example: Imagine we have many Files and records in File 1 and the File2 is only 1 file but many records.

File1
Code:
A<tab>B<tab>C<tab>D<tab>E

File2
Code:
E<tab>F<tab>C<tab>G<tab>H

Expected Output:
Code:
A<tab>B<tab>C

Since the 3rd field "C" is the same in file 1 and file 2 it will output only the 3 fields into File3

I'm trying to use the awk to display the 1,2,3 Field but i don't know how to bash the file1 and file2 i think it will require loop

Code:
awk -F "\t" '{print $1}'
awk -F "\t" '{print $2}'
awk -F "\t" '{print $3}'

Please advise,

Thanks,
-nik
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input and output as well as code.
Is this a homework assignment?

If not, what do you mean by the text marked in red above? You have shown us two sample input files that each only contain 1 line (or record). Obviously a file can contain many lines. But for a file to contain many files, that file is either a directory or an archive file of some type. You need to much more clearly explain what you mean by this and give us a much better example.

If there are multiple lines in files 1 and 2, are lines from file 1 supposed to be copied to file3 if the 3rd field on any line in file 2 matches the 3rd field on a line in file1? Or is a line from file 1 only supposed to be copied to file3 if the 3rd field on the line in file2 matches the 3rd field for the corresponding line (for example, line 1 in both files or line 5 in both files) in file 1?
# 4  
Old 06-07-2014
Sorry Moderator T_T.

Hi Sir Scrutinizer,

Sorry i'm still a newbie, but can you give me sample of NR==FNR?

Please advise,

Thanks,

---------- Post updated at 06:07 PM ---------- Previous update was at 06:01 PM ----------

Sorry Sir Don Cragun.

Sorry Sir It's not an Assignment. I just don't have any idea on how to solve it. Thanks Everyone for the support.

BR,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. Shell Programming and Scripting

Bashing of 2 files

Hi All, Seeking for your assistance on how to bash 2 files and then print if the condition met. Ex. file1.txt: Field1 Field2 Field 3 Field 4 usa <blank> <blank> INDIA file2.txt: Field1 Field2 Field 3 Field 4 canada jap INDIA utah... (7 Replies)
Discussion started by: znesotomayor
7 Replies

3. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

4. UNIX for Dummies Questions & Answers

Bashing of records in different file

Hi Everyone, Good Day, I'm a newbie on scripting, I would like to seek you for assistance regarding on how to bash the 1st file into 2nd file and output the match record. Sample: File1.csv JuanDelaCrus<tab>USA<tab>CANADA TwoDelaCrus<tab>SG<tab>California File2.csv... (2 Replies)
Discussion started by: znesotomayor
2 Replies

5. UNIX for Dummies Questions & Answers

Bashing of Fields then append!

Hi All, I;m a newbie here, Seeking for your assistance regarding on how to bash the 6th and 7th fields of file2 into 2th and 3rd fields of file1 then add fields in the last line of file2 if it's match, if not they will only display the records. Ex. File1 a,1,2,b,c,d,e,USA... (5 Replies)
Discussion started by: nikka
5 Replies

6. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

7. UNIX for Dummies Questions & Answers

write a program in c in unix that display the files(includ sub-direc and files within) in a sorted

the sorting is based on name of file, file size modification time stamps o f file it should dislay the output in the following format "." and ".." enteries should be ignored please give some idea how to do it (1 Reply)
Discussion started by: pappu kumar jha
1 Replies

8. Shell Programming and Scripting

How to extract data from indexed files (ISAM files) maintained in an unix server.

Hi, Could someone please assist on a quick way of How to extract data from indexed files (ISAM files) maintained in an UNIX(AIX) server.The file data needs to be extracted in flat text file or CSV or excel format . Usually we have programs in microfocus COBOL to extract data, but would like... (2 Replies)
Discussion started by: devina
2 Replies

9. Shell Programming and Scripting

How to retrieve all the linked script files/ctl files/sql files?

Hi I am going to migrate our datawarehouse system from HP Tru 64 Unix to the Red Hat Linux. Inside the box, it is running around 40 cron jobs; inside each cron job, it is calling other shell script files, and the shell script files may again call other shell script files or ctl files(for... (1 Reply)
Discussion started by: franksubramania
1 Replies

10. UNIX for Dummies Questions & Answers

All right. ... (no Microsoft bashing, thanks)

First off, this is as much as I know about Unix: its an operating system. Now, I know it goes a tad deeper than that, so first off, what exactly is Unix? Next, im freakin sick of everything microsoft crashing on me, so Id like to make Unix my OS, but I dont want to lose anything but internet... (5 Replies)
Discussion started by: gc_wyo_dave
5 Replies
Login or Register to Ask a Question