Trimming Spaces in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trimming Spaces in Unix
# 1  
Old 02-08-2012
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:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 02-08-2012 at 10:19 AM..
# 2  
Old 02-08-2012
Remove extra spaces by below trick
Say
l
Code:
ampm="a  b  c"
lampms=${lampm//[[:space:]]}
echo $lampms
abc

--Shirish Shukla

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 02-08-2012 at 11:05 AM.. Reason: Please use code tags for code and data samples, thank you
# 3  
Old 02-08-2012
Code:
tr -d [:SPACE:]

is the simplest solution for this
# 4  
Old 02-08-2012
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
# 5  
Old 02-08-2012
I know.. but.. above for simple understand ... as per que asked .. Smilie

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
# 6  
Old 02-08-2012
Quote:
Originally Posted by Shirishlnx
I know.. but.. above for simple understand ... as per que asked .. Smilie

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

# 7  
Old 02-09-2012
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 a = substr($0,11,10) b = substr($0,21,5); i am getting spaces in a and b values .... (6 Replies)
Discussion started by: manish8484
6 Replies

2. Shell Programming and Scripting

Trimming spaces from a variable

Hi guys, when I take substring of a particular data using this command var=substr($0,11,10) it comes with spaces, when I am trying to trim the spaces it is not allowing me to do that. Can you please help me out on that. As I have to reverse the output of the variable also. ---------- Post... (0 Replies)
Discussion started by: manish8484
0 Replies

3. Shell Programming and Scripting

How can I stop the unix script from trimming extra spaces?

I have a file which contains certain records about users. the row length is always fixed to 205 characters. Now I want to read each record line from the file, substring some portion out of it and put into another file. But I have observed that my script is trimming the extra spaces I have used for... (4 Replies)
Discussion started by: Pramit
4 Replies

4. Shell Programming and Scripting

Inserting spaces after a value in UNIX

Hi All, I need to prepare a shell script in UNIX to insert 400 spaces after a variable in a file "file.txt". Eg. if n=T12345 echo $n >> file.txt cat file.txt should give: T12345<400 spaces> Please... (5 Replies)
Discussion started by: learning_skills
5 Replies

5. Shell Programming and Scripting

trimming out spaces in solaris

friends, I have a script in solaris 10 SPARC system which is like this date '+Time: %m/%d/%y %H:%M:%S' echo " GDBRR GDLRR GDBWR GDLWR GDRRR GDRWR " sar -b 10 10 |/usr/xpg4/bin/awk '!/Average/ && !/SunOS/ && !/bread/ {$1="";T=$2;T1=$3;T2=$5;T3=$6;T4=$8;T5=$9}{print(T"\t",... (1 Reply)
Discussion started by: achak01
1 Replies

6. UNIX for Advanced & Expert Users

Trimming the spaces

Hi, How can I remove the unwanted spaces in the line. 123456 789 ABC DEF. - I wanna remove the sapces in this line, I need the output 123456789ABCDEF. Pls help me...... (3 Replies)
Discussion started by: sharif
3 Replies

7. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

8. Shell Programming and Scripting

trimming white spaces

I have a variable that calls in a string from txt file. Problem is the string comes with an abundance of white spaces trailing it. Is there any easy way to trim the tailing white spaces off at the end? Thanks in advance. (9 Replies)
Discussion started by: briskbaby
9 Replies

9. Solaris

spaces trimming while assigning to a variable

Hi my lovely friends, Im writing one pgm in which i trying to assign some values like $var='Jun 6' but if i do echo of this $var will trim the spaces expect one space. $echo $var $Jun 6 But if var='Jun 28', then this will works fine $echo $var $Jun 28 this is required to exctract... (2 Replies)
Discussion started by: Lokesha
2 Replies

10. Shell Programming and Scripting

handling spaces in unix

I am testing a ksh script for email. In the script I receive several parameters. One of them is a subject. The subject may contain spaces. Ex. Test this. When I am running the script on telnet to test, how should the syntax at the command line be written. I have this: ksh ResendE.sh '001111'... (2 Replies)
Discussion started by: supercbw
2 Replies
Login or Register to Ask a Question