$ cut -d' ' -f 2- file2 | paste file1 -
The cut
will remove the first column from file2
(-f 2-
means "output fields (columns) 2 onwards only"). The result of that will be passed to paste
which will put the contents of file1
in the first column. The -
in the paste
command is a placeholder for whatever is delivered on standard input (the pipe from cut
in this case).
Step by step:
$ cut -d' ' -f 2- file2
10.2317 79.1638 6.0 26.7 20.9 0.8 14.0 98.6
10.2317 79.1638 5.6 26.5 20.8 1.9 13.6 98.0
10.2317 79.1638 7.5 27.7 20.8 0.1 15.8 96.4
10.2317 79.1638 8.1 26.0 19.6 0.0 15.5 94.1
$ cut -d' ' -f 2- file2 | paste file1 -
01/01/2007 10.2317 79.1638 6.0 26.7 20.9 0.8 14.0 98.6
02/01/2007 10.2317 79.1638 5.6 26.5 20.8 1.9 13.6 98.0
03/01/2007 10.2317 79.1638 7.5 27.7 20.8 0.1 15.8 96.4
04/01/2007 10.2317 79.1638 8.1 26.0 19.6 0.0 15.5 94.1
The cut
command is expecting tab-delimited input, but since I copied and pasted from your question, it's space-delimited. If the original data is actually tab-delimited, remove the -d' '
from the cut
command.
The paste
command will add a tab between column 1 and 2 by default. If you want a space instead, use paste -d' ' file1 -
.
In another question, it was asked how to use the already existing date in file2
here, and do away with the first file completely.
I ended up with
$ paste <( date -f <( cut -d ' ' -f 1 file2 ) +"%d/%m/%Y" ) \
<( cut -d ' ' -f 2- file2 )
01/01/2007 10.2317 79.1638 6.0 26.7 20.9 0.8 14.0 98.6
02/01/2007 10.2317 79.1638 5.6 26.5 20.8 1.9 13.6 98.0
03/01/2007 10.2317 79.1638 7.5 27.7 20.8 0.1 15.8 96.4
04/01/2007 10.2317 79.1638 8.1 26.0 19.6 0.0 15.5 94.1
Note, this requires a shell that understands process substitution (<( ... )
), like bash
or ksh
, and it also requires GNU's implementation of date
.
A bit of explanation may be in order:
The process substitution <( ... )
more or less creates a temporary file containing the output of the command within the parenthesis (actually a FIFO under /dev/fd
). So the whole command will go through two steps substitutions:
paste <( date -f output_of_cut1 +"%d/%m/%Y" ) \
output_from_cut2
date -f filename
will read the dates in file filename
and format each of them according to the given format string.
Then:
paste output_from_date output_from_cut2
which will paste together the columns with the output from date
as the first column and the output of the second cut
as the other columns.
sed 's,^\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\),\3/\2/\1,'
or similar)? – Toby Speight Jan 18 '17 at 11:45