How to check a word position in a file ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check a word position in a file ?
# 1  
Old 01-03-2008
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 !
# 2  
Old 01-03-2008
Quote:
Originally Posted by tibo
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

# 3  
Old 01-03-2008
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

# 4  
Old 01-03-2008
Quote:
Originally Posted by shamrock
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

# 5  
Old 01-03-2008
Quote:
Originally Posted by rikxik
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
# 6  
Old 01-03-2008
Then lets do this:

Code:
sed 's/ *.*fire \([0-9][0-9]*\).*/\1/' file

Works with/without leading/trailing spaces.
# 7  
Old 01-04-2008
Quote:
Originally Posted by rikxik
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Write a word at 72nd position of a matched line in a file

Hi, I need to search a file for a pattern,replace some other word and write a word at its 72nd position. For example, My name is Mano.Im learning Unix. I want to search the file in all lines containing the word "Mano".In that matched line,replace the word "Unix" with "Java".And... (5 Replies)
Discussion started by: mano1 n
5 Replies

3. Shell Programming and Scripting

Grep the 5th and 6th position character of a word in a file

I am trying to find/grep the 5th and 6th position character (TX) of a word in a file. I tried to do grep "....TX" file The output gives me everything in the file with TX in it. I only need the output with the TX in the 5th and 6th position of the word. Any idea Example: test1 car... (5 Replies)
Discussion started by: e_mikey_2000
5 Replies

4. UNIX for Dummies Questions & Answers

Word check within file

I am trying to see if a word exists within the file, then do command accordingly. cmd=$(grep -ci "$word" $file) if ; then echo "Word exists" else echo "Word does not exist." fiI try this code, but it keeps giving me syntax errors for the cmd line. Am I doing something... (3 Replies)
Discussion started by: itech4814
3 Replies

5. Shell Programming and Scripting

Need to check a file from a certain position and date onwards

Hi Guys, I need some advice please. My script is not grabbing information from a text file from a certain date correctly. It seems to be grabbing everying in the file, i know it is something simple but i have looked to hard and to long, to know what the issue is. Script awk '... (9 Replies)
Discussion started by: Junes
9 Replies

6. Shell Programming and Scripting

Need to check a file from a certain position onwards

Scripting Guru's, I need your help, can you tell me how i can check a file from a certain point onwards via a ksh script. Currerntly i am checking the whole file and can't script it so it checks from 17.01.2012 20:00:00 onwards please.. Any help will be greatly appericated. See file... (10 Replies)
Discussion started by: Junes
10 Replies

7. UNIX for Dummies Questions & Answers

Script to delete a word based on position in a file

Hi, I am new to unix. I want to delete 2 words placed at position say for example at 23rd and 45th position in a line. I used sed but couldnt achieve this. Example: the file contains 2 lines 12345 98765 "12345" 876 12345 98765 "64578" 876 I want to delete " placed at position 13 and 19... (4 Replies)
Discussion started by: nbks2u
4 Replies

8. Programming

C++ > check if a file contains a word...

I wanna check if a file located in /home/user/.config/Home/unkilled.txt contains the word 'permitted'... I am a beginner in C++. This is my code: 1. #include <stdio.h> 2. #include <cerrno> 3. #include <sys/stat.h> 4. #include <fstream> 5. #include <sys/types.h> 6. #include... (1 Reply)
Discussion started by: hakermania
1 Replies

9. Shell Programming and Scripting

How to put a word starting at particular position in a file using shell scripting

Hi all, I'm new to shell scripting and hence this query. I have 2 files. temp.txt and config.txt. The values in temp.txt are tab separated. ex: temp.txt AB CDE GHIJ OPQRS WXY ex:config.txt (1st line for 1st element of temp.txt and so on) start = '1' end='5' start = '6' end =... (26 Replies)
Discussion started by: subhrap.das
26 Replies

10. UNIX for Dummies Questions & Answers

Paste a word in the third field position of a file

Hi All, I have a file like this, 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 The above line is a comma separted data file. I want to modify the third field to The final data file should be like 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 ... (1 Reply)
Discussion started by: girish.raos
1 Replies
Login or Register to Ask a Question