LinuxQuestions.org
Help answer threads with 0 replies.
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-13-2008, 05:06 PM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Programming in BASH - Parsing a String


I have a string that I want to parse in bash that is stored in the array as follows:

echo -e ${FILEINFO[18]} >> $filename

This produces:

TIMESYS = 'UTC ' / Time system (usually UTC)

I want to remove the extra space after the 'C' in 'UTC '. Is there an easy way to do this?

Thanks in advance,
Mistro116
 
Old 06-13-2008, 05:23 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Is the trailing space in the FILEINFO[18] value? If so, FILEINFO[18]="${FILEINFO[18]% }" will delete the trailing space.

If the "UTC " isn't at the end, then you can use FILEINFO[18]="${FILEINFO[18]/UTC /UTC}"

Last edited by jschiwal; 06-13-2008 at 05:25 PM.
 
Old 06-13-2008, 05:31 PM   #3
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
${FILEINFO[18]} = "TIMESYS = 'UTC ' / Time system (usually UTC)"

So far the things you suggested, unfortunately, haven't worked. Is there something else I can do?

I tried to | tr -d ' ', but this removes all of the spaces... I just wanted to remove the one after 'UTC ' to make it 'UTC'.

Thanks in advance,
Mistro116
 
Old 06-13-2008, 05:50 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can use the date program to do simple date parsing for you.

Shell scripting is great for gluing programs together, and providing flow control, but it's no substitute for a more fully-featured language like Perl or Python if you want to do a lot of string processing.
 
Old 06-13-2008, 06:39 PM   #5
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Quote:
Originally Posted by matthewg42 View Post
You can use the date program to do simple date parsing for you.

Shell scripting is great for gluing programs together, and providing flow control, but it's no substitute for a more fully-featured language like Perl or Python if you want to do a lot of string processing.
Entirely accurate, but we should also point out that, annoying as it may be, bash does have a lot of programming syntax, and can do most "low-level" things that other programming languages do. Personally, I prefer higher level languages, like Perl, for scripting in, but maybe that's because I'm a programmer more than a system administrator.

Anyway, the "UTC" string is a fixed string, 3 characters followed by a space (4 characters total), so you can get it to work like this:

Code:
export DATE="UTC 123 456" # Using this string as an example
$ echo -e UTC${DATE:4}
UTC123 456
The :4 strips off the first four characters.
 
Old 06-13-2008, 06:41 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This should be easy enough to do with sed. It is hard to guess how generalized this has to be, but something like:
Code:
sed -i s/'UTC '/'UTC'/g $filename
where $filename names the file containing the string in question.
--- rod.
 
Old 06-15-2008, 03:54 AM   #7
c.d.e
LQ Newbie
 
Registered: May 2008
Posts: 12

Rep: Reputation: 0
You can also use cut:

Code:
echo -e ${FILEINFO[18]} | cut -c 1-14 -c 16- >> $filename
Of course, if the variable is ever something other then "TIMESYS = 'UTC ' / Time system (usually UTC)", the parsed output will be missing the 15th character.

(What cut is doing is selectively cutting out to stdout, the characters 1-14, then cutting out the characters 16 to the end of the line also to stdout. "Cut" is a bit of a misnamer, imho).

Last edited by c.d.e; 06-15-2008 at 03:58 AM.
 
Old 06-16-2008, 12:46 PM   #8
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
Thank you all so much! I've used a variety of your suggestions and together, they have allowed me to accomplish what I wanted.

You guys are fantastic =]
 
Old 06-16-2008, 10:34 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Pragmatically speaking...

"Yes, there is always 'a way to do it.'" In fact, there are always several ways.

Where you get into trouble sometimes is when you glom-onto "the first approach that pops into your head" and try to run-with-it. Then you start hammering on the thing, trying to make it work or to do this-n-that, and eventually you do (of course) "make it work," and you maybe feel very proud of yourself for your accomplishment. Which is all (of course) "well and good, but..."

Always try to pull-back and "see the big picture." If there is "more than one way to do 'it'," what's the best way?

"The best way," of course, is to discover that what you're trying to do has already been done by someone else. It probably has.

"The next-best way" is to discover that most of what you're trying to do has already been done by someone else, and so all you need to do is to leverage what they've already done. The Perl programming-language is rather famous for that, but Unix/Linux environments offer you a suite of power-tools.

It gives me pause to say this, but the Unix world has been building and championing "power tools" for more than 35 years now. (Ahem.) One of the reasons why Unix/Linux is so darned important and so heavily used is because of this extremely rich library of software tools. Get to know (some of) what's out there.

Also consider their intended purpose. "BASH scripting" is really intended to be an extension to BASH... which is intended to be "a shell." In other words, the intent of BASH-scripting is really not "to be a general-purpose programming language." There's no need for it: BASH already allows a command-line program to be written in any scripting-language you please, through the #! "shebang" construct. Therefore, if you're "doing amazing and creative things with BASH scripting," you just might be "struggling to make a square-wheel roll in order to transport your vegetables to market, when an antimatter-transporter is located conveniently nearby..."

Last edited by sundialsvcs; 06-16-2008 at 10:38 PM.
 
Old 06-18-2008, 06:43 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
"an antimatter-transporter"
Aahh, so that's where it all went ...
Physicists have often wondered why there's preponderance of matter in our section of the universe.
 
  


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
string parsing using perl bharatbsharma Programming 1 12-05-2007 06:04 AM
parsing a string from grep output in bash xpromisex Programming 2 11-12-2006 09:12 AM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM
parsing a user input string daphne19 Programming 1 04-22-2004 07:40 AM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 07:13 PM

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

All times are GMT -5. The time now is 01:40 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
Open Source Consulting | Domain Registration