Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 30 Jul 2014, 23:40
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to manipulate strings with bash?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [14 Posts]  
Author Message
2byte

Joined: 09 Oct 2006
Posts: 357

PostPosted: Fri 27 Nov 2009, 16:43    Post subject:  How to manipulate strings with bash?
Subject description: Extracting substrings
 

I'm trying to teach myself bash but I'm stumped on this and I hope some kind soul can help me out. I can't figure out how to extract just the file names that have the extension ".txt" from strings like these.

FILES='hoover.dat mon.txt prev.dat fin.txt do.txt'
FILES='hoover.dat cal.dat mon.txt prev.dat fin.txt binding.old do.txt'

The "FILES=" is part of the string. The number and sequence of the file names vary, they are always enclosed in single quotes, and they are separated by a single space.

I have tried every combination of the bash string operations from here http://tldp.org/LDP/abs/html/refcards.html#AEN22098 that I can think of but I can't get it.

Thanks for your attention
2byte

_________________

Back to top
View user's profile Send private message 
Pizzasgood


Joined: 04 May 2005
Posts: 6270
Location: Knoxville, TN, USA

PostPosted: Fri 27 Nov 2009, 19:28    Post subject:  

If you're willing to cheat and use things besides pure bash, you can do this:
Code:
# STRING="FILES='hoover.dat mon.txt prev.dat fin.txt do.txt'"
# echo $STRING | tr " '" '\n' | grep "\.txt$"
mon.txt
fin.txt
do.txt

Otherwise I suppose you could do it the hard way by locating the position of the first ' and the first space. Then extract the text between them. Locate the final . and look at the text after it and make sure it equals txt. If so, keep the string, otherwise chuck it. Locate the position of the next space in the original string and repeat until you run out of spaces. After the final space, look between the last space and the final '.

_________________
Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib

Back to top
View user's profile Send private message Visit poster's website 
2byte

Joined: 09 Oct 2006
Posts: 357

PostPosted: Fri 27 Nov 2009, 21:15    Post subject:  

Thank you so much. I think I actually deciphered that code Smile
$STRING | tr " '" '\n' | grep "\.txt$"
STRING is piped to tr where the space and ' characters are translated into newline characters, then it's piped to grep and grep returns each line that contains ".txt". Correct?

One more question, if you don't mind. What is the signifigance of the escaped dot and $ in your pattern for grep? Playing with the code, grep ".txt", returns the same output.

Code:
#!/bin/bash
STRING="FILES='hoover.2fs mon.txt prev.dat fin.txt do.txt'"

array="`expr "$STRING" | tr " '" '\n' | grep ".txt"`"
echo $array

for i in ${array[*]}; do
   echo ${i}
done

#mon.txt fin.txt do.txt
#mon.txt
#fin.txt
#do.txt


Thanks again, and I hope you had a nice supper Smile
.

_________________

Back to top
View user's profile Send private message 
Pizzasgood


Joined: 04 May 2005
Posts: 6270
Location: Knoxville, TN, USA

PostPosted: Sat 28 Nov 2009, 01:18    Post subject:  

The trailing $ stands for "end of line". It ensures that grep only returns filenames that end in .txt (as opposed to things like file.txt.log.gz).

As for the dot, it's because grep considers dot to be a wild card that stands for any single character. So a naked dot would let the pattern match files like file.blatxt. So I escaped it so that it would be treated as a literal period.

Grep uses regular expressions. Really useful things. (Sed and Awk also use regex, as does Perl.)

_________________
Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib

Back to top
View user's profile Send private message Visit poster's website 
amigo

Joined: 02 Apr 2007
Posts: 2226

PostPosted: Sat 28 Nov 2009, 05:23    Post subject:  

Bash can emulate simple grepping using 'case':

Code:
#!/bin/bash

FILES='hoover.dat mon.txt prev.dat fin.txt do.txt'

for FILE in $FILES ; do
   case $FILE in
      *.txt) echo $FILE ;;
   esac
   # this also works but is less accurate:
   if [[ $FILE =~ '.txt' ]] ; then
      echo $FILE
   fi
done
Back to top
View user's profile Send private message 
2byte

Joined: 09 Oct 2006
Posts: 357

PostPosted: Sat 28 Nov 2009, 11:31    Post subject:  

Wow. So much good information in just a couple of posts. That's a great link for regex, thanks Pizzasgood. Maybe now I can make sense of the regex Hieroglyphics I see in so many scripts.

Thank you too Amigo. Your example actually makes more sense to me since I learned to program with basic so many years ago. I almost think that's a disadvantage. When it comes to programming I tend to think in basic, and was looking for the equivalents to instr, mid$, left$, right$, etc. It's hard to unlearn.
.

_________________

Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2226

