Skip to main content
added 10 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Given thatWith the above input, using any awk:

Given that, using any awk:

With the above input, using any awk:

added 832 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

I see in the edit history of the question that in a previous incarnation of yourthe question youthe OP showed your sample input as screenshots of what appears to be excelExcel spreadsheets and someone else actually replaced those with the current space-separated textual input example. Given that, I'm going to assume yourthe input will actually be comma-separated since that's athe most common Excel export/import format in which case it'd look like:


 

Original answer when assumingIf the input wastruly is space-separated (tabs and/or blanks):

Using, though, then still using any awk, in case that's OK instead of sed:

$ awk 'NR==FNR{map[$2]=$1; next} FNR>1{$1=map[$1]; $2=map[$2]} {printf "%-10s %-10s %-10s %-10s\n", $1, $2, $3, $4}' file2 file1
a          b          nSites     J9
SF10       SF11       3092845    1
SF10       SF12       3139733    1
SF10       SF13       3339810    1
SF10       SF14       3124263    1

The above was run on these input files:

$ head file{1,2}
==> file1 <==
a   b   nSites  J9
0   1   3092845 1
0   2   3139733 1
0   3   3339810 1
0   4   3124263 1

==> file2 <==
SF10    0
SF11    1
SF12    2
SF13    3
SF14    4

I see in a previous incarnation of your question you showed your sample input as screenshots of what appears to be excel spreadsheets and someone else actually replaced those with the space-separated textual input example. Given that, I'm going to assume your input will actually be comma-separated since that's a common Excel export/import format in which case it'd look like:


 

Original answer when assuming the input was space-separated (tabs and/or blanks):

Using any awk, in case that's OK instead of sed:

$ awk 'NR==FNR{map[$2]=$1; next} FNR>1{$1=map[$1]; $2=map[$2]} {printf "%-10s %-10s %-10s %-10s\n", $1, $2, $3, $4}' file2 file1
a          b          nSites     J9
SF10       SF11       3092845    1
SF10       SF12       3139733    1
SF10       SF13       3339810    1
SF10       SF14       3124263    1

The above was run on these input files:

$ head file{1,2}
==> file1 <==
a   b   nSites  J9
0   1   3092845 1
0   2   3139733 1
0   3   3339810 1
0   4   3124263 1

==> file2 <==
SF10    0
SF11    1
SF12    2
SF13    3
SF14    4

I see in the edit history of the question that in a previous incarnation of the question the OP showed sample input as screenshots of what appears to be Excel spreadsheets and someone else actually replaced those with the current space-separated textual input example. Given that, I'm going to assume the input will actually be comma-separated since that's the most common Excel export/import format in which case it'd look like:

If the input truly is space-separated (tabs and/or blanks), though, then still using any awk:

$ awk 'NR==FNR{map[$2]=$1; next} FNR>1{$1=map[$1]; $2=map[$2]} {printf "%-10s %-10s %-10s %-10s\n", $1, $2, $3, $4}' file2 file1
a          b          nSites     J9
SF10       SF11       3092845    1
SF10       SF12       3139733    1
SF10       SF13       3339810    1
SF10       SF14       3124263    1
added 832 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

I see in a previous incarnation of your question you showed your sample input as screenshots of what appears to be excel spreadsheets and someone else actually replaced those with the space-separated textual input example. Given that, I'm going to assume your input will actually be comma-separated since that's a common Excel export/import format in which case it'd look like:

$ head file{1,2}
==> file1 <==
a,b,nSites,J9
0,1,3092845,1
0,2,3139733,1
0,3,3339810,1
0,4,3124263,1

==> file2 <==
SF10,0
SF11,1
SF12,2
SF13,3
SF14,4

Given that, using any awk:

$ awk 'BEGIN{FS=OFS=","} NR==FNR{map[$2]=$1; next} FNR>1{$1=map[$1]; $2=map[$2]} 1' file2 file1
a,b,nSites,J9
SF10,SF11,3092845,1
SF10,SF12,3139733,1
SF10,SF13,3339810,1
SF10,SF14,3124263,1

Original answer when assuming the input was space-separated (tabs and/or blanks):

Using any awk, in case that's OK instead of sed:

Using any awk, in case that's OK instead of sed:

I see in a previous incarnation of your question you showed your sample input as screenshots of what appears to be excel spreadsheets and someone else actually replaced those with the space-separated textual input example. Given that, I'm going to assume your input will actually be comma-separated since that's a common Excel export/import format in which case it'd look like:

$ head file{1,2}
==> file1 <==
a,b,nSites,J9
0,1,3092845,1
0,2,3139733,1
0,3,3339810,1
0,4,3124263,1

==> file2 <==
SF10,0
SF11,1
SF12,2
SF13,3
SF14,4

Given that, using any awk:

$ awk 'BEGIN{FS=OFS=","} NR==FNR{map[$2]=$1; next} FNR>1{$1=map[$1]; $2=map[$2]} 1' file2 file1
a,b,nSites,J9
SF10,SF11,3092845,1
SF10,SF12,3139733,1
SF10,SF13,3339810,1
SF10,SF14,3124263,1

Original answer when assuming the input was space-separated (tabs and/or blanks):

Using any awk, in case that's OK instead of sed:

added 300 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
Loading