Operating System - HP-UX
1829754 Members
1678 Online
109992 Solutions
New Discussion

test for test field qualifier

 
Pando
Regular Advisor

test for test field qualifier

I have 2 file below with the filename:

(1) BLF1820E-90_SemAuto_97211_9144_0310743.res
(2) BLF177C_95442_9279_0510859.res

As you can see, both fields are separated by underscore "_".

When i tried the command:

ll *_*_????_*.res

it displays the 2 files. However, when i used the command:

ll *_*_*_????_*.res

it displays only 1 file.

What i want to do is capture the 2nd last field (9144 and 9279) for both files and use it as qualifier for listing the all the files.

Maximum points for all correct answers.

8 REPLIES 8
Indira Aramandla
Honored Contributor

Re: test for test field qualifier

Hi Fernando,

Are all the file names of the same length.

TO capture the 2nd last field 9279
ls -l *_*_????_*.res | awk '{print $9}'| cut -c15-18

But if the filenames are of different lengths then you will have to read them from reverse order and print the digits.



Never give up, Keep Trying
Pando
Regular Advisor

Re: test for test field qualifier

Hi Indira,

The file is of different length.

Can i ask how to read them from reverse order and print the digits as you suggested?

Am very new to this. Many thanks.
Biswajit Tripathy
Honored Contributor

Re: test for test field qualifier

Well.. I'm on couple of drinks here, so stay with
me on this :-)

If you are willing to use a script that takes an
argument and prints all the file names that has
the 2nd last parameter matching the argument,
then here is your solution.

Create a file named "myls" with following contect:
----
for file in `ls *.res`
do
str=`echo $file | sed 's/_[0-9]*.res$/ /g' | sed 's/_/ /g'`
echo $str | grep -q "$1$"
if [ $? -eq 0 ]
then
echo $file
fi
done
------

To print all files with 2nd last parameter as 9144,
use:
# myls 9144

Hope this helps..
- Biswajit
:-)
Stephen Keane
Honored Contributor

Re: test for test field qualifier

If the final field before the .res is always numeric, you might try

ll *_????_[0-9]*.res

Substituting 9144 for ???? as required?
Rodney Hills
Honored Contributor

Re: test for test field qualifier

If you set up a loop for processing

for x in *.res ; do
y=${x#*_}
echo ${y%_*}
done

This will display the 2nd last field of each filename

HTH

-- Rod Hills
There be dragons...
Biswajit Tripathy
Honored Contributor

Re: test for test field qualifier

Stephen wrote:
> If the final field before the .res is always numeric,
> you might try
>
> ll *_????_[0-9]*.res
>
> Substituting 9144 for ???? as required?

As far as I know, this regexp will match filenames
like
*_9144_[0-9]???_????.res

9144 has to be the 2nd from last is what OP
asked for, if I understand correctly.

- Biswajit

:-)
Indira Aramandla
Honored Contributor

Re: test for test field qualifier

Hi Fernando,

Since the file names are of different length, you can write a script where you get the number of characters (using word count)in the file name.
For Eg:- for this file
BLF1820E-90_SemAuto_97211_9144_0310743.res
ls -l *_*_*_????_*.res | awk '{print $9}'| wc -c will give 43 as the number of characters in the name. And from the reverse side if you see there are a constant number of character which is 12, to get the 2nd last number. So have the word count in a variable and substract the constant 12 to get the 2nd last field.


Indira A
Never give up, Keep Trying
Indira Aramandla
Honored Contributor

Re: test for test field qualifier

Hi Fernando,

1)BLF1820E-90_SemAuto_97211_9144_0310743.res
2)BLF177C_95442_9279_0510859.res

I have written a smallscript taking your two files as example.
The variable filed2 will hold the value of the second last fields (9144 and 9279) for both files and then you can use the $filed2 variable as a qualifier.

I hipe his helps

Indira A

Never give up, Keep Trying