Extracting entries starting with xxx


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting entries starting with xxx
# 1  
Old 02-07-2013
Extracting entries starting with xxx

Hello,
I am trying to extract all words (entries) from a file beginning with 123. For example:

Code:
ververv ewfaafa 123asd 4334j fdgfgf
fdfdssd 123890 xxxxx eeee sss 1234 sdfsf were sfs fbdsdfb
fsdg sdgfsd bfg sdfb dsfb 
sdfg sdf ergerg 123pop sdfgdfg 123ww 123qq dffg

Desired output:
Code:
123asd
123890
1234
123pop
123ww
123qq

Thanks so much!

Last edited by Scrutinizer; 02-07-2013 at 09:04 PM.. Reason: code tags
# 2  
Old 02-07-2013
Code:
awk '{ for(i=1;i<=NF;i++) if($i ~ /^123/) print $i; }' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-07-2013
Just to provide an alternative, assuming that your words are space-separated:

Code:
tr ' ' '\n' < yourfile | grep ^123

# 4  
Old 02-07-2013
Or if you have GNU grep:
Code:
grep -o '123[a-zA-Z0-9]*' file

EDIT: My bad, please ignore this post. Thanks Scrutinizer for pointing that out.

Last edited by Yoda; 02-07-2013 at 09:15 PM.. Reason: My bad, please ignore this post. Thanks Scrutinizer for pointing that out.
# 5  
Old 02-07-2013
The latter command would also print when 123 is not the start of a word, for example:
Code:
$ echo 4123x | grep -o '123[a-zA-Z0-9]*'
123x

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

2. Red Hat

Gnome3 locksup on new Linux kernel 12.6.xxx & 12.5.xxx

Hi Forum Ive been having a problem with the kernal(s) for some strange reason it every time I try and access the date and time/calendar or system settings it locks up the whole laptop and nothing responds. :(. This doesn't happen 11.10.xxx kernel . Any help would be much appreciated and thank you... (1 Reply)
Discussion started by: ShinTec
1 Replies

3. Shell Programming and Scripting

Kill all proceess with the name XXX

Hello guys, I have a problem. How can i "kill -9" with the name process? I have a lot of process with the same name, but i do not want kill each one with the process ID. There is a comand to "kill -9" al process with name "XXXX". Thanks! (5 Replies)
Discussion started by: Xedrox
5 Replies

4. AIX

echo $varibla | mail -s "subject" "xxx@xxx.com" not ruuning as expected

Hi Folks, As per the subject, the following command is not working as expected. echo $variable | mail -s "subject" "xxx@xxx.com" Could anyone figure it out whats wrong with this. I am using AIX box. Regards, (2 Replies)
Discussion started by: gjarms
2 Replies

5. Shell Programming and Scripting

how to find entries, NOT starting with specific pattern

Hey,I have a file in following format >1 ABC........ >2 XYZ..... >3 ABC........ >4 MNO....... >5 ABC....... now I would like to find only those entries that doesn't start with ABC (specific pattern)e.g preferred output: >2 XYZ.... >4 MNO....... it will be nice if anybody how... (2 Replies)
Discussion started by: ankitachaurasia
2 Replies

6. Shell Programming and Scripting

Compiling multiple ".c" files starting with xxx

Hello, I am trying to figure out how I can write a bashscript that compiles several ".c" files that start with xxx (example: xxx_try.c and xxx_that.c) So I want to compile all these files with a bash script. Anyone can help pls? (6 Replies)
Discussion started by: Freak79
6 Replies

7. UNIX for Dummies Questions & Answers

grep XXX that not followed by YYY

I want to grep all lines containing blocking sess= that are not followed by 0x0 cat trace.log blocking sess=0x0 blocking sess=0x0 rrrr...... blocking sess=121 blocking sess=0x0 blocking sess=0x0 blocking sess=0x0 some other lines blocking sess=001 blocking sess=0x0 blocking sess=0x1... (6 Replies)
Discussion started by: ynixon
6 Replies

8. Shell Programming and Scripting

shell script for extracting out the shortest substring from the given starting and en

hi all, i need an urgent help for writing a shell script which will extract out and print a substring which is the shortest substring from the given string where first and last character of that substring will be given by the user. for e.g. if str="abcdpqracdpqaserd" now if the user gives 'a'... (18 Replies)
Discussion started by: pankajd
18 Replies

9. UNIX for Dummies Questions & Answers

xxx.tar.gz error

Hi All, We have taken data backup using gzip but when we try to restore from ex:xxx.tar.gz file got below error. invalid compressed data--crc error and invalid compressed data--length error How to recover my original data. Thanks in advance for your suggestion (1 Reply)
Discussion started by: bache_gowda
1 Replies

10. Shell Programming and Scripting

Extracting a substring starting from last occurance of a string/character

Hi All, This is Ram. I'm new to this forum & new to shell scripts as well. I've a requirement in which I want to extract a substring from a given string based on last occurance of a character. for eg. I have a string of a file name with absolute path like... (2 Replies)
Discussion started by: krramkumar
2 Replies
Login or Register to Ask a Question