LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2010, 07:07 AM   #1
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Rep: Reputation: 16
Question Remove the Last character from Variable


Hi,

i have the following:
Quote:
echo %host%|sed "$s/.$//"
this would remove the Last character of the value assigned to the %host%. for example if my value is: abcd i get abc. but i am not able to assign the output. for example when i do

Quote:
set k=`echo %host%|sed "$s/.$//"`
after doing echo %k i get no output at the command prompt...!!
whereas when i just type:
Quote:
echo abcd|sed "$s/.$//"
at the command prompt i get abc

any ideas?? maybe some other ways to Remove the Last character...??

Thank you in advance for your help.
 
Old 06-22-2010, 07:26 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi NetRock, forgive my ignorance, but what kind of syntax is this? Which shell are you running on?
Code:
set k=`echo %host%|sed "$s/.$//"`
%host% and %k resemble DOS variables..
 
Old 06-22-2010, 07:28 AM   #3
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by NetRock View Post
Hi,

i have the following:


this would remove the Last character of the value assigned to the %host%. for example if my value is: abcd i get abc. but i am not able to assign the output. for example when i do



after doing echo %k i get no output at the command prompt...!!
whereas when i just type:


at the command prompt i get abc

any ideas?? maybe some other ways to Remove the Last character...??

Thank you in advance for your help.

Code:
param=`echo host| sed 's/.$//g'`
echo $param
 
Old 06-22-2010, 07:28 AM   #4
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by colucix View Post
Hi NetRock, forgive my ignorance, but what kind of syntax is this? Which shell are you running on?
Code:
set k=`echo %host%|sed "$s/.$//"`
%host% and %k resemble DOS variables..
that is what i initially thought...ksh??
 
Old 06-22-2010, 07:30 AM   #5
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
nope...
 
Old 06-22-2010, 07:32 AM   #6
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi...

Thanks for your reply
Running under DOS.!! after will use a batch file.
 
Old 06-22-2010, 08:01 AM   #7
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Under the DOS prompt this would work perfectly.
Quote:
echo host| sed 's/.$//g'
But this one:
Quote:
set param=`echo host| sed 's/.$//g'`
when i do echo param i get parama
this does not work~~!!!! any idea???

thank you for your help,
 
Old 06-22-2010, 08:08 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I know we are all probably a little lost seeing this is a linux site and not windows, but you are putting your newly created variable in between percentages?

yes??

Code:
set param=`echo host| sed 's/.$//g'`

echo %param%
This is only a guess at the issue I am afraid
 
Old 06-22-2010, 08:33 AM   #9
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Good day grail,

you are right about linux site.... but i am using both to get the max. out of the two worlds. i want to avoid LONG Batch files by using the POWER of Linux.

Thank you again for your help. i will look for the code, once have it, will post it here.

Cheers...
 
Old 06-22-2010, 10:51 AM   #10
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Here is the solution for the ones who are interested:

Quote:
echo %host%|sed s/.$//g >clus.txt
set /p host= < clus.txt
so we have to send it to a file and set it after.

 
Old 06-22-2010, 07:48 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
so we have to send it to a file and set it after.
So not to be too narky, but just another illustration of the serious limitations of windblows .... yet again ... LOL
 
Old 06-22-2010, 11:29 PM   #12
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
WindBlows ...... interesting Truth!!! nice
 
Old 06-23-2010, 12:05 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
with Windows, you don't have to create a temporary file just to store your result. Use the venerable for loop. something like this

Code:
for /F %%a ( echo %host% | sed ...) do (
  set var=%%a
)
 
Old 06-23-2010, 12:07 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
windoze: echo %VAR:~0,-1%
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to remove a certain character from a variable linuxromeo Linux - Newbie 5 06-15-2010 08:03 AM
insert character into a variable jadeddog Programming 4 11-04-2008 03:39 PM
Bash: Counting the number of character occurences in a variable basildon Linux - Newbie 3 09-22-2008 10:11 AM
python: using a variable to print unicode character donnied Programming 3 02-09-2008 02:51 AM
[Bash] New line character in variable michael_hk Linux - Newbie 4 06-15-2006 02:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration