How to force awk not to print a newline

In awk, you can force it to not print a newline at the end of the output by using the “ORS” variable. This variable controls the output record separator and by default, it is set to a newline character.

You can set the ORS variable to an empty string to prevent awk from printing a newline at the end of the output. Here’s an example:

echo "Hello World" | awk '{ORS=""; print}'

This will print “Hello World” without a newline at the end.

You can also use the -v option to set the ORS variable before running awk:

echo "Hello World" | awk -v ORS="" '{print}'

It’s also possible to use the -v option when invoking awk to set the ORS variable before running the script:

awk -v ORS="" '{print}' file.txt

It’s important to note that setting ORS to an empty string affects the entire script and all the output generated by it, meaning that if there is any other print statement in the script, it will not include a newline either. Also, the ORS variable is not a built-in variable, it’s a variable that is defined by the user, so it can be overridden by any print statement in the script.

Leave a Comment