AWK: Remove All Blank Spaces From String / Field

I need to parse the following text file:

ADVERTISEMENTS

# example.net config
user = www30021
group = ftp30020
uid = 30021
gid = 30020
tmp_path = /netapp42/shared/www/images/host.example.com/tmp.bin
tmp_perms = defaults,nodev,nosuid,noexec
jail = on
location = /jails/apache/h/host.example.com/
sftp = on
ftps = off
php-cgi = on
perl-cgi = off

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsawk
TimeN/A
I need to find out value for each field and remove any blank spaces / white spaces from the field. How do I solve this problem using awk under Linux or Unix like operating systems?

Use the following syntax to to read each field and display it on the screen:
awk -F'Field-Separator-Here' '/Field-Name-Here/{ print $2}' /path/to/input/file

In this example read value for the field user, enter:

awk -F'=' '/user/{ print "|" $2 "|"}' foo.conf

Sample outputs (note down the white space):

| www30021  |

To remove all unwanted whitespace‎s, enter:

awk -F'=' '/user/{gsub(" |\t","",print "|" $2 "|"); print $2}' filename

Sample outputs (note down the white space):

|www30021|

Final, awk statement will look as follows:

awk -F'=' '/user/{gsub(" |\t","",print $2 ); print $2}' filename
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • mdebusk Apr 14, 2013 @ 19:39

    I’m just barely getting started with awk, so please pardon my ignorance.

    What happens with

    awk '{print $3}' filename

    ?

    • 🐧 nixCraft Apr 15, 2013 @ 5:52

      Just print 3rd field. But, keep in mind the following:

      ## works 
      awk '{ print $3}' <<<'user = foo'
      ## fail
      awk '{ print $3}' <<<'user=foo'
  • mdebusk Apr 15, 2013 @ 13:13

    Ah! That makes sense. Thank you.

  • Dan Apr 15, 2013 @ 21:31

    And without awk:

    cut -d = -f 2 foo.conf | tr -d ” “

  • majkinetor Apr 17, 2013 @ 5:11

    Neither example will work correctly if there is space in the values. Trimming would be better option then replacing.

  • Jotne May 2, 2013 @ 7:01

    You have an error extra “print $2″ and no need ‘ in -F
    awk -F= ‘/user/{gsub(” |\t”,””); print $2}’

    You should also point out that your input could be
    user=www30021
    in your example, as mdebusk pointed out.
    Why all this extra, when your example is fine to go with awk ‘\user\ {print $3}’

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.