how to add a column with specific string depending on 4th column with awk - awk

I have a file in which the 4th column has numbers.
If 4th column is greater than 2 I want to add 5th column corresponding as gain; otherwise, the 5th column will have the string loss.
Input
1 762097 6706109 6
1 7202143 7792617 3
1 8922949 9815420 1
1 10502346 11074110 3
1 11188922 12267136 1
1 12566829 13910626 3
Desired output:
1 762097 6706109 6 gain
1 7202143 7792617 3 gain
1 8922949 9815420 1 loss
1 10502346 11074110 3 gain
1 11188922 12267136 1 loss
1 12566829 13910626 4 gain
How should I do this with awk?

Use awk like this:
$ awk '{print $0, ($4>2?"gain":"lose")}' file
1 762097 6706109 6 gain
1 7202143 7792617 3 gain
1 8922949 9815420 1 lose
1 10502346 11074110 3 gain
1 11188922 12267136 1 lose
1 12566829 13910626 3 gain
As you see, it is printing the full line ($0) followed by a string. This string is determined by the value of $4 using a ternary operator.

Related

Collapsing a column value into lines, copying values of a second column

I have a file with two columns (tab-separated):
In the first column I have the number of lines that I want to collapse, and in the second column is the number that I want to be pasted in each row (in a new file), based on the first column values.
File1:
col1 col2
365 1
6 1
142 1
99 0
223 0
11 1
So basically in the new file I want 365 lines with the number 1, followed by 6 lines of 1, 142 lines of 1, 99 lines of 0, 223 lines of 0 and 11 lines of 1...and so forth...
In total the new file should have 846 lines (which is the sum of the first column on the File1.
Ideally an awk command should do the trick I guess. Any inputs on this would be really appreciated...
Thanks
I would use GNU AWK following way. Contrived example to avoid superlong output, let file.txt be
col1 col2
5 1
3 0
5 1
then
awk 'NR>1{for(i=0;i<$1;i+=1)print $2}' file.txt
output
1
1
1
1
1
0
0
0
1
1
1
1
1
Explanation: I used for statement to print content of 2nd column ($2) times specified in 1st column ($1) for every line beyond 1st line (NR>1).
(tested in gawk 4.2.1)

How to loop awk command over row values

I would like to use awk to search for a particular word in the first column of a table and print the value in the 6th column. I understand how to do this searching one word at time using something along the lines of:
awk '$1 == "<insert-word>" { print $6 }' file.txt
But I was wondering if it is possible to loop this over a list of words in a row?
For example If I had a table like file1.txt below:
cat file1.txt
dna1 dna4 dna5
dna3 dna6 dna2
dna7 dna8 dna9
Could I loop over each value in row 1 and search for this word in column 1 of file2.txt below, each time printing the value of column 6? Then do this for row 2, 3 and so on...
cat file2
dna1 0 229 7 0 4 0 0
dna2 0 296 39 2 1 3 100
dna3 0 255 15 0 6 0 0
dna4 0 209 3 0 0 0 0
dna5 0 253 14 2 3 7 100
dna6 0 897 629 7 8 1 100
dna7 0 214 4 0 9 0 0
dna8 0 255 15 0 2 0 0
dna9 0 606 338 8 3 1 100
So an example looping the awk over row 1 of file 1 would return the numbers 4, 0 and 3.
The looping the command over row 2 would return the numbers 6, 8 and 1
And finally looping over row 3 would return the number 9, 2, 3
An example output might be
4 0 3
6 8 1
9 2 3
What I would really like to to is sum the total value of the numbers returned for each row. I just wasn't sure if this would be possible...
An example output of this would be
7
15
14
But I am not worried if this step isn't possible using awk as I could just do it separately
Hope this makes sense
Cheers
Ollie
yes, you can give awk multiple input files. For your example:
awk 'NR==FNR{a[$1]=a[$2]=1;next}a[$1]{print $6}' file1 file2
I didn't test the above one-liner, but it should go. At least you get the idea.
If you don't know how many columns in your file1, as you said, you want to do a loop:
awk 'NR==FNR{for(x=1;x<=NF;x++)a[$x]=1;next}a[$1]{print $6}' file1 file2
update
edit for the new requirement:
awk 'NR==FNR{a[$1]=$6;next}{for(i=1;i<=NF;i++)s+=a[$i];print s;s=0}' f2 f1
The output of above one-liner: (take f1 and f2 as your input example file1 file2):
7
15
14

Match two files with duplicate ids in awk or sed

I have two files. File 1 has 3000 rows (1500 Ids) and File 2 has 1400 rows (700 Ids). File 1 contains all the ids present in file 2. I have to match the ID column of File1 & File 2 while maintaining the order of the ids. If the id from file 2 is present in file 1 then compare column 2 and print match or mismatch. catch is there are duplicate ids and i need to keep them all. Looking for a awk or sed solution.Thanks!
File1
ID A
1 13
1 14
2 13
2 13
3 13
3 12
4 13
4 14
5 14
5 14
File 2
ID A
2 13
2 13
3 13
3 3
5 14
5 15
Desired output
ID A
2 13 Match
2 13 Match
3 13 Match
3 3 mismatch
5 14 Match
5 15 mismatch
You may use awk to achieve that,
awk '
NR==FNR{ if(a[$1]=="") a[$1]=$2; next}
/[0-9]/{
if(a[$1]==$2){
print $0,"match"
} else {
print $0,"mismatch"
} id=$1
}' File1 File2
Output:
2 13 match
2 13 match
3 13 match
3 3 mismatch
5 14 match
5 15 mismatch
Brief explanation,
NR==FNR{...}: in File1, save id/value to array a if the id has never shown previously
if(a[$1]==$2): if the id and value match in File2, view the record as match, and mismatch otherwise.
The easiest method would be to traverse the rows in File 2 and for each row find the matching ID in file 1. As you do not provide a programming language, here is the solution in pseudocode:
for all rows in file2
for all rows in file1
if current_row_file1.id = current_row_file2.id
then
if current_row_file1.value_column2 = current_row_file2.value_column2
then
print current_row_file2.id + current_row_file2.value_column2 + "Match"
else
print current_row_file2.id + current_row_file2.value_column2 + "Mismatch
The code above takes some time as you loop through all records in file 1 for every row in file 2. If your ID's in file 1 are ordered you can use an algorithm like binary search to speed up the processing. Look here for an explanation https://en.wikipedia.org/wiki/Binary_search_algorithm

awk - insert a row when value in column changes

Using awk I would like to insert a row whenever the value in the second column changes.
I have:
1 3
2 3
3 1
4 1
5 2
I would like to get:
1 3
2 3
>
3 1
4 1
>
5 2
Could anyone point me in the right direction how this can be achieved in one file?
You can use this awk:
awk 'NR==1{prev=$2; print; next} prev!=$2{print ">"} {prev=$2}1' file

AWK: Comparing two different columns in two files

I have these two files
File1:
9 8 6 8 5 2
2 1 7 0 6 1
3 2 3 4 4 6
File2: (which has over 4 million lines)
MN 1 0
JK 2 0
AL 3 90
CA 4 83
MK 5 54
HI 6 490
I want to compare field 6 of file1, and compare field 2 of file 2. If they match, then put field 3 of file2 at the end of file1
I've looked at other solutions but I can't get it to work correctly.
Desired output:
9 8 6 8 5 2 0
2 1 7 0 6 1 0
3 2 3 4 4 6 490
My attempt:
awk 'NR==FNR{a[$2]=$2;next}a[$6]{print $0,a[$6]}' file2 file1
program just hangs after that.
To print all lines in file1 with match if available:
$ awk 'FNR==NR{a[$2]=$3;next;} {print $0,a[$6];}' file2 file1
9 8 6 8 5 2 0
2 1 7 0 6 1 0
3 2 3 4 4 6 490
To print only the lines that have a match:
$ awk 'NR==FNR{a[$2]=$3;next} $6 in a {print $0,a[$6]}' file2 file1
9 8 6 8 5 2 0
2 1 7 0 6 1 0
3 2 3 4 4 6 490
Note that I replaced a[$2]=$2 with a[$2]=$3 and changed the test a[$6] (which is false if the value is zero) to $6 in a.
Your own attempt basically has two bugs as seen in #John1024's answer:
You use field 2 as both key and value in a, where you should be storing field 3 as the value (since you want to keep it for later), i.e., it should be a[$2] = $3.
The test a[$6] is false when the value in a is zero, even if it exists. The correct test is $6 in a.
Hence:
awk 'NR==FNR { a[$2]=$3; next } $6 in a {print $0, a[$6] }' file2 file1
However, there might be better approaches, but it is not clear from your specifications. For instance, you say that file2 has over 4 million lines, but it is unknown if there are also that many unique values for field 2. If yes, then a will also have that many entries in memory. And, you don't specify how long file1 is, or if its order must be preserved for output, or if every line (even without matches in file2) should be output.
If it is the case that file1 has many fewer lines than file2 has unique values for field 2, and only matching lines need to be output, and order does not need to be preserved, then you might wish to read file1 first…