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.

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

Thank you so much. I think I actually deciphered that code
﻿$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.

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

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.
._________________﻿

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.

﻿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 . Here's a few links that are proving to be helpful.

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.

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.

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

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