Compare two files and print based on common variable value.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Compare two files and print based on common variable value.
# 1  
Compare two files and print based on common variable value.

Hi All,

i have below two files.

FILE:

Code:
NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="" MOUNTPOINT=""
NAME="/dev/sda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT="/boot"
NAME="/dev/sda2" TYPE="part" SIZE="29.5G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT=""
NAME="/dev/mapper/centos-root" TYPE="lvm" SIZE="56.5G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda2" MOUNTPOINT="/"
NAME="/dev/mapper/centos-swap" TYPE="lvm" SIZE="3G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda2" MOUNTPOINT="[SWAP]"
NAME="/dev/sda3" TYPE="part" SIZE="30G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT=""
NAME="/dev/mapper/centos-root" TYPE="lvm" SIZE="56.5G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda3" MOUNTPOINT="/"
NAME="/dev/sr0" TYPE="rom" SIZE="603M" OWNER="root" GROUP="cdrom" MODE="brw-rw----" PKNAME="" MOUNTPOINT=""

FILE1:

Code:
 Filesystem Size Used Avail Use% Mounted
 Filesystem Size Used Avail Use% Mounted
 devtmpfs 1.9G 0 1.9G 0% /dev
 tmpfs 1.9G 0 1.9G 0% /dev/shm
 tmpfs 1.9G 191M 1.7G 11% /run
 tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
 /dev/mapper/centos-root 57G 48G 9.4G 84% /
 /dev/sda1 497M 217M 281M 44% /boot
 tmpfs 379M 0 379M 0% /run/user/0

i want to compare both FILE AND FILE1 based on NAME and Filesystem column values.
if both are equal i want to combine both lines and print the entire ouput in one line

Desired output :

Code:
/dev/mapper/centos-root 57G 48G 9.4G 84% /  NAME="/dev/mapper/centos-root" TYPE="lvm" SIZE="56.5G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda2" MOUNTPOINT="/"


tried below script but it's giving error.

Code:
#!/bin/bash
FILE=/tmp/d5
FILE1=/tmp/d4
while read LINE;
do    . <(echo $LINE)
        if [ "$MOUNTPOINT" != "" ]

mounted=$MOUNTPOINT

file_system=$NAME
type=$TYPE


while read LINE1;
do    . <(echo $LINE1)

File_System=$Filesystem
if [ "$MOUNTPOINT" == "$Filesystem" ];then

echo -e "mounted:$MOUNTPOINT type:$type file_system:$Filesystem avail:$Avail \n"

done < FILE1

fi
fi
done < $FILE

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-31-2019 at 03:48 PM.. Reason: code tags, please!
# 2  
how about:
Code:
awk 'FNR==NR{f1[$2]=$0;next} $1 in f1 {print $0, f1[$1]}' FS='"' FILE FS=' ' FILE1

# 3  
What errors do you get? You're applying the logics designed for "FILE" and depending on its structure and wellbehavedness to a totally different data set. That can't fly. Try instead
Code:
awk 'FNR==NR {DF["\"" $1 "\""] = $0; next} $2 in DF {print DF[$2], $0}' file1 FS="[ =]" file2

# 4  
Hi All,

Can someone explain the meaning of the below statement.

awk 'FNR==NR{f1[$2]=$0;next} $1 in f1 {print $0, f1[$1]}' FS='"' FILE FS=' ' FILE1
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #778
Difficulty: Medium
Harmonic analysis is a branch of mathematics concerned with the representation of functions or signals as the superposition of basic waves.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge files based on both common and uncommon rows

Hi, I have two files A (2190 rows) and file B (1100 rows). I want to merge the contents of two files based on common field, also I need the unmatched rows from file A file A: ABC XYZ PQR file B: >LMN|chr1:11000-12456: >ABC|chr15:176578-187678: >PQR|chr3:14567-15866: output... (3 Replies)
Discussion started by: Diya123
3 Replies

2. UNIX for Dummies Questions & Answers

How to join 2 .txt files based on a common column?

Hi all, I'm trying to join two .txt file tab delimitated based on a common column. File 1 transcript_id gene_id length effective_length expected_count TPM FPKM IsoPct comp1000201_c0_seq1 comp1000201_c0 337 183.51 0.00 0.00 0.00 0.00 comp1000297_c0_seq1 ... (1 Reply)
Discussion started by: alisrpp
1 Replies

3. Shell Programming and Scripting

Help on writing data from 2 different files to one based on a common factor

Hello all, I have 2 text files. For example: File1.txt contains data A B C D ****NEXT**** X Y Z ****NEXT**** L M N and File2.txt contains data (13 Replies)
Discussion started by: vat1kor
13 Replies

4. Shell Programming and Scripting

Compare multiple files, and extract items that are common to ALL files only

I have this code awk 'NR==FNR{a=$1;next} a' file1 file2 which does what I need it to do, but for only two files. I want to make it so that I can have multiple files (for example 30) and the code will return only the items that are in every single one of those files and ignore the ones... (7 Replies)
Discussion started by: castrojc
7 Replies

5. Shell Programming and Scripting

Compare two sample files and find common

Hi I have two sample files attached here one file contain entries in one column and second file contains entries in many columns I have to match entries of first file with entries in secon d file form secon column onwards and if matches write "match" in front of it. I tried several... (11 Replies)
Discussion started by: manigrover
11 Replies

6. Shell Programming and Scripting

common entries between files based on 1st column

Hi, I am trying to get the common entries from 2 files based on 1st field.. However when I try to do in perl I am getting blank output.. How can I do this in awk? open(BUFF1, "my_genes"); open(BUFF3, "rawcounts"); #open(WRBUFF,">result_rawcounts"); while($line =<BUFF1>) { ... (3 Replies)
Discussion started by: Diya123
3 Replies

7. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

8. UNIX for Dummies Questions & Answers

compare two files based on common field in unix

I have two files in UNIX. 1st file is Entity and Second File is References. 1st File has only one column named Entity ID and 2nd file has two columns Entity ID | Person ID. I want to produce a output file where entity id's are matching in both the files. Entity File 624197 624252 624264... (4 Replies)
Discussion started by: PRS
4 Replies

9. Shell Programming and Scripting

join files based on a common field

Hi experts, Would you please help me with this? I have several files and I need to join the forth field of them based on the common first field. here's an example... first file: 280346 39.88 -75.08 547.8 280690 39.23 -74.83 538.7 280729 40.83 -75.08 499.2 280907 40.9 -74.4 507.8... (5 Replies)
Discussion started by: GoldenFire
5 Replies

10. Shell Programming and Scripting

Merging 2 files based on a common column

Hi All, I do have 2 files file 1 has 4 tab delimited columns 234 a c dfgyu 294 b g fih 302 c h jzh 328 z c san 597 f g son File 2 has 2 tab delimted columns 234 23 302 24 597 24 I want to merge file 2 with file 1 based on the data common in both files which is the first column so... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

Featured Tech Videos