The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep throws in dashes? kingdbag UNIX for Dummies Questions & Answers 3 04-27-2007 04:18 PM
Alternatives for CLOCK_MONOTONIC amitks21 UNIX for Advanced & Expert Users 3 11-17-2006 04:09 AM
fwrite throws segmentation fault fermisoft High Level Programming 6 09-13-2005 01:46 AM
ASP alternatives Ricki UNIX for Dummies Questions & Answers 3 05-30-2001 05:38 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-08-2008
naseert naseert is offline
Registered User
  
 

Join Date: May 2008
Posts: 2
Substr throws an ERROR. Any alternatives?

Can somebody please help me to remove the last character of a string.??

I have a string variable, in which I dynamically put values in a for loop.I want to remove the last character from the string.

But, the problem is I will not know which character can come in the string (Its inside for loop). So I tried using substr. substr does not work here.

Is there any alternative to do this?? Here my code goes.

....
for files in $rawSourceFiles
loop
MID_INFILE=`cat “${files}” | grep “Message-Id:"`
len=`expr “${MID_INFILE}” : ‘.*’`
len=`expr $len - 1`
trimmedMID_INFILE=`expr substr “${MID_INFILE}” 1 ${len}`

.....
...
done

But, it does not seem working. It throws an error “expr: syntax error” ..

What all I want here is in trimmedMID_INFILE should be the string MID_INFILE with last character trimmed. (last char could be anything like "\n", "/", etc.)


Can anybody please enlighten me?? I would be so grateful. Thanks in advance.
  #2 (permalink)  
Old 05-08-2008
hemangjani hemangjani is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 73
Considering the ''expr' error that you are getting is from the code where you are trying to 'trim', than use the following:

Code:

trimmedMID_INFILE=`echo $MID_INFILE | cut -b 1-$len`
- MID_INFILE is you string.
- cut -b (cut by byte)
- 1-$len (from 1st byte till $len - considering $len is already length - 1)

  #3 (permalink)  
Old 05-08-2008
naseert naseert is offline
Registered User
  
 

Join Date: May 2008
Posts: 2
Thank you..

Thanks Hemangjan.. !!! Thanks a lot.. It worked.. Yay...!!!



One more question,

if [ $a = $b ]
then
echo SUCCESS
fi

If I want to case-insensitive-compare these two variables, what should I use?/

Sorry to ask these dumb questions. I'm a newbie to SHELL SCRIPTING..

:-(
  #4 (permalink)  
Old 05-08-2008
amit_arora amit_arora is offline
Registered User
  
 

Join Date: May 2008
Posts: 9
This link may help
how to make case insensitive checks????
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0