Finding last row


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Finding last row
# 1  
Old 09-21-2005
Finding last row

Using Linux and Bash, I have a script that outputs filenames with complete path, like this:
Quote:
/foo/bar/filename.pdf
/foo/bar/filename with space.pdf
/foo/filename.txt
I would like the following output:
Quote:
filename.pdf
filename with space.pdf
filename.txt
And I would like to get the filenames only. Tricky part is that I cannot predict how many levels deep the filename is located, so I cannot use standard Bash-kungfu to solve this...

Last edited by indo1144; 09-21-2005 at 10:20 AM..
# 2  
Old 09-21-2005
Add this line to the script

Code:
FILENAME=${file##*/}

where file would hold contain the values

Code:
/foo/bar/filename.pdf
/foo/bar/filename with space.pdf
/foo/filename.txt

one at a time.

And FILENAME would hold filename.pdf filename with space.. et al.

vino
# 3  
Old 09-21-2005
Quote:
Originally Posted by vino
Code:
FILENAME=${file##*/}

Now that worked perfectly!

I have no idea what this means, though. You set a new variable FILENAME that is $file with some magic applied. I do not understand the syntax and I am eager to learn more about this, since I find this very useful. Would you care to explain?
# 4  
Old 09-21-2005
Dare to start out with the man pages ? Smilie

Check out the section Parameter Expansion under man sh
and the section Parameters under man ksh

-vino
# 5  
Old 09-21-2005
Quote:
Originally Posted by vino
Dare to start out with the man pages ? Smilie

Check out the section Parameter Expansion under man sh
and the section Parameters under man ksh

-vino
Found it! I wanted to read more about this, but did not realize this was a shell built-in option, so did not think of man bash (in my case)...

Appreciate the help!
# 6  
Old 09-21-2005
Quote:
Originally Posted by indo1144
Found it! I wanted to read more about this, but did not realize this was a shell built-in option, so did not think of man bash (in my case)...
I believe I understand what happened...
Quote:
${parameter##word}
The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the
beginning of the value of parameter, then the result of the expansion is the expanded value of parameter
with the shortest matching pattern (the ``#'' case) or the longest matching pattern (the ``##'' case)
deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter
in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @
or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is
the resultant list.
So the ##-case expands to the longest matching pattern deletion. Since the parameter is *, it removes all matching patterns in the array, until it has no matches left. You read in the entire pathname as an array which has a delimiter "/" and the final result is the filename only.

Am I right?

This would mean that /foo/bar/filename.pdf would result in filename.pdf, but if the input would have a trailing slash like so: /foo/bar/files/ it would return... nothing!
# 7  
Old 09-21-2005
True.

At times {file##*/} can be use as an shell-builtin alternative for the command basename. But as you see, this scenario shows the difference between basename and {file##*/}

vino
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

4. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

5. Shell Programming and Scripting

Finding only one error in a row

I am using below code to validate whether datatype,length and date format is correct as defined in file_layout. Below code is able to find only one error in a row,if there is more than one error in a same row then code is not able to highlight second error. How to customize the below code to... (8 Replies)
Discussion started by: srivalli
8 Replies

6. UNIX for Dummies Questions & Answers

Finding row number along with length of row

I have a fixed length file and I want to find out row number along with row length. I have a program that give me the line length if it satisfy the condition; but i would like to add row number as well? How do I do that? while IFS= read -r line; do if ; then echo ${line} echo... (8 Replies)
Discussion started by: princetd001
8 Replies

7. UNIX for Dummies Questions & Answers

Finding files with one row then moving them

Hi guys can you please help me with a script to find files with one row the move the file to another directory the files are in /optima/prd/optdir/ZTE_BSS/Combiner/ZTE_2G/out/BSC_PS_Basic_Meas and I want to move them to /optima/prd/optdir/ZTE_BSS/Loader/error/ZTE_2G/BSC_PS_Basic_Meas ... (5 Replies)
Discussion started by: Dj Moi
5 Replies

8. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

9. Shell Programming and Scripting

Finding Minimum value per Row range of data

Here is an example of a file I am working with: C 4704 CB 1318 ASP 115 BGRF 1 weak 0.0% 4.33 C 4720 OD 1322 ASP 115 BGRF 1 weak 0.0% 3.71 O 4723 OD 1322 ASP 115 BGRF 1 weak 0.0% 3.48 O 4723 CG 1321 ASP 115 BGRF 1 weak 0.0% 4.34... (3 Replies)
Discussion started by: userix
3 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question