using awk insert new data and adding how far away [closed] - awk

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i have a data set like this (file.txt) tab separated
1 a
3 b
5 c
7 d
8 e
12 f
17 g
20 h
When i want to add a new data
6
I want to create new column which includes remaining from new entry.
Desired output is like that:
1 a 5
3 b 3
5 c 1
6 0
7 d 1
8 e 2
12 f 6
17 g 11
20 h 14
I have tried:
awk -v new="6" '
BEGIN {
FS=OFS="\t"
{gsub(new,",\n")}
{print $0"\n"$2,$3=|new-$1|}
' file

$ awk -v new=6 '
BEGIN {
FS=OFS="\t"
}
{
if((new<$1)&&f=="") { # when greater than new values seen
print new # for the first time, act
f=1 # flag to print only once
}
print $1,$2,((v=new-$1)<0?-v:v) # abs with ternary operator
}' file
Output:
1 a 5
3 b 3
5 c 1
6
7 d 1
8 e 2
12 f 6
17 g 11
20 h 14

Related

calculate current line column less previous line column using awk

my input
a 9
b 2
c 5
d 3
e 7
desired output (current line column 2 - previous line column 2)
a 9
b 2 -7
c 5 3
d 3 -2
e 7 4
explanation
a 9
b 2 -7 ( 2-9 = -7 )
c 5 3 ( 5-2 = 3 )
d 3 -2 ( 3-5 = -2 )
e 7 4 ( 7-3 = 4 )
I tried this without success
awk '{ print $1, $2,$2 - $(NR-1) }' input
I want a awk code to generate an additional column contains de calculation of current line less previous line in column 2
You can try this awk
$ awk 'NR==1{ print $0 } NR>1{ print $0,$2 - pre } { pre=$2 }' file
a 9
b 2 -7
c 5 3
d 3 -2
e 7 4

Use awk to replace value from column A with value from column B [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm very new to coding so I am here seeking some help. I have the table below, and I need to do as follow:
Every time that
[All_SNPs]<[Informative_SNPs]
I need to replace negative numbers or number=0 in [All_SNPs] with the values in [Informative_SNPs]. I have tried with awk but I can't get my head around this. Thank you if you can help.
Input
ID Informative_SNPs All_SNPs
1 13 0
2 29 -27
3 15 18
4 10 0
5 11 -850
6 25 37
Output
ID Informative_SNPs All_SNPs
1 13 13
2 29 29
3 15 18
4 10 10
5 11 11
6 25 37
awk 'NR>1 && ($3<=0 || $3<$2) {$3=$2}1' file
Output:
ID Informative_SNPs All_SNPs
1 13 13
2 29 29
3 15 18
4 10 10
5 11 11
6 25 37

Transpose column to row using awk

In my data file, there is a certain column I am interested. So I used awk to print out only that column (awk '{print $4}') and put a condition to eliminate using "if". However, I could not figure out how to transpose every nth line on that column to new row.
input:
1
2
3
4
5
6
7
8
9
desired output:
1 4 7
2 5 8
3 6 9
I have checked out the other solutions and tried but none of them gave me what I want. I will appreciate if anyone could help me with that.
you can also use pr here
$ seq 9 | pr -3ts' '
1 4 7
2 5 8
3 6 9
$ seq 9 | pr -5ts' '
1 3 5 7 9
2 4 6 8
where the number indicates how many columns you need and the s option allows to specify the delimiter between columns
Using awk:
$ seq 9 |
awk ' {
i=((i=NR%3)?i:3) # index to hash a
a[i]=a[i] (a[i]==""?"":" ") $1 # space separate items to a[i]
}
END {
for(i=1;i<=3;i++) # from 1 to 3 (yes, hardcoded)
print a[i] # output
}'
Output:
1 4 7
2 5 8
3 6 9
The columns program from the autogen package can do this, e.g.:
seq 9 | columns --by-column -w1 -c3
Output:
1 4 7
2 5 8
3 6 9

Convert n number of rows to columns repeatedly using awk

My data is a large text file that consists of 12 rows repeating. It looks something like this:
{
1
2
3
4
5
6
7
8
9
10
}
repeating over and over. I want to turn every 12 rows into columns. so the data would look like this:
{ 1 2 3 4 5 6 7 8 9 10 }
{ 1 2 3 4 5 6 7 8 9 10 }
{ 1 2 3 4 5 6 7 8 9 10 }
I have found some examples of how to convert all the rows to columns using awk: awk '{printf("%s ", $0)}', but no examples of how to convert every 12 rows into columns and then repeat the process.
Here is an idiomatic way (read golfed down version of Tom Fenech's answer) of doing it with awk:
$ awk '{ORS=(NR%12?FS:RS)}1' file
{ 1 2 3 4 5 6 7 8 9 10 }
{ 1 2 3 4 5 6 7 8 9 10 }
{ 1 2 3 4 5 6 7 8 9 10 }
ORS stands for Output Record Separator. We set the ORS to FS which by default is space for every line except the 12th line where we set it to RS which is a newline by default.
You could use something like this:
awk '{printf "%s%s", $0, (NR%12?OFS:RS)}' file
NR%12 evaluates to true except when the record number is exactly divisible by 0. When it is true, the output field separator is used (which defaults to a space). When it is false, the record separator is used (by default, a newline).
Testing it out:
$ awk '{printf "%s%s", $0, (NR%12?OFS:RS)}' file
{ 1 2 3 4 5 6 7 8 9 10 }

awk script to add numbers in one column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I add (numbers on the 2nd column) using for or while loops in an awk script?
While numbers at $1 are random and in increasing order:
1 through 2 in first iteration,
1.1 through 2.1 in 2nd iteration,
1.2 through 2.2 in 3rd iteration,
1.3 through 2.3 in 4th iteration
... up to the end
That means with 0.1 increment at each iteration.
Expected output:
39 for 1st iteration
47 for 2nd iteration...
Input data:
1.0 1
1.1 3
1.2 4
1.3 3
1.4 5
1.5 7
1.6 10
2.0 6
2.1 9
2.2 2
2.3 8
2.4 0
3.0 4
3.2 5
4.0 8
4.1 6
5.0 7
6.0 6
7.0 7
8.7 9
9.8 2
Here, I multiply each $1 by 10 to avoid problems with imprecise decimal numbers.
awk -v max=$(tail -1 data | awk '{print $1*10}') '
{n = $1 * 10}
NR==1 {min = n}
{
for (i=min; i<=(max-10); i++) {
if (i <= n && n <= (i+10)) {
sum[i, i+10] += $2
}
}
}
END {
for (key in sum) {
split(key, a, SUBSEP)
printf "[%.1f,%.1f] = %d\n", a[1]/10, a[2]/10, sum[key]
}
}
' data | sort -n
output
[1.0,2.0] = 39
[1.1,2.1] = 47
[1.2,2.2] = 46
[1.3,2.3] = 50
...
[8.6,9.6] = 9
[8.7,9.7] = 9
[8.8,9.8] = 2