0

I'm new to Linux and programming. My problem is the following: I have a file listing 3 columns. I want to swap the first and the last column, print it to prompt AND to a new file in one line. So I swapped the columns and printed it to prompt OR to a file.

$ awk -F, ' { t = $1; $1 = $3; $3 = t; print; } ' OFS=, liste.csv

This is my base line to print it to prompt. But it seems impossible to print it to a new file in the same command line. Does anyone have the idea? Here are some examples that didn't work:

$ awk -F, ' { t = $1; $1 = $3; $3 = t; print; } ' OFS=, liste.csv | >liste2.csv

$ printf "$(sudo awk -F, ' { t = $1; $1 = $3; $3 = t; print; } ' OFS=, liste.csv > liste2.csv)"

$ cat $(sudo awk -F, ' { t = $1; $1 = $3; $3 = t; print; } ' OFS=, liste.csv > liste2.csv)

I think you catch the drift of what I ask. Thanks!

2 Answers2

1

print it to prompt AND to a new file in one line

This sound like task for tee. Assuming

awk -F, ' { t = $1; $1 = $3; $3 = t; print; } ' OFS=,  liste.csv

does produce correct output to standard output, this

awk -F, ' { t = $1; $1 = $3; $3 = t; print; } ' OFS=,  liste.csv | tee liste2.csv

should write to liste2.csv and standard output

Daweo
  • 31,313
  • 3
  • 12
  • 25
1

Use tee command as mentioned in How to redirect output to a file and stdout

Or, redirect it within awk itself by adding print > "liste2.csv" in addition to the existing print for displaying on stdout

Sundeep
  • 23,246
  • 2
  • 28
  • 103