Working on files with names containing arbitrary indices in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Working on files with names containing arbitrary indices in shell
# 1  
Old 06-22-2012
Working on files with names containing arbitrary indices in shell

Hi,

I want to run a command in bash on files with names as file0001.txt, file0002.txt, file0003.txt and so on. The indices I have to work on are arbitrary like from file0009.txt to file0161.txt. So basically I want a string that can be updated from 0009 to 0010 to 0011 and so on. How can I do this?

I had to do something similar in g++. I did it in the following way:

Code:
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
       	ofstream f;
	char file[50];
	int size;
	string st1 = "file", ext = ".txt";
	string fname, filename;
	for(int i=10009; i<10161; i++)
	{
		stringstream ss;
		ss << i;
		fname = ss.str();
		filename = fname.substr(1,fname.length()-1);
		filename = st1 + filename + ext;
		size = filename.size();
		for(int j=0; j<=size; j++) 	file[j]=filename[j];
		f.open(file);
		...
	}
	return 0;
}

Can I do something analogous in shell?

Thanks in advance!

Last edited by methyl; 06-22-2012 at 09:16 AM.. Reason: Please use code tags
# 2  
Old 06-22-2012
Code:
[root@node2 ~]# bash --version | grep release
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
[root@node2 ~]# echo {0009..0161} | xargs -n 15
0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020 0021 0022 0023
0024 0025 0026 0027 0028 0029 0030 0031 0032 0033 0034 0035 0036 0037 0038
0039 0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 0050 0051 0052 0053
0054 0055 0056 0057 0058 0059 0060 0061 0062 0063 0064 0065 0066 0067 0068
0069 0070 0071 0072 0073 0074 0075 0076 0077 0078 0079 0080 0081 0082 0083
0084 0085 0086 0087 0088 0089 0090 0091 0092 0093 0094 0095 0096 0097 0098
0099 0100 0101 0102 0103 0104 0105 0106 0107 0108 0109 0110 0111 0112 0113
0114 0115 0116 0117 0118 0119 0120 0121 0122 0123 0124 0125 0126 0127 0128
0129 0130 0131 0132 0133 0134 0135 0136 0137 0138 0139 0140 0141 0142 0143
0144 0145 0146 0147 0148 0149 0150 0151 0152 0153 0154 0155 0156 0157 0158
0159 0160 0161

or
Code:
seq -w 9 0161 | xargs -n 15

the same output!
# 3  
Old 06-22-2012
Do you want to convert the numbers to a fixed-width format left-padded with zeroes? If yes, use something like this

Code:
printf "%04d" 9

# 4  
Old 06-22-2012
I'm sorry if I wasn't clear. I want a string. I want a string that one can somehow vary from file0009.txt to file0161.txt. I want to use this string in a lot of places.

Thanks!
# 5  
Old 06-22-2012
Code:
[root@node2 ~]# echo file{0009..0011}.txt
file0009.txt file0010.txt file0011.txt

# 6  
Old 06-22-2012
Code:
num=8;
while [ $((num+=1)) -le 20 ]
do
 printf "file%04d.txt\n" $num
done

Output:

Code:
file0009.txt
file0010.txt
file0011.txt
file0012.txt
file0013.txt
file0014.txt
file0015.txt
file0016.txt
file0017.txt
file0018.txt
file0019.txt
file0020.txt

This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 06-22-2012
Thanks elixir_sinari. That really helped.

Though a more exact code desired is:
Code:
num=8;
while [ $((num+=1)) -le 20 ]
do
  filename='printf "file%04d.txt\n" $num'
  echo $filename
done


Last edited by Franklin52; 06-22-2012 at 10:04 AM.. Reason: Please use code tags for data and code samples, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

List files of arbitrary depth according to a given pattern

I would like to list all the .c and .h files in the current directory or any of its subdirectories. I tried ls -R *.c *.h or ls -R | *.c *.h but that doesn't work. A related question : how to copy all the .c and .h files in the current directory or any of its subdirectories into another... (3 Replies)
Discussion started by: jakezkerrien
3 Replies

2. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

3. Red Hat

Name resolution is only working from server side. Clients cannot resolve host names.

Hi Folks, Could anyone please point me to the right direction as I have spent so much time on this without luck. :wall: I have installed Bind on my CentOS 5 server for internal network. The name resolution is working from the server itself only but not from the clients. --------------... (7 Replies)
Discussion started by: dbadmin100
7 Replies

4. UNIX for Dummies Questions & Answers

Working with file-names

Hello forum, I am new to shellscripting. My first goal is to write a script that verifies that for each file in one path there is an associated one in another path. The association is deviated by a distinct 'part' of the filename(s). Therefore I wanted to work with substitution. Here is... (3 Replies)
Discussion started by: danielandross
3 Replies

5. Shell Programming and Scripting

Finding indices in an array nearest to a set of values

I have an two arrays. One array BINDIST consists of fences. I have another array XOFFS. Eg BINDIST = 0 10 20 30 40 50 60 XOFFS = 2 3 4 23 25 28 55 58 I want to find to find the indices of values in XOFFS that are closest to each BINDIST. My idea is to do as follows I create array... (7 Replies)
Discussion started by: kristinu
7 Replies

6. Programming

Returning start/end indices

I have an array of distances and a len, for example len = 323 dist = I want to calculate the start and end index values around each distance in the array with a length of len from it. Values in dist are stored in ascending order. (4 Replies)
Discussion started by: kristinu
4 Replies

7. Shell Programming and Scripting

how to copy files followed by list of names of all the files in /etc?

....... (2 Replies)
Discussion started by: pcbuilder
2 Replies

8. UNIX for Dummies Questions & Answers

Working with folder names

I have the following directory structure: /maindir /maindir/product1/ /maindir/product1/type1 /maindir/product1/type2 /maindir/product1/type3 /maindir/product2/ /maindir/product2/type1 /maindir/product2/type2 /maindir/product2/type3 /maindir/product2/type4 ... I'm able to traverse... (6 Replies)
Discussion started by: ricksj
6 Replies

9. Shell Programming and Scripting

Max amount of awk array indices

Does anyone know what the max amount of indices you can store in a awk array? (0 Replies)
Discussion started by: timj123
0 Replies
Login or Register to Ask a Question