Combine text from two files, output to another

前端 未结 2 1111
太阳男子
太阳男子 2021-01-25 00:25

i\'m having a bit of a problem and i\'ve been searching allll day. this is my first Unix class don\'t be to harsh.

so this may sound fairly simple, but i can\'t get it

相关标签:
2条回答
  • 2021-01-25 01:15

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

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

    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.

    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}'
    

    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 (\t) in place of spaces as connectors to get a nicely formatted output.

    0 讨论(0)
  • 2021-01-25 01:17

    In this case join command will also work,

    join -1 1 -2 1 <(sort file1) <(sort file2)
    

    Description

    -1 -> file1
    1  -> first field of file1 (common field)
    -2 -> file2
    1 -> first field of file2 (common field)
    
    
    **cat file1**
    
    David 734.838.9801
    Roberto 313.123.4567
    Sally 248.344.5576
    Mary 313.449.1390
    Ted 248.496.2207
    Alice 616.556.4458
    Frank 634.296.1259
    
    **cat file2**
    
    Roberto Tuesday 2
    Sally Monday 8
    Ted Sunday 16
    Alice Wednesday 23
    David Thursday 10
    Mary Saturday 14
    Frank Friday 15
    

    output

    Alice 616.556.4458 Wednesday 23
    David 734.838.9801 Thursday 10
    Frank 634.296.1259 Friday 15
    Mary 313.449.1390 Saturday 14
    Roberto 313.123.4567 Tuesday 2
    Sally 248.344.5576 Monday 8
    Ted 248.496.2207 Sunday 16
    
    0 讨论(0)
提交回复
热议问题