[Beginner's questions] Filename Validation & Parsing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Beginner's questions] Filename Validation & Parsing
# 1  
Old 11-02-2012
Hammer & Screwdriver [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.
# 2  
Old 11-02-2012
Hi

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

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 11-02-2012
should work in ksh acc. to man page:
Code:
$ x="myapp_20121031_1021_1234.jar"
$ echo ${x%%_*}
myapp

This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-02-2012
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

Last edited by levaldez; 11-02-2012 at 08:58 AM..
# 5  
Old 11-03-2012
This works in bash; pls try in ksh (should work acc. to man page):
Code:
$ txt="my_newapp_20121031_1021_1234.jar"
$ echo ${txt:0:${#txt}-23}
my_newapp
$ echo ${txt:${#txt}-22}
20121031_1021_1234.jar

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

beginner scripting questions User variables

If there's anywhere to look this up, it would be just as helpful. I googled and really couldn't find anything relative to this. ok... General Variables 1) When creating a script I made a file "prog1.sh" does it matter if the end is .sh or is this what has to be done like prog.bash or... (4 Replies)
Discussion started by: austing5
4 Replies

2. AIX

Beginner's questions about AIX (6.1)

Hello, For some time I have intellistation 9111-285 and I installed AIX 6.1 on it. As a complete beginner I have 2 questions in general about AIX and two specific: 1. is the SMS (system management services) part of AIX? As I noticed when I had Yellowdog Linux installed they weren't available?... (2 Replies)
Discussion started by: kenashkov
2 Replies

3. Homework & Coursework Questions

Beginner Questions.

This is the Test_Data.snp file: MEGAUPLOAD - The leading online storage and file delivery service 1. The problem statement, all variables and given/known data: Problem Set: Before you get started working with these challenges, be aware that the first challenge is reformatting the test data... (7 Replies)
Discussion started by: vlay2
7 Replies

4. Shell Programming and Scripting

Filename Validation

Hi All, We need to use & execute some several scripts before loading the data successfully using Informatica. Can you help me the below scenario: We will be receiving the files from different countries, and using ftp all the files will be placed onto the server. The file structure will be (.csv... (6 Replies)
Discussion started by: psr2009
6 Replies

5. Linux

Beginner questions about versions and installing

1. I have never used or dealt with unix, linux, or any variation thereof, and want to. My biggest problem is that all the versions I've looked at want you to install from a CD or DVD, but I'm wanting to put it onto an Asus eeepc, which has no such drive. How would I go about installing on it? ... (4 Replies)
Discussion started by: lemming
4 Replies

6. UNIX for Dummies Questions & Answers

Absolute Beginner Questions

... before you role your eyes, I picked up my first Unix book 3 days ago! As such, I have a few quick questions that I'm sure are super easy for everyone out there but me! Forgive me if the terminology I use is wrong ... I'm accessing a remote Unix server, I can make my way around directories... (2 Replies)
Discussion started by: joey_tomatoes
2 Replies

7. UNIX for Dummies Questions & Answers

Beginner Questions

Hi everyone. I guess I am the new guy, and also new to Unix. I purchased a box of computer supplies at an auction, and found an unopened box of Compaq Smartstart. So here is what I have... Smartstart 2.5, Netware, Windows NT, OS2 & Lan Server, SCO Open Server release, SCO Unixware 2, Oracle 7 for... (3 Replies)
Discussion started by: Darin
3 Replies

8. Shell Programming and Scripting

validation :file <filename>

My user is ftp a CSV file from window. There is possiblity to doing FTP through different mode. This file is then picked by another program, but I want to make sure the FTP file would be always ascii format, bu releasing a commaon File <file name> Can any one provide input , abut how to put a... (2 Replies)
Discussion started by: u263066
2 Replies

9. UNIX for Advanced & Expert Users

Filename Validation

I'm going through a script someone wrote and has left the company. I've a file_name -> s31-d173-lookup-20060502111140-sr In the ksh this is a validation : if |)-(*)-(*)---(|-)-sr)) ]]; The validation for the above file_name fails. Any idea why and what is file naming convention the... (2 Replies)
Discussion started by: F-1
2 Replies
Login or Register to Ask a Question