parsing a file name and add a number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing a file name and add a number
# 1  
Old 10-01-2010
parsing a file name and add a number

Hi,

I have a file po8282.fmt998.fbi.mopac.x.macs.btt.txt . I want to parse the first field which is separated by "." and then find the number and add 1 to it.

here is what I am doing to get the first field, but not sure how to only pick the number and add 1 to it
Code:
file_nm=po8282.fmt998.fbi.mopac.x.macs.btt.txt 

echo $file | cut -d'.' -f1  ( this is get me po8282 )
now I want to add 1 to 8282 and so it becomes 8283

my new file name will be
Code:
po8283.fmt998.fbi.mopac.x.macs.btt.txt

here are some scenarios
Code:
input: po8282.fmt998.fbi.mopac.x.macs.btt.txt 
output: po8283.fmt998.fbi.mopac.x.macs.btt.txt 

input: po828255.fmt998.fbi.mopac.x.macs.btt.txt 
output: po828256.fmt998.fbi.mopac.x.macs.btt.txt

can this be done by a single command if possible?

Thanks

Last edited by Franklin52; 10-01-2010 at 02:17 PM.. Reason: Correcting code tags
# 2  
Old 10-01-2010
Something like this?
Code:
awk -F\. '{n=match($1,"[0-9]");c=substr($1,n);$1=substr($1,1,n-1) ++c}1' OFS="."

# 3  
Old 10-01-2010
For fun of shell
bash code:
  1. NewName() { local A N; A=${1%%.*}; N=${A//[^0-9]/}; echo {A/$N/}$((N+1)).${1#*.}; }
  2. #
  3. # Test of the function:
  4. for F in po8282.fmt998.fbi.mopac.x.macs.btt.txt po828255.fmt998.fbi.mopac.x.macs.btt.txt
  5. do
  6.    echo "----------------------"
  7.    echo "$F"
  8.    echo "$(NewName $F)"
  9. done

Last edited by frans; 10-01-2010 at 02:57 PM.. Reason: local variables
# 4  
Old 10-01-2010
I am running the exact script and gives me the ouput as below.

----------------------
po8282.fmt998.fbi.mopac.x.macs.btt.txt
{A/8282/}8283.fmt998.fbi.mopac.x.macs.btt.txt
----------------------
po828255.fmt998.fbi.mopac.x.macs.btt.txt
{A/828255/}828256.fmt998.fbi.mopac.x.macs.btt.txt

Can you please help.
# 5  
Old 10-01-2010
The script works fine when recorded in a file but gives me the same strange output when pasted into a terminal.
-----------

Now works if line 1. (function) is
#
bash code:
  1. NewName() { A=${1%%.*}; N=${A//[^0-9]/}; echo "${A/${N}/}$((N+1)).${1#*.}"; }
Code:
echo "${A/${N}/}$((N+1)).${1#*.}"

This User Gave Thanks to frans For This Post:
# 6  
Old 10-01-2010
Code:
n="${file_nm%%.*}"
echo "${n%%${n##??}}$((${n##??}+1)).${file_nm#*.}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add number of fields throughout file together?

So, I have three problems that cover this subject. First one asks me to find the number of fields in the file that contain the substring "he". I found the number of fields, but the problem I have is that they are displaying by each record. I want to add all of the records' fields together. With... (2 Replies)
Discussion started by: mc10
2 Replies

2. Shell Programming and Scripting

Parsing log file and print latest number in loop

Hello All, I have an awk script which parses my log file and prints number grepping from a specific line/pattern, now i have to come with a shell script to continue reading the log untill the job is completed, which i would know while reading session log untill process encounters a final... (1 Reply)
Discussion started by: Ariean
1 Replies

3. Shell Programming and Scripting

Replace first number of each line in a file with another number

Hi, I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@, for file in... (13 Replies)
Discussion started by: scorpioraghu
13 Replies

4. Shell Programming and Scripting

parsing characters and number from a big file with brackets

I have a big file with many brackets () in it from which I need to parse number characters and numbers. Below is an example of my file 14 (((A__0:0.02,B__1:0.3)0:0.04,C__0:0.025)2:0.01),(D__0:0.00978,E__2:0.01031)1:0.00362; 15... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

5. Shell Programming and Scripting

add serial number begining of each line in a text file

Dear All, i am having text file below rama 123 done raju 456 not done keshav 123 done ............... ............ i want to add a serial number to this file, the output should be 1 rama 123 done 2 raju 456 not done 3 keshav 123 done 99 ............... 100 ............ ... (3 Replies)
Discussion started by: suryanarayana
3 Replies

6. Shell Programming and Scripting

Help with text/number parsing

Hello I have a file that contains 10 rows as below: "ID" "DP" "ID=GRMZM2G015073_T01" "23.6044288292005" "ID=GRMZM2G119852_T01" "59.7782287606723" "ID=GRMZM2G100242_T02" "61.4167813736184" "ID=GRMZM2G046274_T01" "6.63061838134219" "ID=GRMZM2G046274_T02" ... (5 Replies)
Discussion started by: cs_novice
5 Replies

7. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

8. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

9. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

10. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies
Login or Register to Ask a Question