Sponsored Content
Top Forums Shell Programming and Scripting Working on files with names containing arbitrary indices in shell Post 302660193 by KidD312 on Friday 22nd of June 2012 08:05:59 AM
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
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

8. 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

9. 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
dos2unix(1)						      General Commands Manual						       dos2unix(1)

NAME
dos2unix - DOS/MAC to UNIX text file format converter SYNOPSYS
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...] Options: [-hkqV] [--help] [--keepdate] [--quiet] [--version] DESCRIPTION
This manual page documents dos2unix, the program that converts plain text files in DOS/MAC format to UNIX format. OPTIONS
The following options are available: -h --help Print online help. -k --keepdate Keep the date stamp of output file same as input file. -q --quiet Quiet mode. Suppress all warning and messages. -V --version Prints version information. -c --convmode convmode Sets conversion mode. Simulates dos2unix under SunOS. -o --oldfile file ... Old file mode. Convert the file and write output to it. The program default to run in this mode. Wildcard names may be used. -n --newfile infile outfile ... New file mode. Convert the infile and write output to outfile. File names must be given in pairs and wildcard names should NOT be used or you WILL lost your files. EXAMPLES
Get input from stdin and write output to stdout. dos2unix Convert and replace a.txt. Convert and replace b.txt. dos2unix a.txt b.txt dos2unix -o a.txt b.txt Convert and replace a.txt in ASCII conversion mode. Convert and replace b.txt in ISO conversion mode. Convert c.txt from Mac to Unix ascii format. dos2unix a.txt -c iso b.txt dos2unix -c ascii a.txt -c iso b.txt dos2unix -c mac a.txt b.txt Convert and replace a.txt while keeping original date stamp. dos2unix -k a.txt dos2unix -k -o a.txt Convert a.txt and write to e.txt. dos2unix -n a.txt e.txt Convert a.txt and write to e.txt, keep date stamp of e.txt same as a.txt. dos2unix -k -n a.txt e.txt Convert and replace a.txt. Convert b.txt and write to e.txt. dos2unix a.txt -n b.txt e.txt dos2unix -o a.txt -n b.txt e.txt Convert c.txt and write to e.txt. Convert and replace a.txt. Convert and replace b.txt. Convert d.txt and write to f.txt. dos2unix -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt DIAGNOSTICS
BUGS
The program does not work properly under MSDOS in stdio processing mode. If you know why is that so, please tell me. AUTHORS
Benjamin Lin - <blin@socs.uts.edu.au> Bernd Johannes Wuebben (mac2unix mode) <wuebben@kde.org> MISCELLANY
Tested environment: Linux 1.2.0 with GNU C 2.5.8 SunOS 4.1.3 with GNU C 2.6.3 MS-DOS 6.20 with Borland C++ 4.02 Suggestions and bug reports are welcome. SEE ALSO
unix2dos(1) mac2unix(1) 1995.03.31 dos2unix v3.0 dos2unix(1)
All times are GMT -4. The time now is 06:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy