File name offset


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers File name offset
# 8  
Old 11-18-2014
Don't strip the leading zeroes
Code:
off=${1:-30}
ls -r ANI_WFMASS_PIST*.gif |
while IFS='T.' read -r f num t
do
  new=$(printf '%05d' "$((10#$num + off))")
  echo mv "${f}T$num.$t" "${f}T$new.$t"
done


Last edited by MadeInGermany; 11-18-2014 at 04:42 AM.. Reason: 10#$ to force a base 10 number
This User Gave Thanks to MadeInGermany For This Post:
# 9  
Old 11-18-2014
Thank you MadeInGermany

I am getting some mistake in 10 th and 11th file.

mv ANI_WFMASS_PIST00016.gif ANI_WFMASS_PIST00044.gif
mv ANI_WFMASS_PIST00015.gif ANI_WFMASS_PIST00043.gif
mv ANI_WFMASS_PIST00014.gif ANI_WFMASS_PIST00042.gif
mv ANI_WFMASS_PIST00013.gif ANI_WFMASS_PIST00041.gif
mv ANI_WFMASS_PIST00012.gif ANI_WFMASS_PIST00040.gif
mv ANI_WFMASS_PIST00011.gif ANI_WFMASS_PIST00039.gif
mv ANI_WFMASS_PIST00010.gif ANI_WFMASS_PIST00038.gif
mv ANI_WFMASS_PIST00009.gif ANI_WFMASS_PIST00039.gif
mv ANI_WFMASS_PIST00008.gif ANI_WFMASS_PIST00038.gif
mv ANI_WFMASS_PIST00007.gif ANI_WFMASS_PIST00037.gif
mv ANI_WFMASS_PIST00006.gif ANI_WFMASS_PIST00036.gif
mv ANI_WFMASS_PIST00005.gif ANI_WFMASS_PIST00035.gif
mv ANI_WFMASS_PIST00004.gif ANI_WFMASS_PIST00034.gif
mv ANI_WFMASS_PIST00003.gif ANI_WFMASS_PIST00033.gif
mv ANI_WFMASS_PIST00002.gif ANI_WFMASS_PIST00032.gif
mv ANI_WFMASS_PIST00001.gif ANI_WFMASS_PIST00031.gif
# 10  
Old 11-18-2014
Quote:
Originally Posted by MadeInGermany
Don't strip the leading zeroes
Code:
off=${1:-30}
ls -r ANI_WFMASS_PIST*.gif |
while IFS='T.' read -r f num t
do
  new=$(printf '%05d' "$((num + off))")
  echo mv "${f}T$num.$t" "${f}T$new.$t"
done

Good idea. It works just fine with ksh. But note that it fails with bash if there are any leading 0s since bash will interpret a numeric string starting with 0 as octal instead of decimal.

If the OP is not willing to use ksh, we need to use a loop to strip leading 0s, use non-standard parameter expansions available in some shells that use REs instead of filename patterns, or use something like awk to process the ls output instead of the shell.

