marcin
marcin

Reputation: 67

how to increment the number in the filename

all the files in my folder are named like this:

aodv-0-0.pcap aodv-1-0.pcap aodv-2-0.pcap aodv-3-0.pcap

etc

Is there any chance to rename them so as they become:

1.pcap 2.pcap 3.pcap 4.pcap

aodv-0-0 ------> 1 aodv-1-0 ------> 2 etc

i tried using "rename" but i don't know how to increment the number in the filename.

#RENAMING the files

printf "...RENAMING the files to get proper a nodeID...\n"

cd /home/marcin/workspace/bake/source/ns-3.19/RESULTS/TRAFFIC_LIGHTS/
rename aodv- "" *.pcap

After using it i get:

aodv-0-0.pcap     --->  0-0.pcap  (and i need 1.pcap)
aodv-1-0.pcap     --->  1-0.pcap  (and i need 2.pcap)

and then maybe i could use the number(just the number) as a variable "nodeID". I used "basename" function to get the filename as a variable before but i don't need ".pcap" extension as a variable anymore.

Thanks in advance

Upvotes: 1

Views: 612

Answers (1)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14965

ls aodv* | sed -e "p;s/aodv-//" -e "s/-.//" |xargs -n2 mv

For counter increase:

ls aodv*|awk -F\. 'split($1,a,"-"){print $0,a[2]+1""FS""$2}'|xargs -n2 mv

The ls output is piped to sed , then we use the p flag to print the argument without modifications, in other words , the original name of the file .

The next step is use the substitute command to change file extension.

The result is a combined output that consist of a sequence of old_file_name -> new_file_name.

Finally we pipe the resulting feed through xargs to get the effective rename of the files.

http://nixtip.wordpress.com/2010/10/20/using-xargs-to-rename-multiple-files/

Upvotes: 1

Related Questions