Reputation: 1446
EDIT:
I would like to add a column to a file but only from a specific line onwards. The column to be added is stored in another file, which has several columns and I only want to add the 3rd. The first file has two extra-lines (1st and 2nd line) compared to the second file. I would also like to add an extra string in the second line (at the end of this line)
Here is an example that shows what I want:
$ cat file1.txt
file1
index place value
1 A 0.1
2 A 0.3
3 B 0.1
4 B 0.6
$ cat file2.txt
A 1 0.3
A 1 0.4
B 1 0.4
B 1 0.6
And this it would be the desired output
$ cat output.txt
file1
index place value score
1 A 0.1 0.3
2 A 0.3 0.4
3 B 0.1 0.4
4 B 0.6 0.6
Thanks in advance
Upvotes: 2
Views: 851
Reputation: 786359
You can use tail
paste
and echo
:
awk 'NR==1; NR==2{print $0, "score"} NR>2{a[++i]=$0} NR>FNR{print a[FNR], $NF}' file1 file2
file1
index place value score
1 A 0.1 0.3
2 A 0.3 0.4
3 B 0.1 0.4
4 B 0.6 0.6
PS: To get formatted tabular output use column -t
:
awk 'NR==1;NR==2{print $0, "score"} NR>2{a[++i]=$0} NR>FNR{print a[FNR], $NF}' file1 file2|
column -t
file1
index place value score
1 A 0.1 0.3
2 A 0.3 0.4
3 B 0.1 0.4
4 B 0.6 0.6
Upvotes: 1
Reputation: 204731
$ paste -d' ' file1.txt <(sed '1s/^/\nscore\n/' file2.txt)
file1
index place value score
1 A 0.1 0.3
2 A 0.3 0.4
3 B 0.1 0.4
4 B 0.6 0.6
Upvotes: 1