Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-09-2012
Registered User
 

Join Date: Dec 2011
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
trim spaces in unix for variable

HI Guys

I have written a script using awk to split a file based on some identifier and renaming the file based on two values from specific length. ts a fixed width file.

When I am trying to fetch the values

Code:
a = substr($0,11,10)
b = substr($0,21,5);

i am getting spaces in a and b values .
I have to form a file like a_b.txt but the file is forming as "a _ b .txt"

Please help guys.

Last edited by Franklin52; 02-09-2012 at 04:29 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #2  
Old 02-09-2012
itkamaraj's Avatar
Unix is God
 

Join Date: Apr 2010
Posts: 2,355
Thanks: 21
Thanked 470 Times in 459 Posts
To remove all the spaces in $a and $b

Code:
 
a=${a// /}
b=${b// /}

Sponsored Links
    #3  
Old 02-09-2012
Registered User
 

Join Date: Dec 2011
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
Hi when i tried using the way you provided

Code:
a = substr($0,11,10)
a1= ${a// /}

i got this error

Code:
The error context is
               a= >>> ${ <<<
awk: 0602-502 The statement cannot be correctly parsed.


Last edited by Franklin52; 02-09-2012 at 04:29 AM.. Reason: Please use code tags for code and data samples, thank you
    #4  
Old 02-09-2012
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 7,289
Thanks: 55
Thanked 427 Times in 408 Posts
To trim the spaces in your awk program from the a variable:

left spaces:

Code:
gsub(/^[ \t]+/, "", a)

right spaces:

Code:
gsub(/[ \t]+$/, "", a)

Sponsored Links
    #5  
Old 02-09-2012
itkamaraj's Avatar
Unix is God
 

Join Date: Apr 2010
Posts: 2,355
Thanks: 21
Thanked 470 Times in 459 Posts
is it inside the awk ?

if it is inside the awk, use the franklin suggestion.
Sponsored Links
    #6  
Old 02-09-2012
Registered User
 

Join Date: Dec 2011
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
my data is coming as

Code:
INDIAINDIABACD      11    
INDIAINDIABACD      12


and i split the data into multiple files based in INDIAINDIA and file name will be BACD_11.txt and BACD_12.txt but in this case it is forming as
Code:
"BACD      12   .txt" and "BACD      11   .txt"

a = substr($0,11,10)
a2=gsub(/[ \t]+$/, "", a)
b = substr($0,21,5)
b2=gsub(/[ \t]+$/, "", b)
fn = a2 "_Src" b2 ".txt"

using gusb i am not able to trim also it is returning blank to a2 and b2 respectively.

This whole code is inside awk

---------- Post updated at 07:01 AM ---------- Previous update was at 05:19 AM ----------

Thanks guys I got that solved.

Last edited by Franklin52; 02-09-2012 at 07:03 AM.. Reason: Code tags
Sponsored Links
    #7  
Old 02-09-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
Try losing the $ signs in the gsub statements.
You cannot assign gsub to a variable, it only returns the exit status
What is a sample input line?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
trim spaces and replacing value badrimohanty Shell Programming and Scripting 2 06-29-2009 04:30 PM
trim spaces in a file badrimohanty Shell Programming and Scripting 7 06-22-2009 01:53 PM
TRIM spaces in shell anumkoshy UNIX for Advanced & Expert Users 3 08-31-2007 04:13 AM
To Trim spaces at the end of line sbasetty Shell Programming and Scripting 1 01-31-2007 09:01 PM
Trim white spaces using awk mona Shell Programming and Scripting 2 07-28-2006 04:47 AM



All times are GMT -4. The time now is 04:54 AM.