How to force awk not to print a newline

I have two columns (fields) in my text file. I am trying to insert ‘|' between two and create an HTML table based upon the updated file. My sample input file
FOO This is a description
BAR Another description for bar
TEXT Another description for TEXT

So I ran awk '{ print $1 "|"; $1=""; print}' input > output but print command is adding a new line:
FOO|
This is a description
BAR|
Another description for bar
TEXT|
Another description for TEXT

Is there any way to tell awk not to print a newline and create the file? Here is what I want so that I can create final HTML table based upon '|':
FOO|This is a description
BAR|Another description for bar
TEXT|Another description for TEXT


We use print in awk to display data, including variables. However, it will always add a newline by default. To avoid this, we use printf() or spintf() to force awk not to print a newline

Use printf() when you want awk without printing newline

The syntax is as follows to display a string/words/vaiable constructed from given expr-list according to format:

printf "format", var1
sprintf "format", var1
printf "%s", $var_a
printf "%d", $var_b
printf "%.2f\n", $1

AWK printf duplicates the printf C library function writing to screen/stdout. The complete ANSI C format specifications are recognized with conversions:

  • %c : An unsigned char type
  • %d : The int argument is converted to signed decimal notation
  • %e : The double argument is rounded and converted in the style [-]d.ddde±dd
  • %E : An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00
  • %f : The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd
  • %g : The double argument is converted in style f or e. The precision specifies the number of significant digits
  • %i : The int argument is converted to signed decimal notation.
  • %o : The unsigned int argument is converted to unsigned octal.
  • %s : The string
  • %u : An unsigned decimal
  • %x : An unsigned hexadecimal
  • %X : An unsigned hexadecimal
  • %% : A '%' is written
  • conversion qualifiers h and l : A following integer conversion corresponds to a short int or unsigned short int argument using qualifier h. A following integer conversion corresponds to a long int or unsigned long int argument when using qualifier l.

awk without printing newline

Let us try awk as follows:

awk '{ printf "%s|",$1; $1=""; print}' input.txt
awk '{ printf "%s|",$1; $1=""; print}' input.txt > output.txt
cat output.txt


The $1=""; print; will skip first field and print the rest of line.

awk - printing column value without new line using ORS

AWK has many builtin-variables. One of them is ORS. We use ORS to terminates each record on output and initially set it to "\n" (newline). In other words, we can modify ORS and skip using printf or sprintf in our awk as follows to avoid displaying a newline:

awk 'BEGIN{ORS=""}{ print $1 "|"; $1=""; print ; print "\n"}' input.txt
awk 'BEGIN{ORS=""}{ print $1 "|"; $1=""; print ; print "\n"}' input.txt > output.txt

Of course, we can skip awk and use a combination of tr and cut too. Perl is another option. But, remember, awk created for this kind of job. Hence, I would suggest sticking with awk.

Generating finale output to HTML format using AWK

Now all you have to do is type the following AWK:

awk -F'|' 'BEGIN{ print "<table>"} { print "<tr><td>" $1 "</td><td>" $2 "</td></tr>"} END{ print "</table>"}' output.txt
awk -F'|' 'BEGIN{ print "<table>"} { print "<tr><td>" $1 "</td><td>" $2 "</td></tr>"} END{ print "</table>"}' output.txt > tmp.html
cat tmp.html

Conclusion

In this quick tutorial, we learned how to use awk not to print a newline using either printf/sprintf statement or ORS awk variable. It is also possible to combine everything in a single AWK, which is left as an exercise to readers. See awk man page:
man awk


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Joe Klemmer Jan 22, 2021 @ 14:18

    Nicely done.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz