2013-12-11 44 views
0

我在unix中有兩個文件。我只想補充與列中有兩個文件的內容明智如何在shell腳本中加入兩個文件

file 1:     file 2: 

2013-09-09 5656   2013-09-09 4321 
2013-09-10 1234   2013-09-10 3234 
         2013-09-11 5056 
         2013-09-12 1256 

我用下面的:

paste -d " " file1 file2>file3 

但預期

我需要的輸出像它不工作:

2013-09-09 5656  2013-09-09 4321 
2013-09-10 1234  2013-09-10 3234 
        2013-09-11 5056 
        2013-09-12 1256 

paste -d「」file1 file2 return:

2013-09-09 5656  2013-09-09 4321 
2013-09-10 1234  2013-09-10 3234 
2013-09-11 5056 
2013-09-12 1256 
+0

請修正格式。目前還不清楚每個文件中的什麼數據。 –

+0

你會得到什麼輸出? –

回答

3

pr是適合工作的工具:

$ pr -m -t file1 file2 
+0

謝謝,它的工作正常 – user2587385

0

paste不會嘗試將文件整齊排列在列中。它僅在列之間插入分隔符。如果默認paste file1 file2不適合您,那麼您需要親自處理。

例如:

# Assign file1 and file2 to file descriptors 3 and 4 so we can read from them 
# simultaneously in the loop. 
exec 3< file1 || exit 
exec 4< file2 || exit 

while true; do 
    # Keep looping as long as we've read a line from either file. 
    # Don't stop until both files are exhausted. 
    line_read=0 
    read a <&3 && line_read=1 
    read b <&4 && line_read=1 
    ((line_read)) || break 

    # Use `%-20s' to ensure each column is 20 characters wide at minimum. 
    printf '%-20s %-20s\n' "$a" "$b" 
done