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   -   Original Discussion by svajhala
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Join Date: Jul 2012
Last Activity: 23 January 2018, 8:56 AM EST
Location: San Jose, CA, USA
Posts: 10,946
Thanks: 611
Thanked 3,824 Times in 3,268 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   -   Original Discussion by svajhala
svajhala's Unix or Linux Image
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   -   Original Discussion by svajhala
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Join Date: Nov 2008
Last Activity: 22 January 2018, 11:34 PM EST
Location: Amsterdam
Posts: 11,713
Thanks: 532
Thanked 3,402 Times in 2,997 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 01:12 PM
Removing leading and trailing spaces only in PERL kumar04 Shell Programming and Scripting 2 04-04-2009 04:17 PM
remove trailing and leading spaces using tr command sureshg_sampat Shell Programming and Scripting 5 03-04-2009 05:04 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 11:48 PM

All times are GMT -4. The time now is 09:57 AM.