File comparisons using awk: Match columns
File3
a c e
File4
a 1 b 2 c 3 d 4 e 5
the one liner for comparing the first field of file4 with the first field of file3 is:
awk 'FNR==NR{a[0ドル];next}(1ドル in a)' file3 file4
and the output is:
a 1 c 3 e 5
And if you want to remove the lines which match just change the above mentioned command by adding a !
awk 'FNR==NR{a[0ドル];next}!(1ドル in a)' file3 file4
Subscribe to:
Post Comments (Atom)
Popular Posts
About The Author
I am a software programmer working in India. I usually like to blog in my free time....Read More
Labels
- array
- auto
- awk
- bash
- books
- c
- c++
- compare
- copy
- cpu
- cut
- Data Structures
- dbx
- debugger
- dtrace
- explicit
- expr
- find
- fork
- ftp
- gdb
- grep
- head
- interview-question
- isql
- java
- join
- malloc
- match
- mdb
- memory
- memory leaks
- mv
- nawk
- One liners
- paste
- pattern match
- perl
- perl script
- perl-hashes
- perldoc
- plsql
- popen
- process
- ps
- regex
- rename
- replace
- reverse
- scp
- search
- sed
- shell
- shell variable
- shell-script
- shell-scripting
- solaris
- sort
- split
- sql
- stl
- string
- sybase
- table
- tail
- tar
- truss
- unix
- vector
- vi
- vim
- xargs
Search This Blog
Blog Archive
-
2013
(50)
- November (1)
- September (2)
- July (2)
- June (4)
- May (8)
- April (4)
- March (4)
- February (4)
- January (21)
-
2012
(50)
-
December
(50)
- Interesting perl One-liner
- Return type of malloc
- Moving a process from background to foreground
- Join every two lines in a file
- Compare two files and replace
- Print lines after every n lines in a file
- Delete duplicate lines in a file in Unix
- Delete comments from a C/C++ source file
- Searching multiple strings in VI editor
- Understanding C++ polymorphism
- Archiving files in a directory - tar command
- Changing the extension of large number of files
- Reverse a list of numbers stored in a variable
- Using The Perl Debugger
- Using awk to only print the matching lines in two ...
- Using awk to delete lines from a file which are pr...
- File comparisons using awk: Match columns
- AWK one-liner for multi-column comparision two uns...
- Print lines between two patterns in a file
- Using Perl's -F flag
- Ftp using perl and getting the files based upon a ...
- Print every character in a new line
- Extracting Range of lines in perl
- Print limited characters per a line
- Find and replace a string in all the files recursi...
- secure copy in a shell script
- Change format of a field using awk
- Join lines based on a pattern
- Join lines based upon the first field
- Join consequent lines in unix
- Check for perl modules installed
- Using Perl File::Find
- Executing Sql query in a Perl script
- Compare two files in Perl
- Sort unix processes by memory usage
- Shell Script for taking backup
- Sort file on columns and characters
- Truss using a process id
- Breaking an infinite loop in a shell script
- Removing duplicates from a line
- Split a line into 5 fields each into multiple lines
- Printing lines after a match is found
- Add the contents of one file into other based upon...
- sort contents of a file based upon the fields
- Add line just before the first match
- Count the number of occurrences of each letter by row
- Delete lines from a file
- extract the contents in between the tags
- extract the contents in between the tags
- Comparing two files using awk
-
December
(50)
Powered by Blogger.
Pages
Gallery
[フレーム]Contact Us
Popular Posts
-
I have two files File 1 contains 3 fields File 2 contains 4 fields The number of rows of File 1 is much smaller than that of File 2 I...
-
Normally while working on PL/SQL stored procedures , we use DBMS_TRACE for knowing about the values of various variables and put some print...
-
At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. Th...
-
Below is a most simple command for Sorting Unix processes by highest memory usage: ps -eo pmem,pid,pcpu,rss,vsz,time,args | sort -k 1 -r ...
-
This question attempts to collect the few pearls among the dozens of bad C++ books that are released every year.Unlike many other programmin...
-
There are lot of ways where one can identify memory leaks and lots of blogs and materieals are available to help a designer to identify the...
-
This awk one-liner works for multi-column on unsorted files. Comparision is done based on 1st,2nd,3rd,4th of the first file and 1st,3rd,6th,...
-
File3 a c e File4 a 1 b 2 c 3 d 4 e 5 the one liner for comparing the first field of file4 with the first field of file3 is:...
-
In unix, if we run a process(a shell script).It will not return to the terminal untill the script ends. lets say there is a script temp.sh ...
-
Most of the time it is required that w eneed to rename the files in bulk. this can be done in many ways,Mostly people do it by writing a s...
3 comments:
Can you please explain how it is working ?
Reply Deleteawk 'FNR==NR{a[0ドル];next}(1ドル in a)' file3 file4
Reply DeleteFNR->line number of the file.
NR->line number of all collected data of all the files.
So the first thing is:
FNR==NR->this condition will be a succes untill all the lines in the first file are completed processing.As soon as all the lines in the file3 are completed,FNR will be set back to 1 and NR will continue with its numbering.
So untill this condition satisfies the array a keeps on building with 0ドル(which is the complete line of file3 here).So at the end of file3 the array has all the lines of file3.
next is like continue in c language it will tell awk to start processing the next line.
The rest of the code (1ドル in a) will applied only after all the lines in file3 are completed(that is from first line of file4).1ドル represents the first field of file4.
(1ドル in a) will check whether ther is a 1ドル as a key in the array a.If success this will print the line
I want to cmpare two files columnwise in unix using shell script
Reply Deletefile1
datasrid BMStrid Mersionid country curr
Met_CCD V14121011081 Recent US USD
Met_CCD V14121011082 Recent US USD
Met_CCD V14121011083 Recent GB GDB
Met_CCD V14121011084 Recent IE GDB
Met_CCD V14121011085 Recent GB GDB
Met_CCD V14121011086 Recent AU AUD
Met_CCD V14121011086 Recent HK HKD
Met_CCD V14121011087 Recent IE GDB
file2
datasrid BMStrid Mersionid country curr
Met_CCD V14121011081 Recent US USD
Met_CCD V14121011082 Recent US USD
Met_CCD V14121011083 Recent GB GDB
Met_CCD V14121011088 Recent IE GDB
Met_CCD V14121011085 Recent HK GDB
Met_CCD V14121011086 Recent AU AUD
Met_CCD V14121011086 Recent HK HKD
Met_CCD V14121011087 Recent IE GDB
Outputfile
need to compare file2 wrt file1.
change in any cell should get highlighted in output file.
like
o/p file should contain
Met_CCD 'V14121011088' Recent IE GDB
Met_CCD V14121011085 Recent 'HK' GDB