LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 08-15-2007, 01:33 AM   #1
EmLS
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Rep: Reputation: 0
String manipulation (subst for cut)


Well, cut is not cutting it... cough.

Anyway, I need to trim a string down to 6 characters but without trimming the trailing spaces.

I've been using cut --characters=1-6, but this seems to remove the trailing space.

This is in a bash script, using it to edit a script variable.

Thanks for all help
 
Old 08-15-2007, 02:13 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
echo ${VAR:0:6} - look into the bash manual's parameter expansion section for more
 
Old 08-15-2007, 10:21 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

Both solutions seemed to work for me, as with:
Code:
#!/bin/sh

# @(#) s1       Demonstrate cut with character trailing blanks.

set -o nounset
echo

echo "GNU bash $BASH_VERSION" >&2
# echo "zsh version $ZSH_VERSION >&2"
cut --version | head -1 >&2

echo

var="abcd  ghij  mnop"
with_cut=$( echo "$var" | cut --characters=1-6 )
with_builtin=${var:0:6}

echo " Results of operations, characters bounded by \"|\":"
echo " Original |$var|, cut |$with_cut|, bash builtin |$with_builtin|"

exit 0
Producing:
Code:
% ./s1

GNU bash 2.05b.0(1)-release
cut (coreutils) 5.2.1

 Results of operations, characters bounded by "|":
 Original |abcd  ghij  mnop|, cut |abcd  |, bash builtin |abcd  |
You didn't post any code, so it's difficult to diagnose where the problem might be.

If you are dealing with a small number of instances, then the second solution is probably best from an efficiency point-of-view, whereas if you have a large file, then I'd use cut. The latter may also be more portable, although when I invoked bash with "--posix" the script worked correctly ... cheers, makyo
 
Old 08-15-2007, 03:49 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by makyo View Post
You didn't post any code, so it's difficult to diagnose where the problem might be.
I just accepted his question at face value, but you're right that cut should have given the right value. I suspect he didn't quote his variable.

Code:
:tail -4 test.sh; ./test.sh
FOO="foo  "
echo "$(echo $FOO | cut -c 1-4)bar"
echo "$(echo "$FOO" | cut -c 1-4)bar"

foobar
foo bar
Quote:
Originally Posted by makyo View Post
The latter may also be more portable, although when I invoked bash with "--posix" the script worked correctly ... cheers, makyo
Bash isn't very good about enforcing posix compliance. IIUC, only the 'substitute-if-unset'-type parameter expansions, the 'suffix/prefix removal' expansions, and the 'string length' expansion are posix - the substring expansions aren't, though I could well be wrong. So you're right that it's almost certain cut would be more portable, but EmLS did say it was a bash script in question.
 
Old 08-16-2007, 01:53 AM   #5
EmLS
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Fixed the problem . You are correct that cut wasn't the problem. I sort of mistakenly led myself to believe it was.

(I'm editing something someone else wrote with little clue what I'm doing, so it is a total hackjob)

Anyway, thank you both for your input. It helped guide me to the solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need command for string manipulation hanke Programming 6 07-03-2007 01:27 PM
String manipulation Manuel Alvarado Linux - Newbie 2 09-06-2006 10:49 PM
string manipulation mannahazarika Programming 6 12-31-2005 12:02 PM
bash + string manipulation dave bean Programming 7 02-16-2005 12:16 PM
String manipulation with a script? philipina General 1 03-15-2004 01:07 PM


All times are GMT -5. The time now is 09:45 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration