Variable has spaces around the string, need to remove them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable has spaces around the string, need to remove them
# 1  
Old 10-13-2005
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 variable, it looks like "Joe Smith", not "JoeSmith". What is the exact syntax for that? (Im using bash shell script)

(2) Down below is a script that I executed called testfile. When I execute, note the bold face item where there is a space between the numbers 516 and 10757. How can I get rid of the space before and after those two numbers? The string I need to output is a little bit tricky...Im puzzled on this one.

Thanks in advance for your response.

[root@mymachine mikey]# sh testfile
Oct
516
10757
Mycmd1 is next
get "$$ BID=# 516 " RPT132.20051012
get "$$ BID=# 10757 " RPT590.20051012






my script looks like this:

[root@mymachine mikey]# cat testfile
# Set date
curdate=`date +%Y%m%d`
mondate="Oct 12"
#daydate=`date +%d`
#totaldate=$mondate . $daydate
mondate=Oct
daydate=12
#totaldate=$mondate + $daydate
cat filelist.txt | grep -i "Oct 12" | grep -i rpt132 > mike1.txt
cat filelist.txt | grep -i "Oct 12" | grep -i rpt590 > mike2.txt

echo $mondate
#
# Strip the Batch ID from the listing of the file
#rm mike1.txt
#rm mike2.txt
batchid1=`cat mike1.txt | cut -nb 27-36`
echo $batchid1
batchid2=`cat mike2.txt | cut -nb 27-36`
echo $batchid2
echo "Mycmd1 is next"
mycmd1="get \"\$\$ BID=#$batchid1\" RPT132.$curdate"
echo $mycmd1
mycmd2="get \"\$\$ BID=#$batchid2\" RPT590.$curdate"
echo $mycmd2
#
#

[root@mymachine mikey]#
# 2  
Old 10-13-2005
For you first question :
var1="Joe"
var2="Smith"
echo ${var1}" "${var2}


result is "Joe Smith"

I don't understand your second question...
# 3  
Old 10-13-2005
First - the UUoC patrol....

cat filelist.txt | grep -i "Oct 12" | grep -i rpt132 > mike1.txt

better written as

grep -i "Oct 12" filelist.txt | grep -i rpt132 > mike1.txt

and

batchid1=`cat mike1.txt | cut -nb 27-36`

better written as

batchid1=`cut -nb 27-36 mike1.txt`

Could the second problem be due to the fact that mike1.txt contains white space?

try:

batchid1=`cut -nb 27-36 mike1.txt | sed 's/ //'`

Else; post mike1.txt so we can see the input.

Cheers
ZB
# 4  
Old 10-13-2005
More info on mike1.txt mike2.txt

Hi zazzybob.

Here are my two scripts I am reading from.

[mikey@mymachine mikey]$ cat mike1.txt
-r--r--r-- 1 mikftp 516 508 Oct 12 07:11 rpt132
[mikey@mymachine mikey]$ cat mike2.txt
-r--r--r-- 1 mikftp 10757 58 Oct 12 07:11 rpt590
[mikey@mymachine mikey$]


Thanks for reviewing it for me. Smilie
# 5  
Old 10-13-2005
mikey20, look at what you posted. Those aren't scripts.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: lodhi1978
5 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

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

4. Shell Programming and Scripting

Remove spaces in the beginning of a string

Hi, I am trying to remove spaces from the beginning of a string (which i am using in my shell script). For ex - my string looks like this - " no rows selected" and i want the string to look like this - "no rows selected" How can i achieve this? Thanks! (18 Replies)
Discussion started by: shrutihardas
18 Replies

5. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

6. Shell Programming and Scripting

How to remove extra spaces from a string??

Hi, I have a string like this and i want to remove extra spaces that exists between the words. Here is the sentence. $string="The small DNA genome of hepadnaviruses is replicated by reverse transcription via an RNA intermediate. This RNA "pregenome" contains ... (2 Replies)
Discussion started by: vanitham
2 Replies

7. Shell Programming and Scripting

sed - remove spaces before 1rst occurence of string

seems easy but havent found in other posts... i want to delete any spaces if found before first occurence of ${AI_RUN} sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
Discussion started by: danmauer
6 Replies

8. Shell Programming and Scripting

sh, ksh: command to remove front spaces from a string?

dear pro-coders, is there any command out there that takes out the front spaces from a string? sample strings: 4 members 5 members 3 members but it has to be like so: 4 members 5 members 3 members (3 Replies)
Discussion started by: pseudocoder
3 Replies

9. Shell Programming and Scripting

remove blank spaces in a string

can any help how to remove blank spaces in a string? STR="GOOD BYE" by removing blank spaces, the string should be GOOD,BYE thanks in advance (2 Replies)
Discussion started by: spandu
2 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies
Login or Register to Ask a Question