The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check position of end of line(URGENT PLS) evvander Shell Programming and Scripting 6 11-11-2007 02:46 PM
check position of end of line for some specific lines senthil_is Shell Programming and Scripting 1 11-08-2007 10:19 PM
how to check a word in a file and assign to a variable hippo2020 Shell Programming and Scripting 1 06-28-2007 09:00 AM
Change Position of word character cedrichiu Shell Programming and Scripting 6 03-11-2007 10:52 PM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 03:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-03-2008
Registered User
 

Join Date: Jan 2008
Posts: 1
How to check a word position in a file ?

Hello everybody,
I have a file like this : "window 1 truck 3 duck 2... fire 1... etc..." and I would like to print the following number of a word I am searching for. (For example here, if I search for the word "fire", I will print "1")
Thank you for your help !
Reply With Quote
Forum Sponsor
  #2  
Old 01-03-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 568
Quote:
Originally Posted by tibo View Post
Hello everybody,
I have a file like this : "window 1 truck 3 duck 2... fire 1... etc..." and I would like to print the following number of a word I am searching for. (For example here, if I search for the word "fire", I will print "1")
Thank you for your help !
Code:
sed 's/.* fire \([0-9][0-9]*\) .*/\1/' file
Reply With Quote
  #3  
Old 01-03-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Code:
s="window 1 truck 3 duck 2... fire 1"
f=0
for i in $s;  
do  
    if [ $f -eq 1 ];then    
      echo "Found number $i"
    fi
    case $i in 
      fire) f=1 ; continue;;
    esac;  
done
Reply With Quote
  #4  
Old 01-03-2008
rikxik's Avatar
Registered User
 

Join Date: Dec 2007
Posts: 105
Quote:
Originally Posted by shamrock View Post
Code:
sed 's/.* fire \([0-9][0-9]*\) .*/\1/' file
Doesn't work here:

Code:
$ uname -a
SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M
$ which sed
/usr/bin/sed
$ cat file
window 1 truck 3 duck 2 fire 1
$ sed 's/.* fire \([0-9][0-9]*\) .*/\1/' file
window 1 truck 3 duck 2 fire 1
Reply With Quote
  #5  
Old 01-03-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by rikxik View Post
Doesn't work here:

Code:
$ uname -a
SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M
$ which sed
/usr/bin/sed
$ cat file
window 1 truck 3 duck 2 fire 1
$ sed 's/.* fire \([0-9][0-9]*\) .*/\1/' file
window 1 truck 3 duck 2 fire 1
This is because in your sample file, after the "1", there is no space.
The sed statement has a <space> after the \) bracket. It should be
Code:
# sed 's/.* fire \([0-9][0-9]*\).*/\1/' file
1
for your case of sample file
Reply With Quote
  #6  
Old 01-03-2008
rikxik's Avatar
Registered User
 

Join Date: Dec 2007
Posts: 105
Then lets do this:

Code:
sed 's/ *.*fire \([0-9][0-9]*\).*/\1/' file
Works with/without leading/trailing spaces.
Reply With Quote
  #7  
Old 01-03-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 568
Quote:
Originally Posted by rikxik View Post
Then lets do this:

Code:
sed 's/ *.*fire \([0-9][0-9]*\).*/\1/' file
Works with/without leading/trailing spaces.
But the above pattern will also catch words like backfire, spitfire etc...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0