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


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 23 July 2017, 6:19 PM EDT
Location: San Jose, CA, USA
Posts: 10,412
Thanks: 527
Thanked 3,638 Times in 3,104 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:

Code:
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
    #9  
Old Unix and Linux 1 Week Ago
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
Quote:
Originally Posted by Scrutinizer View Post
Try:

Code:
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.

Thanks.
Sponsored Links
    #10  
Old Unix and Linux 1 Week Ago
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 July 2017, 4:54 PM EDT
Location: Amsterdam
Posts: 11,488
Thanks: 494
Thanked 3,321 Times in 2,929 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
Reply

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 1 Week Ago 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 02:33 AM.