How to cat and sed together?

Yes:

sed 's/0/1/g' "$location_x/file_1" >"$location_y/file2"

Your code first makes a copy of the first file and then changes the copy using inline editing with sed -i. The code above reads from the original file, does the changes, and writes the result to the new file. There is no need for cat here.

If you're using GNU sed and if the $location_x could contain a leading -, you will need to make sure that the path is not interpreted as a command line flag:

sed -e 's/0/1/g' -- "$location_x/file_1" >"$location_y/file2"

The double dash marks the end of command line options. The alternative is to use < to redirect the contents of the file into sed. Other sed implementation (BSD sed) stops parsing the command line for options at the first non-option argument whereas GNU sed (like some other GNU software) rearranges the command line in its parsing of it.

For this particular editing operation (changing all zeros to ones), the following will also work:

tr '0' '1' <"$location_x/file_1" >"$location_y/file2"

Note that tr always reads from standard input, hence the < to redirect the input from the original file.


I notice that on your sed command line, you try to access /${location_y}/file2, which is different from the path on the line above it (the / at the start of the path). This may be a typo.