Sponsored Content
Top Forums Shell Programming and Scripting awk to match field4 and field5, field6 and field7, field8 and field9 and so on. Post 302955871 by huiyee1 on Wednesday 23rd of September 2015 01:34:23 AM
Old 09-23-2015
Hi disedorgue and RudiC,

I need to add in one more condition another input file as follows,
if field4≠field5 and field4≠NN and field5≠NN, print NN

Input2:
Code:
loc01	1000560	G	AA	AG	GC	GA	AC	AG	NN	NN	NN	NN
loc02	1001612	C	NN	NN	CT	CC	NN	NN	NN	NN	CT	NN
loc03	1001797	C	AC	NN	NN	NN	NN	NN	NN	NN	AC	NN
loc04	1004204	G	NN	AG	NN	NN	NN	NN	AG	NN	AG	NN
loc05	1005484	C	NN	NN	CT	NN	NN	NN	NN	NN	NN	NN
loc06	1005486	C	CT	CG	CT	NN	NN	NN	NN	NN	NN	NN
loc07	1005499	G	NN	NN	AG	NN	NN	NN	NN	NN	NN	NN
loc08	100707	T	NN	NN	NN	NN	NN	NN	NN	NN	NN	NN
loc09	100937	C	NN	NN	NN	NN	NN	NN	CT	NN	NN	NN
loc10	100949	C	NN	NN	NN	NN	NN	NN	CT	NN	NN	NN
loc11	101063	T	NN	NN	NN	NN	NN	CT	NN	NN	CT	NN
loc12	1010912	G	AG	AG	AG	NN	AG	NN	AG	NN	NN	AG
loc13	1011214	A	NN	NN	NN	AG	NN	NN	NN	NN	AG	AG
loc14	1011673	T	NN	CT	CT	NN	CT	NN	CT	NN	CT	CT
loc15	1011981	A	NN	NN	NN	NN	NN	AG	NN	NN	NN	NN
loc16	1012439	C	NN	NN	NN	NN	NN	NN	NN	NN	NN	CT
loc17	1012718	T	NN	NN	CT	NN	CT	NN	CT	NN	CT	NN
loc18	1015524	A	NN	NN	NN	NN	AT	AT	AT	NN	AT	NN
loc19	1023408	C	CG	NN	NN	NN	NN	CG	NN	NN	NN	NN
loc20	102483	A	AA	CC	CC	CT	NN	NN	NN	NN	NN	NN

Output2:
Code:
loc01	1000560	G	NN	NN	NN	NN	NN
loc02	1001612	C	NN	NN	NN	NN	CT
loc03	1001797	C	AC	NN	NN	NN	AC
loc04	1004204	G	AG	NN	NN	AG	AG
loc05	1005484	C	NN	CT	NN	NN	NN
loc06	1005486	C	NN	CT	NN	NN	NN
loc07	1005499	G	NN	AG	NN	NN	NN
loc08	100707	T	NN	NN	NN	NN	NN
loc09	100937	C	NN	NN	NN	CT	NN
loc10	100949	C	NN	NN	NN	CT	NN
loc11	101063	T	NN	NN	CT	NN	CT
loc12	1010912	G	AG	AG	AG	AG	AG
loc13	1011214	A	NN	AG	NN	NN	AG
loc14	1011673	T	CT	CT	CT	CT	CT
loc15	1011981	A	NN	NN	AG	NN	NN
loc16	1012439	C	NN	NN	NN	NN	CT
loc17	1012718	T	NN	CT	CT	CT	CT
loc18	1015524	A	NN	NN	AT	AT	AT
loc19	1023408	C	CG	NN	CG	NN	NN
loc20	102483	A	NN	NN	NN	NN	NN

I tried to use the following script modified from disedorgue, but i failed.
Code:
 awk '{
printf("%s\t%s\t%s\t",$1,$2,$3)
for(i=4;i<=NF;i+=2){
   if($i==$(i+1)) new=$i;
   else if($i=="NN" || $(i+1)!="NN") new=$(i+1);
   else if($i!="NN" || $(i+1)=="NN") new=$i;
   else if($i!=$(i+1) || $i!="NN" || $(i+1)!="NN") new=NN;
   printf("%s\t",new)} print ""}' input2.txt

What is the mistake I have made?

---------- Post updated 09-23-15 at 12:34 AM ---------- Previous update was 09-22-15 at 08:50 PM ----------

Hi all,

I figured it out using the following script:
Code:
awk '{
printf("%s\t%s\t%s\t",$1,$2,$3)
for(i=4;i<=NF;i+=2){
   if($i==$(i+1)) new=$i;
   else if($i=="NN" && $(i+1)!="NN") new=$(i+1);
   else if($i!="NN" && $(i+1)=="NN") new=$i; 
   else if($i!=$(i+1) && $i!="NN" && $(i+1)!="NN") new="NN";
   printf("%s\t",new)} print ""}' input2.txt

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

dynamic match thru awk

