AWK, 66 bytes
a=FILENAME,b=!b?a:b{c[a]=c[a]$0RS}END{printf c[a]>b;printf c[b]>a}
Try it online!
Note: I provided a TIO line, but I have no idea how to make it work when the code needs to read from input files?
The rename approaches seem to be much shorter, but here's one in AWK that copies the data instead. At a high level, it takes a commandline like,
gawk '...code...' i.txt o.txt
which feeds both files into the program on STDIN. It uses the magic variable FILENAME
to cache the contents of each one into a separate entry in an associative array. Then the END
clause just overwrites each file with the content from the other one.
The "test" per line, which is always truthy, sets a
to the current filename over and over. And it also ensures b
is to the first filename seen.
a=FILENAME,b=!b?a:b
The code block executed per line, just appends the current line to the accumulated data for the current filename.
c[a]=c[a]$0RS
Then once all the lines from both files have been read, the END
code block writes out the files with the content swapped.
printf c[a]>b;printf c[b]>a
inode
s the goal here would be fori.txt
'sinode
to contain the data fromo.txt
'sinode
, and vice versa, so that if there are hardlinks to thoseinode
s elsewhere, their contents will appear swapped as well. Renaming can't accomplish that. \$\endgroup\$