Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Remove leading and trailing spaces from a file

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 07-13-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 20 September 2017, 2:57 AM EDT
Location: San Jose, CA, USA
Posts: 10,502
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
Building on what RudiC suggested and avoiding the issues mentioned by Scrutinizer (which also applies to the sed code suggested by wbport), you could try:

awk 'gsub(/[[:space:]]*\|[[:space:]]*/, "|")+gsub(/^[[:space:]]+|[[:space:]]+$/, "")+1' temp_dbinfo.7880

If you are using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
Sponsored Links
Old Unix and Linux 07-13-2017
svajhala svajhala is offline
Registered User
Join Date: Nov 2009
Last Activity: 21 July 2017, 4:59 AM EDT
Posts: 120
Thanks: 39
Thanked 0 Times in 0 Posts
Originally Posted by Scrutinizer View Post

awk '{for(i=1; i<=NF; i++) sub(/^[ \t]+|[ \t]+$/,x,$i)}1' FS=\| OFS=\|  file

It worked perfectly for my requirement. Thank you Scrutinizer!

Would you spare few minutes to explain the awk command that you posted.

Sponsored Links
Old Unix and Linux 07-13-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Join Date: Nov 2008
Last Activity: 20 September 2017, 2:01 PM EDT
Location: Amsterdam
Posts: 11,533
Thanks: 500
Thanked 3,335 Times in 2,943 Posts
Hi, you are welcome. It means the following:

  • The awk code use "|" as both input and output field separator (using the FS and OFS awk variables).
  • The for loop iterates over those fields
  • for every field the gsub command replaces leading space (one or more space characters, space or TAB) ^[ \t]+ and trailing space [ \t]+$ by the empty string (contained in uninitialized variable "x").
  • The "1" means "print the record"..
  • This is repeated for every line/record in the file
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove the leading and trailing date from a CSV file shivamayam Shell Programming and Scripting 7 07-13-2017 12:12 PM
Removing leading and trailing spaces only in PERL kumar04 Shell Programming and Scripting 2 04-04-2009 03:17 PM
remove trailing and leading spaces using tr command sureshg_sampat Shell Programming and Scripting 5 03-04-2009 04:04 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 01:24 PM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 10:48 PM

All times are GMT -4. The time now is 03:53 PM.