Search backwards


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search backwards
# 1  
Old 10-06-2008
Search backwards

Hi,

I have a variable , lets say
a=/disk1/net/first.ksh

i need to grep "first.ksh"

everytime "a" gets changed dynamically and i do not know how many '"/" are there in my variable.

Can somebody help me out.
# 2  
Old 10-06-2008
in awk
Code:
echo $a| awk -F/ '{print $NF}'

# 3  
Old 10-06-2008
great it works....

Can you please explain me the command
# 4  
Old 10-06-2008
Or simply use basename -- or if your shell understands ${a##*/} then by all means use that.
# 5  
Old 10-06-2008
explanation

The awk command splits its input into the character specified by the -F option (in this case "/"). So awk sees something like:
disk1 net first.ksh
Awk has an internal variable named "NF" which stands for the Number of Fields on the current line. In this case that's 3. So it's like writing:
awk '{ print $3 '}
which would print the third field of the input. Of course, it's not really 3 every time, just this time. So the last part of the path gets printed.
# 6  
Old 10-07-2008
Thanks a lot....

is that i can take

/disk/net in one variable and first.ksh in one variable ?
# 7  
Old 10-07-2008
basename $a
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date Time Zone Conversion (backwards of what I want)

I am trying to convert local time to time in Ireland. Instead it is going the opposite direction (taking the local time as it if were in Ireland and displaying that the time would be here). $ echo "$TZ"; date; date --date='TZ="Europe/Dublin" '"$(date)" America/Phoenix Mon, Apr 13, 2015... (7 Replies)
Discussion started by: Michael Stora
7 Replies

2. Shell Programming and Scripting

Search backwards to certain string

Hi, I'm using the following to do a backwards search of a file for a string sed s/^M//g FILE | nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=10 a=0 s="9005"|grep "policy "|sort -u |awk '{print $4}'|cut -c2-10 My issue is that because I'm looking back 10 lines it's... (11 Replies)
Discussion started by: SaltyDog
11 Replies

3. Programming

How to search a file based on a time stamp backwards 10 seconds

Hi all, I'm after some help with this small issue which i'm struggling to work out a fix for. I have a file that contains records that all have a time stamp for each individual record, i need to search the file for a specific time stamp and then search back 10 seconds to see if the number... (2 Replies)
Discussion started by: sp3arsy
2 Replies

4. Shell Programming and Scripting

Searching backwards using regular expressions

I'm having trouble writing a regular expression that matches the text I need it to. Let me give an example to express my trouble. Suppose I have the following text: if(condition) multiline statement else if(condition) multiline statement else if(condition) multiline statement else... (3 Replies)
Discussion started by: Altay_H
3 Replies

5. Shell Programming and Scripting

search for string and replace backwards

I'm new to Unix scripting and I'm not sure if this can be done. Example: search (grep) in a file for 'Control ID' and then replace with 4 blanks 7 bytes before 'Control ID. input "xxxxxx1234xxxxxxxControl IDxxxxxx" output: "xxxxxx xxxxxxxControl IDxxxxxx" thanks! (7 Replies)
Discussion started by: jbt828
7 Replies

6. Shell Programming and Scripting

How to search backwards in a log file by timestamp of entries?

Hello. I'm not nearly good enough with awk/perl to create the logfile scraping script that my boss is insisting we need immediately. Here is a brief 3-line excerpt from the access.log file in question (actual URL domain changed to 'aaa.com'): 209.253.130.36 - - "GET... (2 Replies)
Discussion started by: kevinmccallum
2 Replies

7. Shell Programming and Scripting

search backwards relative to a string

Hi, I have to search for first occurenceof string str1 in a file(>5GB). Now, after I have that , I have to search backwards from that offset till I get another string str2. I should also be able to get the new string str2's offset. Similarly, I look for last occurence of str1 and then... (1 Reply)
Discussion started by: finder255
1 Replies

8. Shell Programming and Scripting

read file backwards

Assume file1 contains a list of strings. My first script is scanning the file and deals with the lines with a certain patern in it: grep 'somepatern' file1 \ while read LINE ; do doSomethingAboutIt done Now, I need a second script that deals with the same lines (& do something... (3 Replies)
Discussion started by: bluemoon1
3 Replies

9. UNIX for Advanced & Expert Users

Scanning file backwards

Is there any way to look for a directory path that is listed any number of lines *before* a keyword in an error message? I have a script that is trying to process different files that are always down a certain portion of a path, and if there is an error, then says there is an error, contact... (2 Replies)
Discussion started by: tekster757
2 Replies

10. AIX

ML level went backwards?

Hi all. I've been put in charge of updating one of our AIX 5.2 servers to ML7. (perhaps not wise since I'm an absolute n00b, but hey, it's good experience to fly by the seat of one's pants). So: a) I typed "oslevel -r" and got back "5200-04" b) I went to IBM's Fix Central and downloaded... (1 Reply)
Discussion started by: pschlesinger
1 Replies
Login or Register to Ask a Question