Grep question


 
Thread Tools Search this Thread
# 1  
Grep question

Guys,

I'm a newbie for UNIX. I want to grep the last characters after the last "-" sign.

eg:

abcd-231-1-44
abcd-2341-22-55
abcd-1-23-6

In the above example. I want to grep just the last characters after the 3rd "-" sign. line1 it will be 44. line2 55 and line3 6

TIA
# 2  
grep matches lines.

how about awk?

Code:
awk -F- '{ print $NF }' filename

This User Gave Thanks to Corona688 For This Post:
# 3  
Code:
awk -F"-" '{print $NF}' file

This User Gave Thanks to bartus11 For This Post:
# 4  
You want to find lines with -nnn at the end of the line?

Code:
grep '-[0-9]+$'  somefile

Or do you want only those exact patterns?
Code:
grep  -e  '-55$'  -e  '-44$' -e '-6$'  somefile

This User Gave Thanks to jim mcnamara For This Post:
# 5  
Quote:
Originally Posted by bartus11
Code:
awk -F"-" '{print $NF}' file

awk -F- '{ print $NF }'

both the options work great. thanks a lot guys.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question on grep

Hello all, I'm trying to grep the string "scott" from all files whose names are like srvr*.log and that were created "Nov 15"...I'm trying the following command but throws an error message...seems like the syntax is incorrect.. grep scott < ls -l srvr*.log|grep "Nov 15" Thanks for your... (9 Replies)
Discussion started by: luft
9 Replies

2. Shell Programming and Scripting

Question about grep

is there anyway i can ask grep to only get the first line? as in the top command line line 1 <-- just grep this line line 2 line 3 ---------- Post updated at 04:24 PM ---------- Previous update was at 04:19 PM ---------- nvm.. found out that i can do it with |head (12 Replies)
Discussion started by: Nick1097
12 Replies

3. Shell Programming and Scripting

Question about grep

can anyone tell me what the \/$ means? from grep \/$ (8 Replies)
Discussion started by: Nick1097
8 Replies

4. UNIX for Dummies Questions & Answers

grep question

Instead of using the following command #dmesg | grep -v sendmail | grep -v xntpd How can I use just one grep -v and give both arguments. Please suggest thanks (4 Replies)
Discussion started by: Tirmazi
4 Replies

5. UNIX for Dummies Questions & Answers

Another grep question

Hi guys, I'm very new to unix but am liking it a lot so far, so please be gentle on this newb. I tried using the search for this question, but no luck. Anyways, i've got a few data fields... for example John Adress1 Adress2 Sam Adress3 Adress4 Jason Adress5 Adress6 Is... (4 Replies)
Discussion started by: Uss_Defiant
4 Replies

6. UNIX for Dummies Questions & Answers

grep question

Hi, I want to grep a word "success" from /home/user/ab123 and put it in a txt file. There are lot of directories under ab123 and subdirectories. So is it like this? /home/user/123: grep -i -R "success" > grepsuccess. Please give me a command... Thanks. (4 Replies)
Discussion started by: everurs789
4 Replies

7. Shell Programming and Scripting

Question of grep

As i understand the filter process of grep i was wondering, is it possible to to have a hidden word in a file(eg ------) and then use the grep filter to find a specific letter in that word which you can then replace with the letter in that word (eg ---a--) if it is please show me an example if it... (6 Replies)
Discussion started by: ZAXTHEGREAT
6 Replies

8. UNIX for Dummies Questions & Answers

grep question

I wanted to search a for all lines containing ERROR but not errors that contained the word "foo" (for example). The only way I could figure out to do it was: grep ERROR myfile.log | grep -v foo is there a way to do this with one grep command instead of two? One grep is faster than two,... (4 Replies)
Discussion started by: tim-bobby
4 Replies

9. UNIX for Dummies Questions & Answers

grep -f question

when using grep -f file1 file2 if you have multiple entries in the pattern file1 that are the same will it take the line out of file2 that matches file1 each time it comes up? if not by default can you set a flag to make this possible? or another way - can you get it to search for and match the... (8 Replies)
Discussion started by: Adriel
8 Replies

10. UNIX for Dummies Questions & Answers

Question about GREP

I have 2 files, in one file is a list of hex numbers, the other is what i need matched. Is it possible for me to specify to grep the list and have it go through the second file for each item in the list in the first file to match the lines? so this is like a cross-refference. (16 Replies)
Discussion started by: Adriel
16 Replies

Featured Tech Videos