Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Extract the same lines from the two files Post 302919717 by drl on Friday 3rd of October 2014 10:51:50 AM
Old 10-03-2014
Hi.

There are utilities that specialize in these kinds of operations, such as combine from moreutils, a package in the Debian repositories, as well as being available via git. Here's an example:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate combine files with boolean operators, package "moreutils".
# combine - combine sets of lines from two files using boolean operations
# For combine, part of moreutils, see:
# http://joeyh.name/code/moreutils/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C specimen combine

pl " Input data files:"
specimen data?

pl " Results, combine data1 AND data2:"
combine data1 and data2

pl " Results, combine data2 AND data1:"
combine data2 and data1

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
specimen (local) 1.17
combine (pm:moreutils) 0.31

-----
 Input data files:
Edges: 5:0:5 of 378 lines in file "data1"
AB029895
AF208401
AF309648
AF526378
AJ444445
   ---
XM_429498
XR_211629
XR_212326
Z97238
Z97296

Edges: 5:0:5 of 820 lines in file "data2"
NM_001128101
NM_205517
NM_204411
AB909129
NM_001205066
   ---
M30369
M30368
GALGA25_CL10
GALGA25_Ktn6
GALGA25_Ktn9

-----
 Results, combine data1 AND data2:
NM_001277927
NM_001277933

-----
 Results, combine data2 AND data1:
NM_001277933
NM_001277927

The documentation explains that for combine, a and b is not always the same as b and a, but they are in this case.

See comments in the script for appropriate links.

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

is it hard to extract particular lines & strings from the files??

Hi Experts, I have lots of big size files. Below is the snapshot of a file. From the files i want extract informmation like belows. What could be command or script for that? DELETE RESP:940120105 CREATE RESP:0 GET RESP:0 File contains like below- ... ... <log... (8 Replies)
Discussion started by: thepurple
8 Replies

2. Shell Programming and Scripting

Extract lines from files

hi all, I have three files. The first file (FILE_INFO in my code) consists of four parameters for each line. 0.00765600 0.08450704 M3 E3 0.00441931 0.04878049 M4 E5 0.01904574 0.21022727 M5 E10 0.00510400 0.05633803 M6 E12 0.00905960 ... (11 Replies)
Discussion started by: my_Perl
11 Replies

3. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

4. Shell Programming and Scripting

How to extract lines between tags into different files?

I have an xml file with the below data: unix>Cat address.xml <Address City=”Amsterdam” Street = “station straat” ZIPCODE="2516 CK " </Address> <Address City=”Amsterdam” Street = “Leeuwen straat” ZIPCODE="2517 AB " </Address> <Address City=”The Hauge” Street = “kirk straat” ... (1 Reply)
Discussion started by: LinuxLearner
1 Replies

5. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

6. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

7. Shell Programming and Scripting

Can you extract (remove) lines from log files?

I use "MineOS" (a linux distro with python scripts and web ui included for managing a Minecraft Server). The author of the scripts is currently having a problem with the Minecraft server log file being spammed with certain entries. He's working on clearing up the spam. But in the meantime, I'm... (8 Replies)
Discussion started by: nbsparks
8 Replies

8. Shell Programming and Scripting

Extract lines from text files

I have some files containing the following data # RESIDUE AA STRUCTURE BP1 BP2 ACC N-H-->O O-->H-N N-H-->O O-->H-N TCO KAPPA ALPHA PHI PSI X-CA Y-CA Z-CA 1 196 A M 0 0 230 0, 0.0 2,-0.2 0, 0.0 0, 0.0 0.000 360.0 360.0 360.0 76.4 21.7 -6.8 11.3 2 197 A D + 0 0 175 1,-0.1 2,-0.1 0, 0.0 0, 0.0... (10 Replies)
Discussion started by: edweena
10 Replies

9. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

10. Shell Programming and Scripting

Extract lines that appear twice

I have a text file that looks like this : root/user/usr1/0001/abab1* root/user/usr1/0001/abab2* root/user/usr1/0002/acac1* root/user/usr1/0002/acac2* root/user/usr1/0003/adad1* root/user/usr1/0004/aeae1* root/user/usr1/0004/aeae2* How could I code this to extract just the subjects... (9 Replies)
Discussion started by: LeftoverStew
9 Replies
fmemcmpi(3)						     Library Functions Manual						       fmemcmpi(3)

NAME
fmemcmpi - fuzzy comparison of two memory areas SYNOPSIS
#include <fstrcmp.h> #define FSTRCMPI_IDENTICAL #define FSTRCMPI_THRESHOLD #define FSTRCMPI_ERROR int fmemcmpi(const void *data1, size_t size1, const void *data2, size_t size2); DESCRIPTION
The fmemcmpi() function compares the two memory areas, the size1 bytes pointed to by data1 and the size2 bytes pointed to by data2. RETURN VALUE
The fmemcmpi function returns an int value between 0 and FSTRCMPI_IDENTICAL. A value of 0 means the memory areas are utterly un-alike. A value of FSTRCMPI_IDENTICAL means the memory areas are identical. A value of more than FSTRCMPI_THRESHOLD (it lies between 0.0 and FSTR- CMP_IDENTICAL) would be considered "similar" by most people. A value of FSTRCMPI_ERROR (always negative) indicates a malloc(3) failure. SEE ALSO
fmemcmp(3) fuzzy comparison of two memory areas fstrcmpi(3) fuzzy comparison of two strings memcmp(3) compare memory areas COPYRIGHT
fstrcmp version 0.4 Copyright (C) 2009 Peter Miller Peter Miller <pmiller@opensource.org.au> The comparison code is derived from the fuzzy comparison functions in GNU Gettext 0.17. The GNU Gettext comparison functions were, in turn, derived from GNU Diff 2.7. Copyright (C) 1988-2009 Free Software Foundation fmemcmpi(3)
All times are GMT -4. The time now is 10:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy