2

I have the following line of code:

awk -F, '{printf "%09d,%d\n" ,$1,$2}' $newDir/$processNew

and it does what I want it to, but instead of overwriting the current file, it prints out of screen.

What do I need to change to overwrite the current input file which is $processNew ?

Thanks.

2 Answers 2

2

If you want to override the source file, you need to use a temporary file file:

awk -F, '{printf "%09d,%d\n" ,$1,$2}' $newDir/$processNew > tmp && mv tmp $newDir/$processNew
Sign up to request clarification or add additional context in comments.

Comments

2

With GNU awk, you can do

gawk -i inplace -options 'script' file ...

or

gawk -i inplace -v INPLACE_SUFFIX=.bak -options 'script' file ...

ref: https://www.gnu.org/software/gawk/manual/html_node/Extension-Sample-Inplace.html

3 Comments

I'm curious; how does one make those extensions work? It's obviously not part of posix awk. My awk (GNU Awk 4.0.1) on ubuntu 14.04 knows nothing of a -i switch.
Don't know what version added them. Possibly 4.3. So upgrade to make them work
4.1.3 ... ? anyway, got 4.1.1 from PPA, and that has it. What had me confused was the -i ... I didn't expect that to load that extension, but to work analogously to the one in sed. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.