How to get the last field of a string(pattern)?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to get the last field of a string(pattern)?
# 1  
Old 11-07-2009
How to get the last field of a string(pattern)?

b= find =/home/root/dir1/*.xml | xargs grep '</XML>' | cut -d ":" -f1 | uniq

From a folder that has many files, I am trying to get the filename which has </XML> in it using the above command. But while giving the result, the variable b is showing the path also.

Ex: b=/homt/root/dir1/gg.xml.
How can I just get the value gg.xml in b?
The path I am giving as a variable.It is not hardcoded as (/home/root/dir1/*.xml)
Ex: $path/*.xml.So the occurrence of "/" can vary.

I am using KSH.


Last edited by asmfloyd; 11-07-2009 at 08:11 PM..
# 2  
Old 11-07-2009
One options:

b=$(find $MY_PATH -exec grep -l '</XML>' {} \; | xargs -I{} basename {} | uniq -u)

puts all the filenames which match into b.

Not so great if any file names have spaces in them.
# 3  
Old 11-08-2009
Hi Scott,
It does not print anything for me.It just come out of the shell. My filenames are like file1.xml,file2.xml,...
Also I tried echo ${b##*/}. That also does nothing.Bad luck..

Last edited by asmfloyd; 11-08-2009 at 02:47 AM.. Reason: Addition
# 4  
Old 11-08-2009
One way ..
$ b=/homt/root/dir1/gg.xml
$ echo $b
$ echo $(basename $b)

# 5  
Old 11-08-2009
You said that the path you wanted to search was in a variable. Did you change $MY_PATH to something meaningful for you?

I did test this before I posted it!

$ cat file1.xml
this is some xml

$ cat file2.xml
this is some more xml


$ b=$(find $MY_PATH -exec grep -l '</XML>' {} \; | xargs -I{} basename {} | uniq -u>

# echo $b
file1.xml file2.xml

# 6  
Old 11-08-2009
Thanks friends
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

4. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

5. Shell Programming and Scripting

Displaying the first field if the second field matches the pattern using Perl

Hi, I am trying with the below Perl command to print the first field when the second field matches the given pattern: perl -lane 'open F, "< myfile"; for $i (<F>) {chomp $i; if ($F =~ /patt$/) {my $f = (split(" ", $i)); print "$f";}} close F' dummy_file I know I can achieve the same with the... (7 Replies)
Discussion started by: royalibrahim
7 Replies

6. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

9. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

10. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies
Login or Register to Ask a Question