Blog

03/06/2019

AWK: Remove All Blank Spaces From String / Field



I need to parse the following text file:

# 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

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

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...