LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 12-25-2011, 02:30 AM   #1
rpd25
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Rep: Reputation: Disabled
Finding max number in filename and opening it


Hi,

I have files named as

energy.dat.1
energy.dat.2
energy.dat.3
...
energy.dat.2342

I would like to find the file with maximum number in the filename (ex. energy.dat.2342), select it and read the numeric content in scilab/octave/python for plotting.

Would you please share your expertize in writing the script?

Thanks in advance.
 
Old 12-25-2011, 05:25 AM   #2
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Lightbulb

This will give you the file count so a s long as you know the starting # and the naming convention is consisten then you will know the last file name.



Code:
find /path/to/where/you/want_to/look/ -iname *.dat.*[0-9] | wc -l
Hope this Helps.
 
Old 12-25-2011, 07:49 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Two alternatives using numerical sorting and numerical comparison respectively:
Code:
ls energy.dat.* | sort -t. -k3n | tail -1
Code:
ls energy.dat.* | awk -F. '{if ( num<$3 ){num = $3; file = $0}}END{print file}'
 
1 members found this post helpful.
Old 12-25-2011, 08:06 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Another alternative:
Code:
ls -1v | tail -1
That's a 1 (one) not an l (el).

Hope this helps.
 
1 members found this post helpful.
Old 12-25-2011, 08:13 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nice one, druuna! I didn't know about the -v option.
 
Old 12-25-2011, 08:53 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by colucix View Post
Nice one, druuna! I didn't know about the -v option.
I have my moments
 
Old 12-25-2011, 09:44 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As a in-shell solution, you can loop through filenames, and search and print out the one with the largest number. In bash:

Code:
last=0
for file in energy.dat.[0-9]* ; do
	(( ${file//[^0-9]} > ${last//[^0-9]} )) && last=$file
done

echo "$last"
This example assumes that there's only a single set of non-zero-padded* digits in each files, as ${file//[^0-9]} simply removes everything else. But it should be easily modifiable for other patterns.

(*most shells treat integers with leading zeros as octal values when doing arithmetic operations. )

Speaking of zero-padding, the real best way to handle it, in my opinion, is to simply make sure all the numbers are zero-padded to the same number of digits first. Then the shell will sort them for you automatically, and you can simply use whichever technique you want to grab the last value in the list.

Add all the names to an array, for example, and use the final entry.

Code:
files=( energy.dat.[0-9]* )

echo "${files[-1]}"	#bash 4.2+ allows negative indexing
echo "${files[@]: -1}"	#for earlier versions
I wrote this script a while ago that zero-pads numbers in file names. You can give it a try.

Code:
#!/bin/bash

# Pads numbers in file names if found.
# See help message for more.

# Set the environment
shopt -s extglob
IFS=""

BCYAN="${BCYAN:-\e[1;36m}"    #Define some color codes, for prettified output.
#BGREEN="${BGREEN:-\e[1;32m}"  #Use environment defaults if they exist.
#BRED="${BRED:-\e[1;31m}"
#BBLUE="${BBLUE:-\e[1;34m}"
#BMAGENTA="${BMAGENTA:-\e[1;35m}"
RESET="${RESET:-\e[0m}"

#set default padding level
pad=2


# Set up the help dialog
help+=( '' 																				)
help+=( '\tA quick script to zero-pad files that contain numbers.' 									)
help+=( '\tIt will only pad the first number string it finds in the name, and ignores files without numbers.'	)
help+=( '' 																				)
help+=( "\tUsage: \t${BCYAN}${0##*/} [-n <num>] <files>${RESET}"										)
help+=( "\t\t${BCYAN}${0##*/} -h${RESET}" 														)
help+=( '' 																				)
help+=( '\tUse -n to specify the number of digits to pad, from 1-9.  Defaults to '"$pad"' if not used.'		)
help+=( '\tIf no files are given, it processes the current directory.' 								)
help+=( '' 																				)


# Process input options
# If "-h" print help & exit.
# If "-n" test for valid inputs and set "pad" variable
# Ignore anything else
while getopts ":hn:" opt; do

     case "$opt" in

          h) IFS=$'\n'
		   echo -e "${help[*]}" >&2
             exit "2"
          ;;

          n)	if [[ "$OPTARG" =~ [^[:digit:]] ]] || (( "10#$OPTARG" < 1 )) || (( "10#$OPTARG" > 9 )); then
				echo
				echo -e "${BCYAN}invalid option: [$OPTARG].${RESET}" >&2
				echo -e "${BCYAN}-n must be an integer from 1 to 9${RESET}" >&2
				echo -e "${BCYAN}Falling back to the default of $pad${RESET}"
				echo
			else
				pad="$(( 10#$OPTARG ))"
			fi
          ;;

          \?) echo -e "${BCYAN}Invalid option: [-$OPTARG].  Ignoring.${RESET}" >&2
          ;;
     esac
done

shift $(( OPTIND - 1 )) ; OPTIND=1

# Now test for files.
# If nothing given, set input parameters to files in current directory.
if [[ -z "$*" ]]; then
	set -- ./*
fi

# Process files in input parameters
for file in "$@" ; do

	# Ignore files without digits
	[[ "$file" != *[0-9]* ]] && continue

	# Split filename into prefix-digits-suffix
	[[ "$file" =~ ([^[0-9]*)([0-9]+)(.*) ]]

	# Pad digits to desired width
	printf -v numpad "%0*d" "$pad" "${BASH_REMATCH[2]##*(0)}"

	# Add old and new filenames to arrays for final processing
	oldfile+=( "$file" )
	newfile+=( "${BASH_REMATCH[1]}${numpad}${BASH_REMATCH[3]}" )

done

# If there are any files to rename, ask to confirm the operation.
# And rename if confirmed.
if [[ -n "${oldfile[*]}" ]]; then

	echo
	echo -e "${BCYAN}Rename the following files?${RESET}"
	echo
	for i in "${!oldfile[@]}" ; do
		echo -e "${oldfile[i]/#$PWD/.}\t-->\t${newfile[i]/#$PWD/.}"
	done
	echo

	read -p "(y/n): "

	echo

	case "$REPLY" in

		y|Y*)  	for i in "${!oldfile[@]}" ; do
					mv -n "${oldfile[i]}" "${newfile[i]}"
				done
				echo
				;;

		*)		echo -e "${BCYAN}Aborting.${RESET}"
				echo
				exit 1
				;;
	esac

# Otherwise just exit.
else
	echo
	echo -e "${BCYAN}No files to rename.${RESET}" >&2
	echo -e "${BCYAN}Exiting.${RESET}" >&2
	echo
	exit 1

fi

exit 0
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding files with filename format adshocker Linux - Newbie 4 10-28-2010 01:01 AM
extract last number from filename csegau Linux - Newbie 8 05-28-2010 07:24 AM
Find the max number Taz_3 Programming 5 12-28-2007 07:43 PM
help on finding the number that has max occurrences in a column [sql query] zeppelin Programming 6 06-15-2004 01:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 11:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration