For some reason, my grep doesn't work as expected


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers For some reason, my grep doesn't work as expected
# 1  
Old 05-31-2009
For some reason, my grep doesn't work as expected

I am trying to find only those entries where 7018 and another number appear in the end of the line.

Code:
7018 2828 1423 2351
7018 2828 14887
2828 7018 1222
123 7018 1487

I am looking for a way to generate only the last two lines. I was trying to do just "grep '7018[\s\t][0-9]{1,5}" but it does not work. What I am looking for is a way to find those lines where there is 7018 and exactly one more number after it... Can someone suggest a better way please?
# 2  
Old 05-31-2009
Code:
awk '$(NF-1)=="7018" && $NF+0==$NF' file

# 3  
Old 05-31-2009
Thank You. That works perfectly. I wanted to do this task in a directory recursively on the files and note down the file in which this was found. It was possible with grep but is there a way to make awk to it? (Sorry... I am new to awk and am still learning it)

I am actually thinking of using "find ./ -name '*ext' | xargs awk". I'm still experimenting on how to print the filename along with the line that awk finds... I will get back if I get it...

I think I figured it out...

Code:
 find ./ -name "*ext" | xargs awk '$(NF-1)=="7018" && $NF+0==$NF {print FILENAME "::" $0}'

Thanks for your help...

x

Last edited by Legend986; 05-31-2009 at 02:34 AM..
# 4  
Old 05-31-2009
with sed
Code:
sed -n '/.*[ \t]7018[ \t][0-9]\{1,5\}$/p' filename

# 5  
Old 05-31-2009
Quote:
Originally Posted by ghostdog74
Code:
awk '$(NF-1)=="7018" && $NF+0==$NF' file

i don't think the second check condition is required..
because if last but one filed is 7018 then it automatically implies there is one more field after that..
# 6  
Old 05-31-2009
Quote:
Originally Posted by vidyadhar85
i don't think the second check condition is required..
because if last but one filed is 7018 then it automatically implies there is one more field after that..
yes i had taken it into consideration as well, but my final solution takes care of the case where the last field the OP's data happens not to be a number but a word.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep doesn't work when assigning to variable

Hello, First of all, I'd like to say hello to all members of forum. Can You please help me with the matter described below? I am trying to fetch a data from the file to variable, I am doing this using below script: returned=`tail -50 SapLogs.log | grep -i -E "Error|"` echo $returned ... (2 Replies)
Discussion started by: jedzio
2 Replies

2. UNIX for Advanced & Expert Users

sed not working as I expected. What can be the possible reason?

# cat test <abc> </abc> <abc> </abc> # sed 's/abc/checker/' test>test1 # cat test1 <checker> </checker> <checker> </checker> # I want the first instance of abc to be changed to checker. But all(rest 3 also) are changed. I din't used global (g) option too.. What can be the... (5 Replies)
Discussion started by: Ugenther
5 Replies

3. Shell Programming and Scripting

Why doesn't "grep -w" ALWAYS work?

Working with IP addresses is a pain... Here's my problem: I'm putting some interface information to a file: 3.185.201.2 | Tu1102 152.162.54.130 | Gi0/2.36 3.185.226.2 | Gi0/1 3.185.0.82 | Tu1 ... (12 Replies)
Discussion started by: turk22
12 Replies

4. Shell Programming and Scripting

pipe to grep doesn't work in bash script

Hi, I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script: cmnd="gvfs-mount -l | grep -i... (4 Replies)
Discussion started by: kcstrom
4 Replies

5. Shell Programming and Scripting

tail -XXX with grep doesn't work in while loop

Hi all, I need some help. my shell script doesn't work especially in the loop. #!/bin/sh -xv export ORA_ADMIN=/oracle/home/admin export ORACLE_SID=ORA_SID cat ${ORA_ADMIN}/param_alert_log.ora | while read MSG do #echo $MSG #echo "tail -400... (8 Replies)
Discussion started by: sidobre
8 Replies

6. Shell Programming and Scripting

two grep in one script doesn't work?

Hi there, the following script doesn't work. the first part works, then the second 'grep' fails with ': not found'. However, if I take out the second part (starting with the grep command) and put in a seperate script, it works. everyone know what's wrong here? no two 'grep' in one script, that... (2 Replies)
Discussion started by: monkey77
2 Replies

7. UNIX for Dummies Questions & Answers

Help! "grep" doesn't work for me!

totally pis*ed off. I have a data set (xxx.txt), as follows: chr1 3821 rs127372 A/C 0.823 chr1 3822 rs127376 A/C/G 0.899 chr1 3722 rs612634 A/C 9.22 chr1 3262 rs7152 A/T 0.22 chr1 3711 rs737 A/C/G 0.2323 ....... I only want to get those lines... (6 Replies)
Discussion started by: kaixinsjtu
6 Replies

8. Shell Programming and Scripting

Script doesn't work as expected when run on cron

The script checks for free space stats on Oracle. If there are any tablespaces with more than 85% usage it prints the details of the tablespace. If all the tablespaces have more than 15% free space, then "All tablespaces have more than 15 pct free space" must be printed on the screen. When I run... (2 Replies)
Discussion started by: RoshniMehta
2 Replies

9. Solaris

grep -e doesn't work on solaris

grep -e doesn't work in Soalris. Same script with grep -e worked on AIX/HP/LINUX.. I would like to search a list of patterns on "log.txt" like ... grep -e FATAL -e ERROR log.txt I get the error message as grep: illegal option -- e Usage: grep -hblcnsviw pattern file . . . (3 Replies)
Discussion started by: jmkraja
3 Replies

10. Shell Programming and Scripting

grep doesn't work within shell script?

I am trying to run the following code from a script file but it complains that syntax of (both instances of) grep is wrong. When I copy and paste it to the terminal, it is OK. Any idea what the problem might be? set i = `grep -c #define flags.h` while ($i>20) @ i-- my func (`cat... (4 Replies)
Discussion started by: barisgultekin
4 Replies
Login or Register to Ask a Question