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-08-2012
Registered User
 

Join Date: Dec 2011
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
Trimming Spaces in Unix

Hi All,

I am using following script to name the file base of some values

Code:
#!/bin/sh
sourcefile=$1
awk '
BEGIN{ n = 1; name = "FILEFILE12" n ".txt"; }
{
if (substr($0,1,10) == "FILEFILE12") 
{
close (name)
n++
a = substr($0,11,10);
b = substr($0,21,5);
name = b "_Src_" a ".txt"

}
print > name
}
' $sourcefile

Values in a and b are coming with spaces and hence the file which is forming is like "test1 _Src_test2 .txt" .

Can you please help me in trimming the spaces.
Moderator's Comments:
Please use next time code tags for your code and data

Last edited by vbe; 02-08-2012 at 09:19 AM..
Sponsored Links
    #2  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Location: India
Posts: 102
Thanks: 9
Thanked 2 Times in 2 Posts
Remove extra spaces by below trick
Say
l
Code:
ampm="a  b  c"
lampms=${lampm//[[:space:]]}
echo $lampms
abc

--Shirish Shukla

Moderator's Comments:
How to use code tags

Last edited by Franklin52; 02-08-2012 at 10:05 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #3  
Old 02-08-2012
pandeesh's Avatar
Registered User
 

Join Date: Jul 2011
Posts: 276
Thanks: 69
Thanked 11 Times in 11 Posts

Code:
tr -d [:SPACE:]

is the simplest solution for this
    #4  
Old 02-08-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
You just need to tweak the substr function parameters to avoid the space. Provide the input file you are using so that we can also have a look.

--ahamed
Sponsored Links
    #5  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Location: India
Posts: 102
Thanks: 9
Thanked 2 Times in 2 Posts
I know.. but.. above for simple understand ... as per que asked ..

Note: SPACE here always have to be in lower case .... [:space:]

# a="a b c"
# echo $a | tr -d [:SPACE:]
tr: invalid character class `SPACE'

Also
# echo $a | tr -d [:space:]
b c <-- Missing 1st char ...


# echo $a | tr -d "[:space:]" <-- Must be quoted
abc

There are multiple ways ... using sed, awk, tr etc...

-Shirish
Sponsored Links
    #6  
Old 02-08-2012
pandeesh's Avatar
Registered User
 

Join Date: Jul 2011
Posts: 276
Thanks: 69
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by Shirishlnx View Post
I know.. but.. above for simple understand ... as per que asked ..

Note: SPACE here always have to be in lower case .... [:space:]

# echo $a | tr -d "[:space:]" <-- Must be quoted
abc

There are multiple ways ... using sed, awk, tr etc...

-Shirish
Else:

Code:
pandeeswaran@ubuntu:~$ echo a b c|tr -d [[:space:]]
abc

Sponsored Links
    #7  
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

INDIAINDIABACD 11 XYZ
INDIAINDIABACD 12 XYZ


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 "BACD _Src_12 .txt" and "BACD _Src_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:02 AM ---------- Previous update was at 05:33 AM ----------

Thanks guys , I got the solution.
Thanks for your help
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
Trimming spaces from a variable manish8484 Shell Programming and Scripting 0 12-13-2011 07:50 AM
How can I stop the unix script from trimming extra spaces? Pramit Shell Programming and Scripting 4 11-02-2011 06:22 AM
trimming out spaces in solaris achak01 Shell Programming and Scripting 1 10-16-2009 07:40 AM
Trimming the spaces sharif UNIX for Advanced & Expert Users 3 07-07-2008 06:26 AM
trimming white spaces briskbaby Shell Programming and Scripting 9 09-23-2006 06:10 PM



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