hey , my i/p text looks like this, FILE_TYPE=01|FILE_DESC=Periodic|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=B FILE_TYPE=02|FILE_DESC=NCTO|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=M NOTE Look carefully for the position FILE_TYPE,FILE_DESC... (23 Replies)
Discussion started by: manas_ranjan
23 Replies

2. Shell Programming and Scripting

AWK match and print

I have thousands of tables compiled in a single txt document that I'm parsing with AWK. Scattered throughout the document in random sections I would like to parse out the sections that look like this: 1 Seq. Descrição do bem Tipo do bem Valor do bem (R$) 2 1 LOCALIZADO ANA RUA PESSEGO N 96... (3 Replies)
Discussion started by: daveyabe
3 Replies

3. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

4. Shell Programming and Scripting

Better way to match a list in awk

Suppose I have a list of strings in a file called stringlist... string1 string2 ... stringn Suppose also that I have another file, or stdin, or whatever, and I want to use awk to see if some field in each record matches any string in stringlist. What I've been doing is using each string... (3 Replies)
Discussion started by: treesloth
3 Replies

5. Shell Programming and Scripting

awk match help

Trying to match $1 of file2.txt with $1 of file 1.txt and output the entire line of the match. Thank you :) awk 'NR==FNR{A=$2; next} A {$2=$2 " " A}1' file1.txt file2.txt > output.txt file1.txt LMNA 285.195652 MZT1P1 166.852113 HFM1 129.847940 file2.txt LMNA PTPN11... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

Using awk for match and print

I have the need to match up the lat / lon from a fileA with the lat / lon and value from fileB. fileA is a small subset of fileB I have the following awk script but it prints out all the contents from fileB. I only need the matches. awk 'FNR==NR {A=$NF; next} {A=$NF} END{for(i in A) printf... (10 Replies)
Discussion started by: ncwxpanther
10 Replies

7. Shell Programming and Scripting

awk if match

Hi, This is the file content: #160814 20:43:00 server id 2 end_log_pos 169934694 Query thread_id=8927407 exec_time=0 error_code=0 use sun_final/*!*/; SET TIMESTAMP=1471207380/*!*/; DELETE FROM `top_pack` WHERE `top_pack`.`id` = 3023 Trying like:awk... (5 Replies)
Discussion started by: ashokvpp
5 Replies

8. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

9. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

10. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies
Locale::Codes::LangFam(3pm)				 Perl Programmers Reference Guide			       Locale::Codes::LangFam(3pm)

NAME
Locale::Codes::LangFam - standard codes for language extension identification SYNOPSIS
use Locale::Codes::LangFam; $lext = code2langfam('apa'); # $lext gets 'Apache languages' $code = langfam2code('Apache languages'); # $code gets 'apa' @codes = all_langfam_codes(); @names = all_langfam_names(); DESCRIPTION
The "Locale::Codes::LangFam" module provides access to standard codes used for identifying language families, such as those as defined in ISO 639-5. Most of the routines take an optional additional argument which specifies the code set to use. If not specified, the default ISO 639-5 language family codes will be used. SUPPORTED CODE SETS
There are several different code sets you can use for identifying language families. A code set may be specified using either a name, or a constant that is automatically exported by this module. For example, the two are equivalent: $lext = code2langfam('apa','alpha'); $lext = code2langfam('apa',LOCALE_LANGFAM_ALPHA); The codesets currently supported are: alpha This is the set of three-letter (lowercase) codes from ISO 639-5 such as 'apa' for Apache languages. This is the default code set. ROUTINES
code2langfam ( CODE [,CODESET] ) langfam2code ( NAME [,CODESET] ) langfam_code2code ( CODE ,CODESET ,CODESET2 ) all_langfam_codes ( [CODESET] ) all_langfam_names ( [CODESET] ) Locale::Codes::LangFam::rename_langfam ( CODE ,NEW_NAME [,CODESET] ) Locale::Codes::LangFam::add_langfam ( CODE ,NAME [,CODESET] ) Locale::Codes::LangFam::delete_langfam ( CODE [,CODESET] ) Locale::Codes::LangFam::add_langfam_alias ( NAME ,NEW_NAME ) Locale::Codes::LangFam::delete_langfam_alias ( NAME ) Locale::Codes::LangFam::rename_langfam_code ( CODE ,NEW_CODE [,CODESET] ) Locale::Codes::LangFam::add_langfam_code_alias ( CODE ,NEW_CODE [,CODESET] ) Locale::Codes::LangFam::delete_langfam_code_alias ( CODE [,CODESET] ) These routines are all documented in the Locale::Codes::API man page. SEE ALSO
Locale::Codes The Locale-Codes distribution. Locale::Codes::API The list of functions supported by this module. http://www.loc.gov/standards/iso639-5/id.php ISO 639-5 . AUTHOR
See Locale::Codes for full author history. Currently maintained by Sullivan Beck (sbeck@cpan.org). COPYRIGHT
Copyright (c) 2011-2013 Sullivan Beck This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.18.2 2013-11-04 Locale::Codes::LangFam(3pm)
All times are GMT -4. The time now is 01:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy