简体   繁体   中英

how to retreive lines between specific words by given two files?

I have two files 1 and 2. file 1 contains all the detail information of metabolic pathways that start with C and D and its contain large number of C and D while files 2 contains only specif ID line with its name that start with C and have unique (shortlisted C, less in number). files look like this:

File 1:

C    00010 Glycolysis / Gluconeogenesis [PATH:smup00010]
D      SMPSPU_277 pfkA; 6-phosphofructokinase   K00850 pfkA; 6-phosphofructokinase 1 [EC:2.7.1.11]
D      SMPSPU_278 gapA; glyceraldehyde 3-phosphate dehydrogenase        K00134 GAPDH; glyceraldehyde 3-phosphate dehydrogenase [EC:1.2.1.12]
D      SMPSPU_274 acoA; pyruvate dehydrogenase E1 component subunit 
alpha       K00161 PDHA; pyruvate dehydrogenase E1 component alpha subunit 
[EC:1.2.4.1]
D      SMPSPU_172 korA; 2-oxoglutarate ferredoxin oxidoreductase subunit alpha  K00174 korA; 2-oxoglutarate/2-oxoacid ferredoxin oxidoreductase subunit alpha [EC:1.2.7.3 1.2.7.11]
D      SMPSPU_061 korB; 2-oxoglutarate ferredoxin oxidoreductase subunit beta   K00175 korB; 2-oxoglutarate/2-oxoacid ferredoxin oxidoreductase subunit beta [EC:1.2.7.3 1.2.7.11]
C    00020 Citrate cycle (TCA cycle) [PATH:smup00020]
D      SMPSPU_201 sucA; 2-oxoglutarate dehydrogenase, E1 component      K00164 OGDH; 2-oxoglutarate dehydrogenase E1 component [EC:1.2.4.2]
D      SMPSPU_120 lpdA; dihydrolipoamide dehydrogenase  K00382 DLD; dihydrolipoamide dehydrogenase [EC:1.8.1.4]
D      SMPSPU_172 korA; 2-oxoglutarate ferredoxin oxidoreductase subunit alpha  K00174 korA; 2-oxoglutarate/2-oxoacid ferredoxin oxidoreductase subunit alpha [EC:1.2.7.3 1.2.7.11]
D      SMPSPU_169 sucD; succinyl-CoA synthetase subunit alpha   K01902 sucD; succinyl-CoA synthetase alpha subunit [EC:6.2.1.5]
D      SMPSPU_229 pdhB; pyruvate dehydrogenase E1 component subunit beta        K00162 PDHB; pyruvate dehydrogenase E1 component beta subunit [EC:1.2.4.1]
D      SMPSPU_275 pdhC; dihydrolipoamide acyltransferase E2 component   K00627 DLAT; pyruvate dehydrogenase E2 component (dihydrolipoamide acetyltransferase) [EC:2.3.1.12]
C    00030 Pentose phosphate pathway [PATH:smup00030]
D      SMPSPU_057 tktB; transketolase, N-terminal subunit       K00615 E2.2.1.1; transketolase [EC:2.2.1.1]
D      SMPSPU_058 tktA; transketolase, C-terminal subunit       K00615 E2.2.1.1; transketolase [EC:2.2.1.1]
C    00051 Fructose and mannose metabolism [PATH:smup00051]
D      SMPSPU_277 pfkA; 6-phosphofructokinase   K00850 pfkA; 6-phosphofructokinase 1 [EC:2.7.1.11]
D      SMPSPU_230 fbaA; fructose-bisphosphate aldolase  K01624 FBA; fructose-bisphosphate aldolase, class II [EC:4.1.2.13]

file 2:

C    00261 Monobactam biosynthesis [PATH:smup00261]
C    00300 Lysine biosynthesis [PATH:smup00300]
C    00660 C5-Branched dibasic acid metabolism [PATH:smup00660]
C    00680 Methane metabolism [PATH:smup00680]
C    02020 Two-component system [PATH:smup02020]
C    02024 Quorum sensing [PATH:smup02024]

Now I want to extract only those C and their respective D which are present in file 2.

I tried this script

fgrep -f name-C-non-homowba00001 wba00001.keg |grep -E '^C.*PATH|^D' | less

but i give me this C id and name files.

Try this:

cat input | grep -E '^[CD]' | sed -n '/^C.*PATH/,/^C/p' | uniq -f2 | grep -E '^C.*PATH|^D'

where:

  • input is your file
  • first grep prints all lines beginning with C or D
  • sed prints all lines from one beginning with C and containing PATH to next one beginning with C (included)
  • uniq suppresses all adjacent lines that are equal, except for the first 2 fields
  • last grep prints all lines beginning with C and containing PATH or beginning with D
awk '$1!~/^D$/ { select=0; } $1=="C" && $NF~/PATH/ { select=1; } {if(select) print; }' inputfile

Explanation:

$1;~/^D$/ { select=0; } $1;~/^D$/ { select=0; } A line other than D stops output.
$1=="C" && $NF~/PATH/ { select=1; } $1=="C" && $NF~/PATH/ { select=1; } A C line that contains PATH in the last field starts output.
{if(select) print; } {if(select) print; } Print current line if selected for output.

This is a safe method:

awk '(NR==FNR){a[$0];next}/^C/{p=($0 in a)}p' file2 file1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM