awk to match field4 and field5, field6 and field7, field8 and field9 and so on.
Hi all,
I want to match field4 and field5, field6 and field7, field8 and field9 and so on using the following input file.
Condition:
if field4=field5, print field4
if field4=NN and field5≠NN, print field5
if field5=NN and field4≠NN, print field4
Repeat the above 3 condition for field6&7, field8&9 and so on.
Input:
Code:
loc01 1000560 G NN AG NN NN NN AG NN NN NN NN
loc02 1001612 C NN NN NN NN 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 NN NN 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 NN NN NN NN NN NN NN NN NN NN
Output file:
Code:
loc01 1000560 G AG NN AG 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 have tried the following code but it does not work:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
LEARN ABOUT X11R4
a2p
A2P(1) Perl Programmers Reference Guide A2P(1)NAME
a2p - Awk to Perl translator
SYNOPSIS
a2p [options] [filename]
DESCRIPTION
A2p takes an awk script specified on the command line (or from standard input) and produces a comparable perl script on the standard out-
put.
OPTIONS
Options include:
-D<number>
sets debugging flags.
-F<character>
tells a2p that this awk script is always invoked with this -F switch.
-n<fieldlist>
specifies the names of the input fields if input does not have to be split into an array. If you were translating an awk script that
processes the password file, you might say:
a2p -7 -nlogin.password.uid.gid.gcos.shell.home
Any delimiter can be used to separate the field names.
-<number>
causes a2p to assume that input will always have that many fields.
-o tells a2p to use old awk behavior. The only current differences are:
* Old awk always has a line loop, even if there are no line actions, whereas new awk does not.
* In old awk, sprintf is extremely greedy about its arguments. For example, given the statement
print sprintf(some_args), extra_args;
old awk considers extra_args to be arguments to "sprintf"; new awk considers them arguments to "print".
"Considerations"
A2p cannot do as good a job translating as a human would, but it usually does pretty well. There are some areas where you may want to
examine the perl script produced and tweak it some. Here are some of them, in no particular order.
There is an awk idiom of putting int() around a string expression to force numeric interpretation, even though the argument is always inte-
ger anyway. This is generally unneeded in perl, but a2p can't tell if the argument is always going to be integer, so it leaves it in. You
may wish to remove it.
Perl differentiates numeric comparison from string comparison. Awk has one operator for both that decides at run time which comparison to
do. A2p does not try to do a complete job of awk emulation at this point. Instead it guesses which one you want. It's almost always
right, but it can be spoofed. All such guesses are marked with the comment ""#???"". You should go through and check them. You might
want to run at least once with the -w switch to perl, which will warn you if you use == where you should have used eq.
Perl does not attempt to emulate the behavior of awk in which nonexistent array elements spring into existence simply by being referenced.
If somehow you are relying on this mechanism to create null entries for a subsequent for...in, they won't be there in perl.
If a2p makes a split line that assigns to a list of variables that looks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the -n
option mentioned above. This will let you name the fields throughout the script. If it splits to an array instead, the script is probably
referring to the number of fields somewhere.
The exit statement in awk doesn't necessarily exit; it goes to the END block if there is one. Awk scripts that do contortions within the
END block to bypass the block under such circumstances can be simplified by removing the conditional in the END block and just exiting
directly from the perl script.
Perl has two kinds of array, numerically-indexed and associative. Perl associative arrays are called "hashes". Awk arrays are usually
translated to hashes, but if you happen to know that the index is always going to be numeric you could change the {...} to [...]. Itera-
tion over a hash is done using the keys() function, but iteration over an array is NOT. You might need to modify any loop that iterates
over such an array.
Awk starts by assuming OFMT has the value %.6g. Perl starts by assuming its equivalent, $#, to have the value %.20g. You'll want to set
$# explicitly if you use the default value of OFMT.
Near the top of the line loop will be the split operation that is implicit in the awk script. There are times when you can move this down
past some conditionals that test the entire record so that the split is not done as often.
For aesthetic reasons you may wish to change the array base $[ from 1 back to perl's default of 0, but remember to change all array sub-
scripts AND all substr() and index() operations to match.
Cute comments that say "# Here is a workaround because awk is dumb" are passed through unmodified.
Awk scripts are often embedded in a shell script that pipes stuff into and out of awk. Often the shell script wrapper can be incorporated
into the perl script, since perl can start up pipes into and out of itself, and can do other things that awk can't do by itself.
Scripts that refer to the special variables RSTART and RLENGTH can often be simplified by referring to the variables $`, $& and $', as long
as they are within the scope of the pattern match that sets them.
The produced perl script may have subroutines defined to deal with awk's semantics regarding getline and print. Since a2p usually picks
correctness over efficiency. it is almost always possible to rewrite such code to be more efficient by discarding the semantic sugar.
For efficiency, you may wish to remove the keyword from any return statement that is the last statement executed in a subroutine. A2p
catches the most common case, but doesn't analyze embedded blocks for subtler cases.
ARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n]. A loop that tries to iterate over ARGV[0] won't find it.
ENVIRONMENT
A2p uses no environment variables.
AUTHOR
Larry Wall <larry@wall.org>
FILES SEE ALSO
perl The perl compiler/interpreter
s2p sed to perl translator
DIAGNOSTICS BUGS
It would be possible to emulate awk's behavior in selecting string versus numeric operations at run time by inspection of the operands, but
it would be gross and inefficient. Besides, a2p almost always guesses right.
Storage for the awk syntax tree is currently static, and can run out.
perl v5.8.9 2005-03-10 A2P(1)