PostPosted: Sat 28 Nov 2009, 12:49    Post subject:  

I like doing things in 'pure shell' when it's possible and not too fancy or messy looking. In some cases it will run faster than calling external programs to do the job because of latency issues(program startup time). For really large or complex jobs you are usually better of using a separate tool though.
Back to top
View user's profile Send private message 
efiguy


Joined: 06 Sep 2006
Posts: 169

PostPosted: Sat 28 Nov 2009, 21:53    Post subject: How to manipulate strings with bash?
Subject description: Bash, Regex, Grep
 

Hello all,

This is very interesting, especially for some graphic programs that renumber (and may add to an existing name) images in the generation of thumbnails, and for use with other camera format naming.

Are the examples above the "heart" of the sort mechanism, there must be a lot more of unstated code involved to deal with eventual output?

Curiosity, trying to learn by actual doing, is bash and/or regex
capable of performing tasks like these:

a) How is the code script used?, is it placed within a file located in the directory of all the associated "file types" in order to be run?

b) does it seperate the "txt" file from the others only displaying just those files?

c) does it generate a directory list?

d) could it create directories, copy and move files into those directories?

e) could it create a list to be used in "href" tag production for indexes?

It's OK to refer me to other sites as a "newbie" have bookmarked the Regex link <;)

Thanks all
Jay
Back to top
View user's profile Send private message 
2byte

Joined: 09 Oct 2006
Posts: 357

PostPosted: Sun 29 Nov 2009, 14:15    Post subject:  

efiguy,
Quote:
a) How is the code script used?, is it placed within a file located in the directory of all the associated "file types" in order to be run?

It was just an exercise to get a grip on bash string handling, in preparation for a utility I want to write. To answer the rest of your questions, yes, bash could be used to do all of those things.

Speaking for myself, the basic bash commands are not the real difficulty. Executing the commands and putting the desired results into variables and working with those is the hard part. In other words, writing a script Smile. Here's a few links that are proving to be helpful.

An A-Z Index of the Bash command line for Linux.
http://ss64.com/bash/
Bash Guide for Beginners
http://tille.garrels.be/training/bash/index.html
Linux Shell Scripting Tutorial
http://steve-parker.org/sh/intro.shtml
.

_________________

Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2226

PostPosted: Sun 29 Nov 2009, 15:48    Post subject:  

To make something really useful out of that code example, you'd need to substitute the input data with some useful data. For instace, to filter all the files and subdirs in the current directory, you could use:
Code:
#!/bin/bash

for FILE in * ; do
   case $FILE in
      *.txt) echo $FILE ;;
   esac
   # this also works but is less accurate:
   if [[ $FILE =~ '.txt' ]] ; then
      echo $FILE
   fi
done


And to do something useful with the filtered items, you'd use some command besides(or in addition to the 'echo $FILE' command.
Back to top
View user's profile Send private message 
efiguy


Joined: 06 Sep 2006
Posts: 169

PostPosted: Sun 29 Nov 2009, 20:56    Post subject: How to manipulate strings with bash?
Subject description: bash links
 

Thank you, those are spectacular info sites, I have a lot to explore !!
jay
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5010
Location: Arizona, U.S.A.

PostPosted: Sun 29 Nov 2009, 23:44    Post subject:  

These guys are great aren`t they efiguy?

This is where I started learning Bash scripting 4 years ago!
Back to top
View user's profile Send private message 
shaily
Guest


PostPosted: Fri 18 Dec 2009, 03:09    Post subject:  

Hi,
In order to facilitate opening my programming projects in vim, I have made a little bash script which starts in my projects directory, asks for a filename and then runs vim, to avoid having to cd all the way to it every time. The only thing I haven't been able to script is resizing the window to a more programming-friendly size.As I don't know much about bash scripting I need your help.
Thanks.
Back to top
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Fri 18 Dec 2009, 11:24    Post subject:  

shaily wrote:
Hi,
In order to facilitate opening my programming projects in vim, I have made a little bash script which starts in my projects directory, asks for a filename and then runs vim, to avoid having to cd all the way to it every time. The only thing I haven't been able to script is resizing the window to a more programming-friendly size.As I don't know much about bash scripting I need your help.
Thanks.


shaily,

You can control the window size by the "vimrc" file. You could put this in.
Code:
set lines=50 columns=100


or -
Code:
if has("gui_running")
  " GUI is running or is about to start.
  " Maximize gvim window.
  set lines=99999 columns=99999
else
  " This is console Vim.
  if exists("+lines")
    set lines=50
  endif
  if exists("+columns")
    set columns=100
  endif
endif


Probably some other ways that I don't know about as well Very Happy

cheers,
s
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [14 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0885s ][ Queries: 11 (0.0070s) ][ GZIP on ]