[Beginner's questions] Filename Validation & Parsing

Hi !!

I'm rather new both to the UNIX and scripting worlds, and I'm learning the ropes of scripting. Having said this, please excuse me if you notice certain basic errors.

I'm working on a script that implements .jar and .war files for a WAS environment and I need to perform certain validations, before copying and moving files around within different working environments.

Our .jar filename structure follows:

<whatever application name>_YYYYMMMDD_HHmm_nnnn.jar

Example:

myapp_20121031_1021_1234.jar
otherappname_20120928_0943_3212.jar

I receive the .jar filename as a parameter and I need to extract the first portion of the filename into a variable. I'm not sure how to do this. Either use the 'cut' command or 'awk'.

Basically, whenever I get the filename parameter, I must discard the final 23 bytes and keep the rest.

For example, for the first sample filename, I need to extract 'myapp' into a variable. For the second example, I need to extract the portion 'otherappname'.

I'd really appreciate your help

Thanks in advance !!

Enrique Valdez

PS: This a AIX 5.3 machine and Korn Shell.

Hi

$ x="myapp_20121031_1021_1234.jar"
$ y=`echo $x | sed 's/_.*//'`
$ echo $y
myapp

Guru.

1 Like

should work in ksh acc. to man page:

$ x="myapp_20121031_1021_1234.jar"
$ echo ${x%%_*}
myapp
1 Like

Hi:

Thanks both guruprasadpr and RudiC.

I notice that on your responses, you look for the first '_' character to discard the rest of the text.

But I don't want to limit the first part of the name to not have a '_' character.

In example, I might have the following filename 'my_newapp_20121031_1021_1234.jar'.

>txt="my_newapp_20121031_1021_1234.jar"
>echo $txt | sed 's/_.*//'
my

>txt="my_newapp_20121031_1021_1234.jar"
>echo ${txt%%_*}
my

I need to keep the 'my_newapp' portion...

The point would be how to extract both the first portion of the text ('my_newapp') in one variable.
The last 23 bytes ('_20121031_1021_1234.jar') in a separate variable which I might need to parse into separate variables for further validation (Year, Month, Day, Hour, Min, milli)....

Anyway, your replies are greatly appreciated.

Enrique

This works in bash; pls try in ksh (should work acc. to man page):

$ txt="my_newapp_20121031_1021_1234.jar"
$ echo ${txt:0:${#txt}-23}
my_newapp
$ echo ${txt:${#txt}-22}
20121031_1021_1234.jar