extracting pattern from every line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extracting pattern from every line
# 1  
Old 08-31-2010
extracting pattern from every line

My scenario:

1. textfle
2. every line similar to:
"...____ your sister?is1are0am0Grammar point1_______ the chairs in..."
3. need to extract only the numbers in each line, eg 001 in the case above.

Tried different GREP/Sed combinations but...here I am

An output like that would be heaven: "Linenumber 001" (or whatever number combination)
# 2  
Old 08-31-2010
I think in your sample, the result should be "1001". Anyway try this:
Code:
perl -ne '@m=m/\d/g;print $. ." ";print @m;print "\n"' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-31-2010
try with grep -o option.
see the below example:
Code:
grep -o "[0-9]"  file name

This User Gave Thanks to ungalnanban For This Post:
# 4  
Old 08-31-2010
OMG Bartus 11, you nailed it! Wow! That's it.
PS: Right now trying to 'understand' the syntax.

-----------
to ungalnanban: yes, i tried this before but... Thank you anyway, man,
# 5  
Old 08-31-2010
using sed:
Code:
sed 's/[a-zA-Z._*? ]//g' file_name

# 6  
Old 08-31-2010
Quote:
Originally Posted by ungalnanban
using sed:
Code:
sed 's/[a-zA-Z._*? ]//g' file_name


You should use
Code:
 
sed 's/[^0-9]//g' filename

# 7  
Old 08-31-2010
Uf, as I can't figure it out, how could your perl script it be implemented to find any other patterns?

---------- Post updated at 02:12 AM ---------- Previous update was at 02:10 AM ----------

malikshahid85 yes, that would also do, although the line numbers are missing. Great job, man, too!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

4. Shell Programming and Scripting

Extracting a certain pattern..

Hi All, Suppose i have 4 coloumns in a excel sheet. Col A Col B Col C Col D 123 time1 abc 8 231 time2 xyz 6 324 time3 abc 4 456 time4 xyz 3 132 time5 abc 2 I want the data of coloum A... (3 Replies)
Discussion started by: ankitknit
3 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. UNIX for Dummies Questions & Answers

Extracting line1 from a file with certain file pattern in line 7

Hello there, I am new to unix and would like to do the following, hoping someone would give some guide, thanks in advance. Lets say i have a file like this: A w x y w x 0.1 B w x y w x 0.3 C w x y w x 0.7 D w x y w x 0.9 E w x y w x 0.2 So i would like to extract line 1 data where line... (2 Replies)
Discussion started by: seiksoon
2 Replies

7. Shell Programming and Scripting

Extracting a string matching a pattern from a line

Hi All, I am pretty new to pattern matching and extraction using shell scripting. Could anyone please help me in extracting the word matching a pattern from a line in bash. Input Sample (can vary between any of the 3 samples below): 1) Adaptec SCSI RAID 5445 2) Adaptec SCSI 5445S RAID 3)... (8 Replies)
Discussion started by: jharish
8 Replies

8. Shell Programming and Scripting

extracting matched pattern from a line using sed

I am trying to pull certain pieces of data out of a line of a file that matches a certain pattern: The three pieces that I want to pull out of this line are the only occurrences of that pattern within the line, but the rest of the line is not consistent in each file. Basically the line is... (3 Replies)
Discussion started by: ellhef
3 Replies

9. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

10. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies
Login or Register to Ask a Question