How to filter out consecutive occurence of digits from file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to filter out consecutive occurence of digits from file?
# 1  
Old 07-13-2016
How to filter out consecutive occurence of digits from file?

Hello,

I have a text file containing the following for example:
Code:
This number 123456 is in line number one.
Line number two contains -333444 number.
The third line has a 111111 in between and also 435.
Line number four has :444444 in it.
Line five has a in front like a555555.

how can i filter out all the six-digit numbers in the text i.e the required output is:

Code:
123456
333444
111111
444444
555555

Thanks
# 2  
Old 07-13-2016
Hello james2009,

Could you please try following and let me know if this helps you. It will remove all 6 consecutive digits from each line.
Code:
awk  '{gsub(/[0-9]{6}/,X,$0);print}'   Input_file

EDIT: If you need to get as output from Input_file for all 6 consecutive digits then following may help you in same.
Code:
awk '{match($0,/[0-9]{6}/);print substr($0,RSTART,RLENGTH)}'   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-13-2016 at 11:56 AM.. Reason: Added possible solution
# 3  
Old 07-13-2016
Hi ravinder,

Thanks but it didn't work.
# 4  
Old 07-13-2016
I think the OP wants to see the numbers (works only with GNU tr command on Linux):
Code:
 awk  '{ for(i=1;i<=NF; i++)
          if( $(i) ~ /[0-9]{6}+/)
          {
            printf("%s\n", $(i) )
          }
       }'  filename | tr -dc '[[:digit:][:cntrl:]]'
123456
333444
111111
444444
555555

# 5  
Old 07-13-2016
Hi.

Also:
Code:
grep -E -o '[0-9]{6}' <file>

producing:
Code:
123456
333444
111111
444444
555555

on a system:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30
grep (GNU grep) 2.20

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Old 07-13-2016
Thanks drl. It worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. UNIX for Dummies Questions & Answers

Filter records in a huge text file from a filter text file

Hi Folks, I have a text file with lots of rows with duplicates in the first column, i want to filter out records based on filter columns in a different filter text file. bash scripting is what i need. Data.txt Name OrderID Quantity Sam 123 300 Jay 342 498 Kev 78 2500 Sam 420 50 Vic 10... (3 Replies)
Discussion started by: tech_frk
3 Replies

3. Shell Programming and Scripting

Replace second to last but one consecutive occurence

Hi Unix Mates, I have requirement where in which I have to replace second to last -1 occurence of space with a word and last occurence of a space with a word and a space,input would be a file. Desired result: Second consecutive to last-1 occurence of space replace with: HRU LAST... (5 Replies)
Discussion started by: Kingcobra
5 Replies

4. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

5. Shell Programming and Scripting

awk filter by occurence of at least two columns

Hi, Using AWK script I want to pick those rows that has AT LEAST TWO columns EACH has a count >=3. i.e. two conditions: at least two columns, each of which has a count at least 3. There must be a simple way to do this job, but could not figure it out by myself. Input file (thousand of... (3 Replies)
Discussion started by: yifangt
3 Replies

6. Shell Programming and Scripting

Split a file into multiple files based on first two digits of file.

Hi , I do have a fixedwidth flatfile that has data for 10 different datasets each identified by the first two digits in the flatfile. 01 in the first two digit position refers to Set A 02 in the first two digit position refers to Set B and so on I want to genrate 10 different files from my... (6 Replies)
Discussion started by: okkadu
6 Replies

7. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

8. Shell Programming and Scripting

Removing consecutive lines in a file

We have very large transaction logs that have transactions which start with a line that starts with 'Begin :' and ends with a line that starts with 'End :'. For most transactions there is valid data between those two lines. I am trying to get rid of lines that look like this: Begin :... (11 Replies)
Discussion started by: deneuve01
11 Replies

9. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

10. UNIX for Dummies Questions & Answers

Cutting n consecutive lines from a file...

Hi, I have this problem of separating 10 consecutive lines from a file, say starting from 21 to 30... I have used a filter like this.. head -n 30 myfile | tail -n 10 Is there a simpler way than this? (2 Replies)
Discussion started by: Vishnu
2 Replies
Login or Register to Ask a Question