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
    #1  
Old Unix and Linux 07-12-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
Remove leading and trailing spaces from a file

Hi,

I am trying to remove leading and trailing spaces from a file using awk but somehow I have not been able to do it.

Here is the data that I want to trim.


Code:
07/12/2017 15:55:00               |entinfdev        |AD ping Time ms          |      .474|      1.41|      .581|green        |flat
07/12/2017 15:55:00              |entinfdev                         |CPU Busy%             |         1|         2|         1|green      |flat
07/12/2017 15:55:00        |entinfdev            |Collected at                |1499888700|1499889000|1499889300|grey        |flat
07/12/2017 15:55:00      |entinfdev     |FS /tmp Used%             |         1|         1|         1|green  |flat
07/12/2017 15:55:00 |entinfdev    |FS /var/tmp Used%            |        74|        74|        74|orange    |flat

And I want the data to be looked like this.


Code:
07/12/2017 15:55:00|entinfdev|AD ping Time ms|.474|1.41|.581|green|flat
07/12/2017 15:55:00|entinfdev|CPU Busy%|1|2|1|green|flat
07/12/2017 15:55:00|entinfdev|Collected at|1499888700|1499889000|1499889300|grey|flat
07/12/2017 15:55:00|entinfdev|FS /tmp Used%|1|1|1|green|flat
07/12/2017 15:55:00|entinfdev|FS /var/tmp Used%|74|74|74|orange|flat

Please let me know if there a better way to do it.

Thank you.

Last edited by svajhala; 07-12-2017 at 05:31 PM..
Sponsored Links
    #2  
Old Unix and Linux 07-12-2017
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 18 September 2017, 4:38 AM EDT
Location: Zürich
Posts: 8,627
Thanks: 347
Thanked 1,111 Times in 959 Posts
Seems to be an easier job for sed than awk. Show us the awk you have, maybe we can work with that.
Sponsored Links
    #3  
Old Unix and Linux 07-12-2017
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 18 September 2017, 12:12 PM EDT
Location: Mississippi
Posts: 113
Thanks: 31
Thanked 17 Times in 17 Posts
Try using sed. First create a file like demo.sed containing

Code:
s/ *\| */|/g

and run it with

Code:
sed -f demo.sed myFileToConvert >convertedFile

Of course "demo" can be replaced with something meaningful in your shop.

What this does is delete any spaces on either side of the "|" character, as many times as needed for each line.

HTH
    #4  
Old Unix and Linux 07-12-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
I tried the following awk command.


Code:
awk 'BEGIN{FS=" * *"; OFS="|"} {$1=$1; print}' temp_dbinfo.7880

However its inserting 'pipes' where it is not required as shown below. Also in some places it is inserting double pipes instead of one.


Code:
07/12/2017|15:55:00||entinfdev||AD|ping|Time|ms|||.474||1.41||.581|green||flat
07/12/2017|15:55:00||entinfdev||CPU|Busy%|||1||2||1|green||flat
07/12/2017|15:55:00||entinfdev||Collected|at||1499888700|1499889000|1499889300|grey||flat
07/12/2017|15:55:00||entinfdev||FS|/tmp|Used%|||1||1||1|green||flat

I want the date column and few other columns to be printed without the '|' between the date and the time, Something like,


Code:
07/12/2017 15:55:00


Code:
AD ping Time ms


Last edited by svajhala; 07-12-2017 at 05:43 PM..
Sponsored Links
    #5  
Old Unix and Linux 07-13-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 26 September 2017, 2:20 PM EDT
Location: Amsterdam
Posts: 11,540
Thanks: 500
Thanked 3,337 Times in 2,944 Posts
Try:

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

The Following User Says Thank You to Scrutinizer For This Useful Post:
svajhala (07-13-2017)
Sponsored Links
    #6  
Old Unix and Linux 07-13-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 September 2017, 11:01 AM EDT
Location: Aachen, Germany
Posts: 11,379
Thanks: 299
Thanked 3,529 Times in 3,246 Posts
How about

Code:
awk 'gsub(/ *\| */, "|")+1' file

Sponsored Links
    #7  
Old Unix and Linux 07-13-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 26 September 2017, 2:20 PM EDT
Location: Amsterdam
Posts: 11,540
Thanks: 500
Thanked 3,337 Times in 2,944 Posts
@RudiC: I'd say that would work if there are only spaces and no tabs and there is no leading space in the first field and no trailing space in the last field.
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 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:57 PM.