Use wildcards in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use wildcards in a script
# 8  
Old 03-06-2008
Using wildcards to view multiple files

hi, i have a similar question,

i would like to view multiple files using cat, but i don't know the correct command to use.

example:
i have the following files
file12345
file12346
file12347
file12348
and so on up to....
file43455


is there a one-line command that can cat all these files?

i know about wildcards, but what i'm thinking is to issue the following commands:

Code:
cat file1234[5-9]
cat file123[4-9]?
cat file12[4-9]??

and so on.. but these are a set of commands that does the cat'ing part by part... can i cat all those files using a one-line command??

thanks for helping
# 9  
Old 03-07-2008
Quote:
Originally Posted by riderandy
hi, i have a similar question,

i would like to view multiple files using cat, but i don't know the correct command to use.

example:
i have the following files
file12345
file12346
file12347
file12348
and so on up to....
file43455


is there a one-line command that can cat all these files?

i know about wildcards, but what i'm thinking is to issue the following commands:

Code:
cat file1234[5-9]
cat file123[4-9]?
cat file12[4-9]??

and so on.. but these are a set of commands that does the cat'ing part by part... can i cat all those files using a one-line command??

Depending on what you want:
Code:
cat file*

Or:
Code:
cat file[0-9][0-9][0-9][0-9]

To see which files will be matched by a pattern without running any command on them, use:
Code:
printf "%s\n" PATTERN
## e.g.: printf "%s\n" file*

# 10  
Old 03-07-2008
Quote:
Originally Posted by cfajohnson

Depending on what you want:
Code:
cat file*

Or:
Code:
cat file[0-9][0-9][0-9][0-9]

To see which files will be matched by a pattern without running any command on them, use:
Code:
printf "%s\n" PATTERN
## e.g.: printf "%s\n" file*

yes, im aware of that, but my concern is that i would like to have a one-line command that lists files file12345-file34224. I know that I can't just issue the command

Code:
cat file[1-3][2-4][3-2][4-5]

because it will not include all other files i want. i have a set of files with sequence numbers but i just want to display a portion of it using a one line command.

if there is an alternative way to do this?

to make it short, i have a set of files from file00000 up to file99999. I just want to display files from file12345-file34224. if anyone can give a one line command for it, i can take it from that example.
# 11  
Old 03-07-2008
Quote:
Originally Posted by riderandy
yes, im aware of that, but my concern is that i would like to have a one-line command that lists files file12345-file34224. I know that I can't just issue the command

Code:
cat file[1-3][2-4][3-2][4-5]

because it will not include all other files i want. i have a set of files with sequence numbers but i just want to display a portion of it using a one line command.

if there is an alternative way to do this?

What files do you want?

The patterns I posted will give you all the files
a) that begin with "file" and
b) all the files that are the word "file" followed by 4 digits.

What more do you want?
# 12  
Old 03-07-2008
Quote:
Originally Posted by cfajohnson

What files do you want?

The patterns I posted will give you all the files
a) that begin with "file" and
b) all the files that are the word "file" followed by 4 digits.

What more do you want?
i have a set of files from file00000 up to file99999. I want to display files from file12345-file34224.

can you please provide a one line command for it, so that i can take the logic from your answer?
# 13  
Old 03-07-2008
Quote:
Originally Posted by riderandy
i have a set of files from file00000 up to file99999. I want to display files from file12345-file34224.

can you please provide a one line command for it, so that i can take the logic from your answer?
Well, there is nothing wrong with a loop on the commandline:

Code:
filenr=12345;while [ $filenr -le 34224 ] ; do cat file$filenr ; (( filenr += 1 )) ; done

You could of course also write a little script:

Code:
#! /bin/ksh

typeset -i start=$1
typeset -i end=$2
typeset -i counter=$start

while [ $counter -le end ] ; do
     if [ -r file$counter ] ; do
          cat file$counter
     fi
     (( counter += 1 ))
done

exit 0

Save this as "auto-cat.sh", give it execute rights and use it like "auto-cat.sh 1 5" to display file1, file2, ...., file5

bakunin

Last edited by bakunin; 03-07-2008 at 03:43 AM..
# 14  
Old 03-07-2008
thanks bakunin and cfajohnson!

i wasn't aware that we can issue a set of looping commands using delimiter ;

great help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wildcards in file input to a script?

I have four files: test test2 test3 test4 I have this simple script: #!/bin/bash ls $1 Why does ./the_script.sh test* only list the first file, when a normal ls test* would list all four? What do I need to change in the script to be able to use wildcard? (12 Replies)
Discussion started by: KidCactus
12 Replies

2. Shell Programming and Scripting

SH Script for file name wildcards

Does anyone know how I would go about inserting text at the beginning of a file with the file name containing a daily time stamp? Essentially I need to find the file name using a wild card, and then insert 3 lines of text - one of which is the processing date. Help please!? (1 Reply)
Discussion started by: cookie33
1 Replies

3. UNIX for Advanced & Expert Users

Wildcards

These 2 websites do a GREAT job of explaining different types of wildcards. I learned about the categories of characters which I never knew about at all. GNU/Linux Command-Line Tools Guide - Wildcards GREP (1 Reply)
Discussion started by: cokedude
1 Replies

4. UNIX for Dummies Questions & Answers

Running script on SFTP server, RMDIR and RM with wildcards

Im going insane trying to figure out what i consider a basic command on an SFTP server... Im trying to download all files from a directory *done* then remove all the files (and sometimes folders that contain files) i have downloaded on the remote directory... the command i would normally... (2 Replies)
Discussion started by: mokachoka
2 Replies

5. Shell Programming and Scripting

Perl script to search and extract using wildcards.

Good evening All, I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. ... (3 Replies)
Discussion started by: CammyD
3 Replies

6. UNIX for Dummies Questions & Answers

script for deletion using wildcards

find ./ -name t\* | sed "s@^./@@" > .filestobedeleted j=$(wc -l < .filestobedeleted) typeset -i cnt=0 typeset -i i=0 while read line do myarray=$line ((cnt = cnt + 1)) done < .filestobedeleted while (1 Reply)
Discussion started by: aishu
1 Replies

7. UNIX for Dummies Questions & Answers

wildcards NOT

Hi All Please excuse another straightforward question. When creating a tar archive from a directory I am attempting to use wildcards to eliminate certain filetypes (otherwise the archive gets too large). So I am looking for something along these lines. tar -cf archive.tar * <minus all *.rst... (5 Replies)
Discussion started by: C3000
5 Replies

8. UNIX for Dummies Questions & Answers

ls with wildcards

ok, I'm trying to write a script file that lists files with specific elements in the name into a txt file, it looks like this ls s*.dat > file_names.txt can't figure out whats wrong with that line, any ideas? thanks in advance (10 Replies)
Discussion started by: benu302000
10 Replies

9. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies

10. UNIX for Dummies Questions & Answers

Wildcards in VI

I'm trying to delete lines from a large text file using VI. Every line that I am wanting to delete start with 'S' - all others do not. (A list of users) I've tried using * but doesn't seem to like it...any ideas... Doesn't have to be VI - but I'm better with VI than sed/awk. (8 Replies)
Discussion started by: peter.herlihy
8 Replies
Login or Register to Ask a Question