The following should work with any POSIX conforming shell:
Code:
off=${1:-30}
ls -r ANI_WFMASS_PIST*.gif | while IFS='T.' read -r f num t
do	# Strip leading zeroes from old number...
	if [ "$num" = "00000" ]
	then	old=0
	else	old="$num"
		while [ "$old" != ${old#0} ]
		do	old="${old#0}"
		done
	fi
	# Comput new number...
	new=$(printf '%05d' "$((old + off))")
	echo mv "${f}T$num.$t" "${f}T$new.$t"
done

And, again, if this produces the correct mvcommands, remove the echo shown in red.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 11-18-2014
Quote:
Originally Posted by kri321shna
Thank you MadeInGermany

I am getting some mistake in 10 th and 11th file.

mv ANI_WFMASS_PIST00016.gif ANI_WFMASS_PIST00044.gif
mv ANI_WFMASS_PIST00015.gif ANI_WFMASS_PIST00043.gif
mv ANI_WFMASS_PIST00014.gif ANI_WFMASS_PIST00042.gif
mv ANI_WFMASS_PIST00013.gif ANI_WFMASS_PIST00041.gif
mv ANI_WFMASS_PIST00012.gif ANI_WFMASS_PIST00040.gif
mv ANI_WFMASS_PIST00011.gif ANI_WFMASS_PIST00039.gif
mv ANI_WFMASS_PIST00010.gif ANI_WFMASS_PIST00038.gif
mv ANI_WFMASS_PIST00009.gif ANI_WFMASS_PIST00039.gif
mv ANI_WFMASS_PIST00008.gif ANI_WFMASS_PIST00038.gif
mv ANI_WFMASS_PIST00007.gif ANI_WFMASS_PIST00037.gif
mv ANI_WFMASS_PIST00006.gif ANI_WFMASS_PIST00036.gif
mv ANI_WFMASS_PIST00005.gif ANI_WFMASS_PIST00035.gif
mv ANI_WFMASS_PIST00004.gif ANI_WFMASS_PIST00034.gif
mv ANI_WFMASS_PIST00003.gif ANI_WFMASS_PIST00033.gif
mv ANI_WFMASS_PIST00002.gif ANI_WFMASS_PIST00032.gif
mv ANI_WFMASS_PIST00001.gif ANI_WFMASS_PIST00031.gif
It isn't just 10 and 11 that have a problem. Note that 12 through 16 are converted to 40 through 44 instead of 42 through 46, respectively.
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 11-18-2014
Thank you somuch Cragun.

It is working fine.


Smilie
# 13  
Old 11-18-2014
Some shells interpret num as octal when there is a leading zero.
But obviously 10#num gives syntax error.
I have corrected my post to 10#$num.
Works in any Posix shell including ksh,bash,zsh.
This User Gave Thanks to MadeInGermany For This Post:
# 14  
Old 11-18-2014
Quote:
Originally Posted by MadeInGermany
Some shells interpret num as octal when there is a leading zero.
But obviously 10#num gives syntax error.
I have corrected my post to 10#$num.
Works in any Posix shell including ksh,bash,zsh.
Although $((10#$num + $off)) does what we want for this script in bash, ksh, and zsh, the recognition of 10# (and 8# and 16#) is not mentioned in the POSIX standards; so there is no guarantee that it will do what we want for this in any other POSIX-conforming shells.
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep --byte-offset not returning the offset (Grep version 2.5.1)

Hi, I am trying to get the position of a repeated string in a line using grep -b -o "pattern" In my server I am using GNU grep version 2.14 and the code is working fine. However when I am deploying the same code in a different server which is using GNU grep version 2.5.1 the code is not... (3 Replies)
Discussion started by: Subhamoy
3 Replies

2. Shell Programming and Scripting

Get Compressed byte offset from .gz file

Hi , I have a .gz file whose contents look like below. data1^filename1 data2^filename2. .. . . Is it possible to find out the byte offset of each record from the .gz file. Like in an uncompressed file. grep -nb "Filename" give the byte offset of the record in this case. ... (4 Replies)
Discussion started by: chetan.c
4 Replies

3. UNIX for Dummies Questions & Answers

Tail with positive offset

I have read the below from the book bash cookbook.Tail +1 filenames is similar to cat filename I have tried the same in Ubuntu 11.10 with bash. 4.0 . I have received error for the Same. May I know in which system that will work fine ? Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

4. Solaris

NTP client offset

How to add offset to NTP client so that, for eg., clock is -20 seconds? (2 Replies)
Discussion started by: orange47
2 Replies

5. Shell Programming and Scripting

Moving bytes in file by offset and length

Hi, I'm looking for a way (other than C) to pull out a number of bytes in a Linux file for a giving length. for example: file1 contains 2 records: abcdefghijkl mnopqrstuv ..... so, I want to pull starting in byte 9 for a length of 8 file2 would contain: ijkmnopq Thanks (2 Replies)
Discussion started by: jbt828
2 Replies

6. Shell Programming and Scripting

increment a value at an offset in hundreds very large hex file

I have a lot of very large hex files that I need to change one value at the same offset and save to another file. I have a script that finds each file and just need to put an operator for each file. I think sed might be able to do this but I have not used it before and would like some help. If... (8 Replies)
Discussion started by: Eruditass
8 Replies

7. Programming

Negative Offset

Function: int fcntl(int fd, int cmd, struct flock * lock) Data Type: struct flock This structure is used with the fcntl function to describe a file lock. It has these members: off_t l_start This specifies the offset of the start of the region to which the lock applies, and... (1 Reply)
Discussion started by: DNAx86
1 Replies

8. UNIX for Dummies Questions & Answers

Reading a file from a specified offset

Hi, I want to read a file from a specified offset from the start of file. With the read command, is it possible to do so. Please suggest. Is there any other alternative? Thanks, Saurabh (2 Replies)
Discussion started by: saurabhsinha23
2 Replies

9. UNIX for Advanced & Expert Users

Script to Extract the line from File with specified offset

Hi All, I need to extract only XML details from large log file which may contain other unwanted junk details. For example, our xml will be start as <OUTBOUND_MESSAGE .....> and ends with </OUTBOUND_MESSAGE>. I want to extract only lines between these start and end tag (Including these tags)... (5 Replies)
Discussion started by: thinakarmani
5 Replies

10. UNIX for Dummies Questions & Answers

offset - informix chunk

Hello all, I am trying to add chunks to my informix dataspace. I have one dataspace ( the rootdbs ) and the new chunk is a raw device. Precisely slice1 on my new external harddisk. The question is, what should be the offset value. The document says, the offset is used by the engine to... (1 Reply)
Discussion started by: shibz
1 Replies
Login or Register to Ask a Question