且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

合并两个文件中的文本,输出到另一个

更新时间:2023-11-22 13:33:10

首先使用sort对文件进行排序,然后使用这个命令:

First, sort the files using sort and then use this command:

paste file1 file2 | awk '{print $1,$4,$2,$5}'

这会让你非常接近.之后,您必须弄清楚如何将时间从 24 小时格式转换为 12 小时格式.

This will bring you pretty close. After that you have to figure out how to format the time from the 24 hour format to the 12 hour format.

如果你想避免单独使用 sort,你可以像这样引入更多的复杂性:

If you want to avoid using sort separately, you can bring in a little more complexity like this:

paste <(sort file1) <(sort file2) | awk '{print $1,$4,$2,$5}'

最后,如果您还没有弄清楚如何以 12 小时格式打印时间,这里是您的完整命令:

Finally, if you have not yet figured out how to print the time in 12 hour format, here is your full command:

paste <(sort file1) <(sort file2) | awk '{"date --date="" $5 ":00:00" +%I%P" |& getline $5; print $1 " " $4 " " $2 " " $5 }'

您可以使用制表符 ( ) 代替空格作为连接符,以获得格式良好的输出.

You can use tabs ( ) in place of spaces as connectors to get a nicely formatted output.