Remove spaces / tabs from variable in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove spaces / tabs from variable in script
# 1  
Old 12-09-2011
Question Remove spaces / tabs from variable in script

I want to remove extra spaces from variable in aix script.
We retrieve the data from oracle database and then print the values. We have a value on 90th position.
When we execute the query on sqlplus it shows the length of 90th position as 3, but when we use the same query in aix script it shows the length as 158. Kindly advice how can we remove the extra spaces.
I tried as
Code:
 parm=` $90 | tr -d ''`
 parmlen=length(parm)

but on execution the script received below error:
Code:
Syntax Error The source line is 7.
 The error context is
                         >>>    parm=` <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 7.

# 2  
Old 12-09-2011
Code:
$ echo -e "\t   hello"
           hello
$ echo -e "\t   hello" | sed "s/[ \t]//g"
hello

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-09-2011
Looks like you are mixing up shell with awk.
Shell does not know about $90. In awk this would be field $90, yes.
On the other side defining a shell variable which's name is consisting only of numeric characters does not work, so you are definetly mixing up things.
Also the function length() is an awk function.

I don't understand if you already extracted field 90 and it has a lot of spaces in it so it produces a length of 158 instead of 3 or if you have a complete line where field 90 is not even extracted yet?

Maybe post the line using code tags and tell if you only want to measure it's length or also get it's output printed, thanks.
This User Gave Thanks to zaxxon For This Post:
# 4  
Old 12-09-2011
My apologies to did not mentioned that we are using awk in the script.
Code:
awk 'BEGIN { FS="\t" ; linewidth=80 } {
    if ( NR == 3 ) {
        num=sprintf("%010d", $2)
        numstr=sprintf("%s-%s", substr(num,1,6), substr(num,7,4))
        parm=` $90 | tr -d ''`
        parmlen=length(parm)

The output is in a single line and $90 also extract with value YES that displays correctly when we do need tried to remove the sapces
Code:
 printf("          SET TEMP Test: %5.5s %-2.3s",parmlen,parm)

Actually I am facing a problem using if condition to check the value of $90 field as below:
Code:
 if ( parm == "YES" )
            printf("          SET TEMP: %5.5s %-2.2s", $51, $52)
        else
           printf("          SET TEMP Test: %5.5s %-2.3s",parmlen,parm)

The value display thru above printf is YES but the length is 158. My condition always fails and print the line after else.

Kindly advice.

---------- Post updated at 12:24 PM ---------- Previous update was at 12:16 PM ----------

After that I change the coding to remove spaces as below
Code:
awk 'BEGIN { FS="\t" ; linewidth=80 } {
    if ( NR == 3 ) {
        num=sprintf("%010d", $2)
        numstr=sprintf("%s-%s", substr(num,1,6), substr(num,7,4))
        parm=$90
        parm=$(print "$parm" | nawk '{gsub(/^[ ]*/,"",$0); gsub(/[ ]*$/,"",$0) ; print }')
        parmlen=length(parm)

the above returned below error:
Code:
0403-057 Syntax error at line 125 : `(' is not expected

# 5  
Old 12-09-2011
Try with this ..
Code:
parm=$(print "$parm" | nawk '{$1=$1};1')

# 6  
Old 12-09-2011
This command also generate the error and the complete output converted into garbage.
The error is as below:
Code:
 
 Syntax Error The source line is 6.
 The error context is
                         >>>    parm=$(print <<<  "$parm" | nawk {05399274=05399274}
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 6.
        awk: 0602-541 There are 2 missing } characters.
        awk: 0602-540 There is a missing ) character.

---------- Post updated at 02:13 PM ---------- Previous update was at 01:08 PM ----------

My problem has been resolved as I used substr function to get 3 characters only as below:
Code:
 parm=$90
 parm= substr(parm,1,3)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove leading and trailing spaces for variable in shell script?

Hi I have variable named tablename. The value to tablename variable has leading and trailing white spaces. How to remove the leading and training white spaces and write the value of the tablename without space to a file using shell script. ( for e.g. tablename= yyy ) INPUT ... (10 Replies)
Discussion started by: pottic
10 Replies

2. Shell Programming and Scripting

PERL : Remove spaces in a variable

I have a variable I want to remove the spaces in between. The output should be How can this be done Any help will be appreciated. Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

3. Shell Programming and Scripting

Unix remove white spaces/tabs before & after pattern

Hi All, I wanted to know is there any way we can remove white spaces/tabs before & after some pattern { eg. before & after "," }. Please find below sample data below, Sat Jul 23 16:10:03 EDT 2011 , 12345678 , PROD , xyz_2345677 , testuuyt , ... (3 Replies)
Discussion started by: gr8_usk
3 Replies

4. Shell Programming and Scripting

In bash script, how to assign output of a command to a variable while keeping tabs?

Hi, wondering if it's been asked before but didn't find matches from google. Basically I have this line: myvar=$(echo -e "a\tb") Now somehow the '\t' from the echo output gets replaced with white space and then stored in $myvar. It creates a problem for me later to use tab as delimiter to do... (2 Replies)
Discussion started by: birddie
2 Replies

5. Shell Programming and Scripting

spaces to tabs - group with IP

hi buddies; i have a file.txt: Note: All the seperators are SPACE. 192.168.1.1 ParameterObject=1 Speech 1 ParameterObject=2 Speech 1 192.168.1.1 ParamFunction=1 UserID 1 (DEACTIVATED) Sector=1,Device=2,Unit=3 DeviceId 1 192.168.1.1 FeederCable=2B ... (18 Replies)
Discussion started by: gc_sw
18 Replies

6. Shell Programming and Scripting

How to remove trailing spaces from a variable?

I am getting a value from a csv file using CUT command, however the command extracting the records with trailing spaces. I am using the result into a sql session to fetch data, because of the trailing spaces the sql session is unable to fetch any data. Please let me know, how to remove this... (2 Replies)
Discussion started by: mady135
2 Replies

7. Shell Programming and Scripting

Replacing tabs with spaces

I want my program to replace tabs with spaces.1tab=4spaces.When i write aa(tab)aaa(tab)(tab)a(tab) it must show me aaxxaaaxxxxxaxxx. I think that my program works corectly but when a write aaa(tab)a it must show aaaxa but it is aaaxxxxxa.Please for help!!! That is my code: #include <stdio.h> ... (3 Replies)
Discussion started by: marto1914
3 Replies

8. Shell Programming and Scripting

spaces or Tabs?

When formatting a script let's say for instance the following: case ${choice} in 1) vi ${tmp1}.tmp # overwrite the tmp1 var with any user changes cp ${tmp1}.tmp ${tmp1} ;; ... (2 Replies)
Discussion started by: llsmr777
2 Replies

9. Shell Programming and Scripting

Variable has spaces around the string, need to remove them

Hi all, I'm a newbie to the Linux world and I got a couple of shell script questions: (1) How do combine two variables and make it equal to a third variable? For example, I got a variable $A=FirstName, $B=LastName, and I want to combine the variable into one variable so when you echo the final... (4 Replies)
Discussion started by: mikey20
4 Replies

10. Shell Programming and Scripting

Converting tabs in to spaces.

Hi! I'm using SunOS 5.7 w/ Bash 2.01. Currently, I'm working on a script that will make it possible to find textfiles which match certain criteria. While I write this message, I had some brainfarts, found the answer myself :D and the question I had in mind is now no longer the question I... (3 Replies)
Discussion started by: indo1144
3 Replies
Login or Register to Ask a Question