grep second word in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep second word in a file
# 1  
Old 05-18-2009
grep second word in a file

How do I match second word and then print that line to output

For eg: My file has following text and I want to check if second word is n then I want to print entire line .

xytxti8naclip y
xytxt32bpsimple y
xytxt32bpna n
xytxti16nae y
xysmps32bpp031_bst n
xyslm32simple y

Can any one help here ?
# 2  
Old 05-18-2009
If these are real examples and the second word is also the last word in the line then you can do:

Code:
$ grep " n$" file

That is only look for lines with an "n" with a space in front of it and an end of line after it, does that help?
# 3  
Old 05-18-2009
that is sufficient but, it gives an error

filc20092> grep " n$\" filename
Illegal variable name.
filc20092>
# 4  
Old 05-18-2009
Mine is c shell not Bourne Shell
# 5  
Old 05-18-2009
grep n\$ infile
# 6  
Old 05-18-2009
Also, that just gets a line that ends in "n". If you really want the second word, you could do:

Code:
#!/usr/bin/perl
my $infile="infile";
open (INFILE,"<$infile");

while (<INFILE>) {
        chomp($_);
         @words=split(/ /,$_);
        if ($words[1] =~ /n$/) {
                print $words[0] . " " . $words[1] . "\n";
        }
}

Or something similar (that was off the cuff, I'd be surprised if it ran.)
# 7  
Old 05-18-2009
Quote:
Originally Posted by pitagi
that is sufficient but, it gives an error

filc20092> grep " n$\" filename
Illegal variable name.
filc20092>
Where has the backslash come from, that is what is causing the error?

Code:
$ cat /tmp/greptest
xytxti8naclip y
xytxt32bpsimple y
xytxt32bpna n
xytxti16nae y
xysmps32bpp031_bst n
xyslm32simple y
$ grep " n$" /tmp/greptest
xytxt32bpna n
xysmps32bpp031_bst n
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script example to Grep matching word from file

Hi expert, Need help in shell script. a.txt file output is below i would like to grep 3 line and 1st column value which is admin\22226 only and not full line. i only know admin word as 22226 can come anything with admin\ in file. also after fetching it i would like to use this... (1 Reply)
Discussion started by: kuljeetpal
1 Replies

2. UNIX for Beginners Questions & Answers

How to Grep second word in config file using parameter?

Currently i am building one script to grep region records in the config file based on parameter and then i am creating a text file with that out put and i am reading the source file path in that out put file now i need to pass one more parameter like module based upon that it has to create a... (1 Reply)
Discussion started by: saranath
1 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. Shell Programming and Scripting

How can I just grep one instance of a word in the file

I want to grep some information out of the dmidecode but when I type dmidecode | grep Memory I get several instances of the word. Is there a way I can just choose which instance I want to display? (8 Replies)
Discussion started by: jcnewton13
8 Replies

5. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

how to grep the max length word form a file?

Hi i have a requirement that is extract the max length word from a file ? plz reply (2 Replies)
Discussion started by: vankireddy
2 Replies

8. UNIX for Dummies Questions & Answers

Grep the word at the end of the file

I need to grep the word "hello" in each and every file. This word will be placed either at the end of the file or before the end of the file. Ex: file1: file2: afdsaf dsfalk fdsa weruoi sdaf erwqiuo fsdaf ... (5 Replies)
Discussion started by: sivakumar.rj
5 Replies

9. Shell Programming and Scripting

how to grep for a word in a log file..

Hi All, I have a log file which is storing a backup time stamps information in it. (pasted below) 20081006210008 0 20081006211910 20081006222824 0 20081006224758 20081007210049 0 ... (2 Replies)
Discussion started by: suri.tyson
2 Replies

10. Shell Programming and Scripting

grep all records in a file and get a word count -perl

Hi, I have a file .. file.txt .. i need to get a total record count in the files into a $variable.. im using perl script thanks (4 Replies)
Discussion started by: meghana
4 Replies
Login or Register to Ask a Question