Bashing of 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bashing of 2 files
# 1  
Old 03-01-2016
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:
Code:
Field1   Field2     Field 3        Field 4
usa      <blank>  <blank>     INDIA

file2.txt:
Code:
Field1     Field2     Field 3        Field 4
canada    jap        INDIA         utah

Condition
If Field 4 of file1.txt = Field 3 of file2.txt, the value of Field 1(canada) and Field 2(jap) of file2.txt will be put on Field 2 and Field 3 of file1.txt

Expected Output in file1.txt:
Code:
Field1   Field2     Field 3        Field 4
usa      canada    jap             INDIA

I used the code below:

Code:
awk 'NR==FNR{a[$1];next}$1 in a{print $1, $2}' file1.txt file2.txt

it will print the common string but i don't know how to put the output in file1.txt

Thanks,
Moderator's Comments:
Mod Comment Using CODE tags correctly doesn't mean putting everything but "Thanks," in CODE tags.

Please put CODE tags around sample input, sample output, and code segments.

Last edited by Don Cragun; 03-01-2016 at 05:07 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 03-01-2016
Hello znesotomayor,

I am not sure about <blank> <blank> is really like a string into Input_file or it is a space. If <blank> <blank> is a string into file1.txt then following may help you in same.
Code:
awk 'FNR==NR && FNR>1{A[$4]=$2 OFS $3;next} ($3 in A){print $1 OFS $2 OFS A[$3]}' file1.txt file2.txt

Output will be as follows.
Code:
canada jap <blank> <blank>

Thanks,
R. Singh
# 3  
Old 03-01-2016
Hi Sir RavinderSingh13,

<blank> meaning empty string in Field 2 and Field 3 of file1.txt.

The expected output is:
Code:
cat file1.txt
Field1   Field2     Field 3        Field 4
usa      canada    jap             INDIA

Thanks
# 4  
Old 03-01-2016
Hello znesotomayor,

Could you please try following and let me know if this helps you.
Code:
awk 'NR==1{;print}FNR==NR && FNR>1{A[$3]=$1 OFS $2;next} ($2 in A){print $1 OFS A[$2] OFS $2}' OFS="\t" file2.txt file1.txt

Output will be as follows.
Code:
Field1     Field2     Field 3        Field 4
usa     canada  jap     INDIA

Where Input_filefile1.txt looks as follows.
Code:
cat file1.txt
Field1   Field2     Field 3        Field 4
usa               INDIA

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 03-01-2016
Quote:
Originally Posted by znesotomayor
Hi All,

Seeking for your assistance on how to bash 2 files and then print if the condition met.

Ex.
file1.txt:
Code:
Field1   Field2     Field 3        Field 4
usa      <blank>  <blank>     INDIA

file2.txt:
Code:
Field1     Field2     Field 3        Field 4
canada    jap        INDIA         utah

Condition
If Field 4 of file1.txt = Field 3 of file2.txt, the value of Field 1(canada) and Field 2(jap) of file2.txt will be put on Field 2 and Field 3 of file1.txt

Expected Output in file1.txt:
Code:
Field1   Field2     Field 3        Field 4
usa      canada    jap             INDIA

I used the code below:

Code:
awk 'NR==FNR{a[$1];next}$1 in a{print $1, $2}' file1.txt file2.txt

it will print the common string but i don't know how to put the output in file1.txt

Thanks,
Moderator's Comments:
Mod Comment Using CODE tags correctly doesn't mean putting everything but "Thanks," in CODE tags.

Please put CODE tags around sample input, sample output, and code segments.
Given that fields are separated by an arbitrary number of <space> characters and fields are not aligned, how do you determine that fields 2 and 3 in file1.txt are blank rather than fields 3 and 4 or 2 and 4?
# 6  
Old 03-01-2016
Hi Sir Don,

If it's empty character or tab delimeted. hmm.. Is it possible to put the field 1 and field 2 of file 2 into field 6 and 7? all i want is to put the data of field 1 and field 2 of file2.txt on file1.txt if Field 4 of file1.txt = Field 3 of file2.txt

Please advise,

Thanks,

---------- Post updated at 05:44 PM ---------- Previous update was at 05:33 PM ----------

Hi Sir RavinderSingh13,

Your output is same with file2.txt
Code:
canada    jap        INDIA         utah

Thanks,

---------- Post updated at 06:18 PM ---------- Previous update was at 05:44 PM ----------

Any suggestion please. Thank you for the help.
# 7  
Old 03-02-2016
Quote:
Originally Posted by znesotomayor
Hi Sir Don,

If it's empty character or tab delimeted. hmm.. Is it possible to put the field 1 and field 2 of file 2 into field 6 and 7? all i want is to put the data of field 1 and field 2 of file2.txt on file1.txt if Field 4 of file1.txt = Field 3 of file2.txt

Please advise,

Thanks,
From your first post in this thread:
Quote:
Condition
If Field 4 of file1.txt = Field 3 of file2.txt, the value of Field 1(canada) and Field 2(jap) of file2.txt will be put on Field 2 and Field 3 of file1.txt
To meet your condition, we first have to be able to identify what is in Field 4 of file1.txt. And, when your field separator is a variable number of spaces, there is no way to determine whether the 2nd line in your sample file1.txt contains fields 1 and 2, 1 and 3, 1 and 4, 2 and 3, 2 and 4, or 3 and 4. With its default field separator, the awk utility will assume that that line contains fields 1 and 2 and that fields 3 and 4 are empty strings.

If your fields each contained a fixed number of characters, we could identify fields by character counts; but the data in your sample files does not line up with the headers, so we can't do that.

If your fields used a single <tab> character as a field separator instead of a seemingly random number of <space> characters, we could identify field boundaries easily; but the data in your sample files does not use <tab> characters; it uses a seeming random number of <space> characters.

I repeat: With no way to determine field boundaries in your input data when some fields are empty, there is no way to determine what is in field 4. So if you want to make decisions based on what is in field 4, you are out of luck.

Come up with an unambiguous input file format (and make sure your input files adhere to that format), tell us what that unambiguous input file format is, and show us sample input files in that format and the output you are trying to produce in that format and we may be able to help you come up with a reliable solution to your problem. If you can't do that, there isn't much we can do to help.
This User Gave Thanks to Don Cragun For This Post:
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

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

3. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: znesotomayor
3 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