I have a little data-manipulation problem which surely can be solved by using awk
or bash
.
I have two existing data-files. The first consists of tab-separated fields which are arranged in n number of blocks. Each block is separated by a single blank line.
1 3
2 4
3 5
4 6
5 7
6 8
The second file only has one column of data which is as long as every block in the first file.
a
b
c
My problem is that I have to append the data-column in file two to every block in file one, such that the output would look like:
1 3 a
2 4 b
3 5 c
4 6 a
5 7 b
6 8 c
Do you have any idea? I already tried to write the data of file two n-times (the number of blocks) in a new file and append this with awk
but I didn't get this working properly.
This simple, dynamic one liner will do the trick:
awk 'NR==FNR{a[i++]=$0;next}{print (NF?$0 OFS a[j++%i]:$0)}' OFS='\t' file2 file1
1 3 a
2 4 b
3 5 c
4 6 a
5 7 b
6 8 c
Save each line of second file in an array and use module operator ( %
) to cycle about its content for each line of first file with data:
awk '
BEGIN { FS = OFS = "\t" }
FNR == NR { data[ NR - 1 ] = $0; next }
! $0 { print; next }
{ ++i; print $0, data[ (i - 1) % 3 ] }
' file2 file1
It yields:
1 3 a
2 4 b
3 5 c
4 6 a
5 7 b
6 8 c
Suppose you have two file, a.dat
, b.dat
, you can do this
cat -n a.dat > aa.dat
cat -n b.dat > bb.dat
join aa.dat bb.dat | awk '{$1=""}1' > c.dat
rm aa.dat bb.dat
you result is saved in c.dat
. awk '{$1=""}1'
removes the first column which is the line number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.