How to grep a number in a file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep a number in a file name
# 1  
Old 12-11-2007
How to grep a number in a file name

Hi,

I have multiple files where it starts with test1.c, test2.c,test3.c and so on.

I would like to get each file separately to perform abstraction from these files.

I tried something like:-

for t in ./*
filenumber=${t:4} # to cut the "test" in order to get the number

cat test$filenumber.c| sed 's/^[ ]*//' >final.txt

However, in my folder, there is other potential file other than test1.c,test2.c...in other words, there are other files in the folder.

Is there other better way to grab the number so that each file can be catenate automatically?


Please advise. Thanks.


-Jason
# 2  
Old 12-11-2007
Not sure what you're trying to accomplish here. Your loop overwrites "final.txt" so that only the last testN.c is within it. If you're looking to combine:

Code:
for t in ./test*.c; do
  sed 's/^[ \t]*//' $t >> final.txt
done

# 3  
Old 12-11-2007
Hi,

Thanks for the prompt reply.

I used the concept you mentioned and basically it is printing out the file contents.

What that I am currently looking for is getting a number from the file name itself in a folder.
Means I have folder A where i have multiples file name; test1.c, test2.c, test3.c...and so on.

I would like to grep the number of 1,2,3,....so that I can open each of test$grepnumber.c to do sed operation and finally append to an output file.

$grepnumber refers to the number of each file name exist in folder A.

Hope this is much clearer than before.Thanks.
# 4  
Old 12-11-2007
Quote:
Originally Posted by ahjiefreak
I would like to grep the number of 1,2,3,....so that I can open each of test$grepnumber.c to do sed operation and finally append to an output file.
First off, it seems to me that you do not even need to extract the number from the filename if you are going to use the complete filename anyways. It would be sufficient to filter out all the files matching a certain name pattern, like "test, followed by a number and having a .c-extension".

There is a group of metacharacters (aka "wildcards"), that are interpreted by the shell: everybody knows "?" and "*", but there are others, which will exactly do what you intend to do: "[]" searches for a single character of the list of characters enclosed in the brackets, for instance: "[0-9]" will find every single digit number.

To search and find only the files you want to find use an expression like "test[0-9].c". If you need the (more powerful) regexps of sed to prepare the list capture the output of "ls" in a pipeline and use sed to work on it. Use a while-loop to further do some work on the files found this way, the following is an example:

Code:
The shell way:
ls -1 /path/to/dir/test[0-9].c| while read fIn ; do
     iNumber="$(print - "$fIn" | sed 's/^test//;s/.c//')"
     print - "Files name is: $fIn"
     print - ".... and its number is $iNumber"
done

...and the regexp way:
ls -1 /path/to/dir | sed -n '/^test[0-9][0-9]*\.c/ p' | while read fIn ; do
     iNumber="$(print - "$fIn" | sed 's/^test//;s/.c//')"
     print - "Files name is: $fIn"
     print - ".... and its number is $iNumber"
done

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

2. UNIX for Dummies Questions & Answers

Grep SQL output file for greater than number.

Hi, This is my first post. I have a korn shell script which outputs a select statment to a file. There is only one column and one row which contains a record count of the select statement. The select statement looks something like this: SELECT COUNT(some_field) AS "count_value" ... (2 Replies)
Discussion started by: MurdocUK
2 Replies

3. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

4. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

5. Shell Programming and Scripting

Grep for the most negative number

Hello, I am trying to write a bash script which will give me the most negative number. Here is an example input: Ce 3.7729752124 -4.9505731588 -4.1061257680 Ce -6.9156611391 -0.5991784762 7.3051893138 Ce 7.6489739875 0.3513020731 ... (6 Replies)
Discussion started by: sdl27789
6 Replies

6. Shell Programming and Scripting

to parse (or grep) a number from a datafile and write it to tab limited file

Hi All, I have a folder that contain 100's of subfolders namely: Main folder -> GHFG - Subfoders ->10 100 234 102 345 .. .. ... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

7. Shell Programming and Scripting

grep the number of self process

Dear All, I plan to write a script and in the beginning, I will check if there's any existing previous process running, if YEs, then exit directly, if Not, then continue. #!/bin/bash NUM=`ps -ef | grep $0 | grep -v grep | wc -l` ps -ef | grep $0 | grep -v grep echo "NUM ==> $NUM" if... (6 Replies)
Discussion started by: tiger2000
6 Replies

8. UNIX for Dummies Questions & Answers

grep the number of first occurence

File1.txt ....... ....... OMC LA OMC LK OMC LS ........ ........ Above is the content of File1.txt, i want to get the Number of Occurence to order, lets say if OMC LA = 1, OMC LS=3, and OMC LK=2.. omc_ident="OMC LA" or "OMC LK" or "OMC LS" omc_num=`grep '^OMC' File1.txt| grep... (4 Replies)
Discussion started by: neruppu
4 Replies

9. UNIX for Dummies Questions & Answers

using Grep for only a certain number of columns

I am given a file with a list of names for the first 20 characters. The 20 characters after is a list of names that somebody is supposed to team up with. Now i'm trying to write a simple script that will read a name somebody inputs and then looks only in the first 20 characters. For example.... (2 Replies)
Discussion started by: MaestroRage
2 Replies

10. Shell Programming and Scripting

How to grep a number in a file to find them in another file

Hi, I tried to simulate some example in Shell particularly finding item and matching in another file. A very simple example:- Basically I have 2 files, file A and file B. In file A, I have columns of fields such that:- aaa 107 bbb 108 ccc 109 In file B, I have columns of fields... (7 Replies)
Discussion started by: ahjiefreak
7 Replies
Login or Register to Ask a Question