2013-03-07 60 views

回答

2

一種方法是使用paste

$ cat file1 
one file 1 
two file 1 
three file 1 
four file 1 
five file 1 

$ cat file2 
1 file 2 
2 file 2 
3 file 2 
4 file 2 
5 file 2 

$ paste -d' ' <(awk '{print $1}' file1) file2 
one 1 file 2 
two 2 file 2 
three 3 file 2 
four 4 file 2 

# Store changes back to file2 
$ paste -d' ' <(awk '{print $1}' file1) file2 > tmp && mv tmp file2 

如果你有bash舊版本不支持process substitution那麼你可以做:

$ awk '{print $1}' file1 | paste -d' ' - file2 
one 1 file 2 
two 2 file 2 
three 3 file 2 
four 4 file 2 
five 5 file 2 

你可以使用cut -d' ' -f1 file1代替awk '{print $1}' file1

相關問題