Parse file name, add to loop?

I have a list of files that I need to loop through, parse, and run a command against. Here's my selected approach.

for i in FIELD1.FIELD2.FIELD3.FIELD4.index
do
  j=${i%.index}
  loadutility -flag1 FIELD1 -flag2 FIELD2 -flag3 FIELD3  $j(without.index extension)
done < filelist.del

I figure a loop like this is my best approach but I might have to throw some awk into the equation, which unfortunately I haven't touched really!

Any suggestions on a good approach is appreciated!

Your code snippet certainly will not do what you expect it to do. A for loop doesn't read from stdin, so a redirection from filelist.del won't do anything (unless the loadutility will read from there). The loop itself will iterate exactly once with the $i variable assuming the value FIELD1.FIELD2.FIELD3.FIELD4.index , and the $j assuming FIELD1.FIELD2.FIELD3.FIELD4 . The loadutility command will execute with the "FIELDn" string constants exactly as written; expansion will be performed on the $j variable only.

"I might have to throw some awk into the equation" - on what? Where? What is your target? What is your input? What is your OS, and shell? And, do you want to "parse a file name", or a file ('s contents)?

I am using bash shell.

Here's a high level summary of what I have if it will help out better explain what I am trying to do.

I need to run a utility against a series of files, it needs to hit each file in a certain directory.

Example of my files in my directory-
$BASEDIR/data/files

file1.field1.field2.field3.index
file2.field1.field2.field3.index
file3.field1.field2.field3.index

The fields can all vary depending on the file.

The desired command syntax to execute is below:

loadcommand -a field2 -g field3 file1.field1.field2.field3.index
loadcommand -a field2 -g field3 file2.field1.field2.field3.index
loadcommand -a field2 -g field3 file3field1.field2.field3.index

Stumped unfortunately.

Hi jeffs42885,
Expanding slightly on what RudiC has already said...

I do not have any idea of what you are trying to do. The loop you have shown us above has one item to be processed, and, therefore, invokes loadutility once. There is no file parsing nor filename parsing indicated in your loop and you haven't specified any reason why you might want to throw awk into your loop.

Assuming that (without.index extension) in the code in post #1Make was intended to be a comment (which it is not any any shell language I'm aware of), your loop can be replaced by the single command:

loadutility -flag1 FIELD1 -flag2 FIELD2 -flag3 FIELD3  FIELD1.FIELD2.FIELD3.FIELD4 < filelist.del

I assume that is this not what you're trying to do, but I can't figure out from your description what it is that you do want to do if it is not to execute the above single command.

I have also never heard of the loadutility utility, but the options you are passing to it seem strange unless you intend for flag1 through flag3 to be variable single-letter options each of which takes an option-argument (but, if that was the case you should have -$flag1 through -$flag3 .

Please tell us what operating system you're using, what shell you're using, and provide us with a much clearer statement of what you are trying to do.

And then we see your post #3 which doesn't really help at all? Why is the .index that you said had to be deleted not deleted from any of the invocations of loadutility ?

What happened to -flag1 FIELD1 in your invocations of loadutility ?

What is special about the filename starting with file3 that the first <period> in its name has to be removed when it is passed to loadutility as an operand?

You aren't the only one that is stumped!

Apologies. Alot of this is custom in house stuff and I am just trying to mask certain things. I am also trying to provide my approach in a scripting sense with examples, as to not just "ASK FOR THE SOLUTION!!"

Anyways, like I said..bash (RHEL)

It might be easier if I just list the steps that I am trying to do..

Here are the high level steps I am trying to accomplish..

1) Gather listing of all files in $basedir/directory with extension .index (They will all be named like this, uniquely - FIELD1.FIELD2.FIELD3.FIELD4.index)
2) Run a custom utility against each file with the .index extension, in the below format.

loadutility -flag1 FIELD3 -flag2 FIELD4 FIELD1.FIELD2.FIELD3.FIELD4

Hope this help, didnt mean to confuse!

something along these lines?
Remove echo when satisfied with results.

#!/bin/bash
path2dir='/whatEverPath2dir'
for file in ${path2dir}/*.index
do
   IFS=. read f1 f2 f3 rest <<< $file
   echo loadcommand -a "${f2}" -g "${f3}" "${f1}.${f2}.${f3}.${rest}"
done

Your approach to learn the ropes yourself in lieu of requesting a turnkey solution is the right one, congrats.

But, for your own - and our - sake: please please please - BE consistent!

You confuse people by having a loadutility here and a loadcommand there, then three flags, two flags, three flags, two flags, then FIELD1, FIELD2, FIELD3 here, field2 & field3 there, then FIELD2 and FIELD4, and a filen in front, or no filen, .index yes, .index no. Do you really expect someone to come up with a decent solution based on that? I'm pretty sure below proposal will NOT fit your actual needs; be prepared to experiment with it and adapt until it fits.

Howsoever, as vgersh99 beat me with the for loop solution, how about this one:

ls *.index | while IFS="." read FN F1 F2 F3 IX; do echo loadutility -a $F2 -g $F3 $F1.$F2.$F3; done
loadutility -a field2 -g field3 field1.field2.field3
loadutility -a field2 -g field3 field1.field2.field3
loadutility -a field2 -g field3 field1.field2.field3

The echo is there for safety / debug purposes; remove if happy with what you see.

The "against each file with the .index extension" makes it sound like you want the .index in the pathname you pass to loadutility . The sample invocation of loadutility you provided does not include the .index .

You haven't said anything about what directory you should be in when you run loadutility and the examples you have been given make varying assumptions about whether or not you just want the final component of the pathname passed to loadutility as its last operand and whether or not the .index should be included.

I will make a different set of assumptions and you can let us know if any of us came close to what you're trying to do:

#!/bin/bash
BASEDIR=/some/where

cd "$BASEDIR/data/files" || exit 1

EC=0
OIFS=$IFS

for FILE in *.index
do	if ! [[ -f $FILE ]]
	then	printf '%s: file "%s" not found\n' "${0##*/}" "$FILE" >&2
		exit 2
	fi
	printf '%s: Processing file "%s"\n' "${0##*/}" "$FILE"
	BASE=${FILE%.index}

	IFS=.
	FIELDS=( $BASE )
	IFS=$OIFS

	echo loadutility -a "${FIELDS[2]}" -g "${FIELDS[3]}" "$BASE"
	if [[ $? -ne 0 ]]
	then	EC=3
	fi
done
exit $EC

As with the other suggestions, the echo causes this to print what it is planning to do, but does not actually run loadutility . At least now you have several different examples of ways to approach what we think you're trying to do.