Thanks ya.
I fully understand about all the code now d
Can I ask you one more things?
Is it when I used A[$x]=$y
The x & y MUST be the same number,right?
Besides that, if I used A[$x]=1
The result will got some empty space for those not match with the pattern file.
awk 'NR == FNR { A[$1]=1 } A[$3]' pattern_file input_file
x
x
bca cd002 cd003 cza
bac cd004 cd005 zac
acb cd006 cd007 caz
cab cd007 cd008 azc
x
x
x represent the empty space.
Quote:
Originally Posted by Scrutinizer
A[$1] = $1 is used to set up the array of patterns. It is set when NR is equal to FNR, in other words when the first file is read i.e. the pattern file. It means fill the associative array "$1" to the value of "$1", $1 being the first field of your pattern file So with the OP's provided inputs it gets filled like so:
So once the pattern file is done it starts reading the input file and it will print each line if field 3 exist as a key in the array.
The script is only testing the existence of the array elements, not using its contents. So IMO the use of $3 is a tiny bit superfluous. I think we could also just set it to 1 instead of $1:
or, since pattern file does not have a third column.
---------- Post updated at 01:31 AM ---------- Previous update was at 01:30 AM ----------
Thanks jp2542a,
I fully understand about the code now d
Really thanks for all of your explanation
Quote:
Originally Posted by jp2542a
As Scrutinizer says, what is assigned to A[$1] is unimportant. It just has to be a value. Why are you changing the A[$1] assignment? what are you trying to do ?
BTW, then 'next' statement has a good side effect... it prevents the A[$3] clause from being executed. If a novice decides to modify the script, it will prevent some undesired behavior. And awk doesn't have to do useless work processing a clause that isn't useful for the pattern file....
Hi,
This is my input file:
ali 5 usa abc
abu 4 uk bca
alan 6 brazil bac
pinky 10 utah sdc
My desired output:
pinky 10 utah sdc
alan 6 brazil bac
ali 5 usa abc
abu 4 uk bca
Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Can anyone please help with this? I have 2 files as given below.
If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value.
file1
abc_1 foo1@a ....
abc_1 soo2@a ...
def_2 soo2@a ....
def_2 foo1@a ........ (7 Replies)
I'm still beginner and maybe someone can help me.
I have this input:
the great warrior a, b, c
and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this :
Warrior Type
a
b
c
Im still very... (3 Replies)
Dear All,
I have a data file input.csv like below. (Only five column shown here for example.)
Data1,StepNo,Data2,Data3,Data4
2,1,3,4,5
3,1,5,6,7
3,2,4,5,6
5,3,5,5,6
From this I want the below output
Data1,StepNo,Data2,Data3,Data4
2,1,3,4,5
3,1,5,6,7
where the second column... (4 Replies)
Hi,
I have a tab delimited text file where I want to delete all rows that have the same string for column 1. How do I go about doing that? Thanks!
Example Input:
aa 1
aa 2
aa 3
bb 4
bc 5
bb 6
cd 8
Output:
bc 5
cd 8 (4 Replies)
Dear fellows, I need your help.
I'm trying to write a script to convert a single column into multiple rows.
But it need to recognize the beginning of the string and set it to its specific Column number.
Each Line (loop) begins with digit (RANGE).
At this moment it's kind of working, but it... (6 Replies)
Hello....
Pls help me (and sorry my english) :)
So
I have a file (test.txt) with 1 long line.... for example:
isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh
How to print after the "error" word the 2nd 4th 5th and 7th word??
output well be:
653 isfbisfb... (2 Replies)
Hi all,
I have searched and searched, but I have not found a solution that quite fits what I am trying to do.
I have a long list of data in three columns. Below is a sample:
1,10,8
2,12,10
3,13,12
4,14,14
5,15,16
6,16,18
Please use code tags
What I need to do is as follows: If a... (4 Replies)