How to cut string and find missing pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to cut string and find missing pattern?
# 1  
Old 05-01-2012
Error How to cut string and find missing pattern?

i have list in file named sample.txt
eg
Quote:
qm01121412
qm02121412
qm03121412
qm05121412
qm07121412
.
.
.
qm24121412
i want to cut the 3rd and 4th character i.e. 01,02,03....,24(max length is 24)
and i want to find the missing sequence .and display them


i.e.
Quote:
seq qm04121412 and qm06121412 is missing in above series.
# 2  
Old 05-01-2012
Try:
Code:
awk '{a[int(substr($0,3,2))]=1;n1=substr($0,1,2);n2=substr($0,5)}END{for (i=1;i<=24;i++) if (!(i in a)) print n1""sprintf("%02d",i)""n2}' sample.txt

# 3  
Old 05-01-2012
Code:
for i in {01..24}; do grep -q "^qm$i" input.txt || echo "qm"$i"21412"; done

# 4  
Old 05-01-2012
this is the exact content of file

Quote:
qm00010512.
qm01010512.
qm02010512.
qm03010512.
qm04010512.
qm05010512.
qm06010512.
qm07010512.
qm08010512.
qm09010512.
qm10010512.
qm11010512.
qm12010512.
after giving the command its giving the output list from 01 to 24 but missing pattern is from 13 to 24


the command i am giving is
Code:
grep "qm.*010512.*"|awk '{a[int(substr($0,3,2))]=1;n1=substr($0,1,2);n2=substr($0,5)}END{for (i=1;i<=24;i++) if (!(i in a)) print n1""sprintf("%02d",i)""n2}' sample.txt


Last edited by sagar_1986; 05-01-2012 at 08:25 AM..
# 5  
Old 05-01-2012
Did you try my solution?
# 6  
Old 05-01-2012
ofcourse, change the highlighted

Code:
s$ for i in {01..24}; do grep -q "^qm$i" test.txt || echo "qm"$i"21412"; done
qm1321412
qm1421412
qm1521412
qm1621412
qm1721412
qm1821412
qm1921412
qm2021412
qm2121412
qm2221412
qm2321412
qm2421412

what is your OS ?
# 7  
Old 05-01-2012
how to cut string and find missing pattern

Hi Bart,

Code:
awk '{a[int(substr($0,3,2))]=1;n1=substr($0,1,2);n2=substr($0,5)}END{for (i=1;i<=24;i++) if (!(i in a)) print n1""sprintf("%02d",i)""n2}'

As far as I know end section willl not able to remember the value of n1 and n2 subtrings and also END section will be executed finally so I believe you have to save the values of n1 and n2 in some array then call in END for appending ... Please correct if i am wrong.

Thanks
Krsnadasa
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. Shell Programming and Scripting

awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the - and last id in it, indicated by the digit after the -, I am trying to use awk to print the missing line or lines in file following the pattern of the previous line. For example, in the file below the next... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

4. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

find: missing argument to `-exec' while redirecting using find in perl

Hi Friends, Please help me to sort out this problem, I am running this in centos o/s and whenever I run this script I am getting "find: missing argument to `-exec' " but when I run the same code in the command line I didn't find any problem. I am using perl script to run this ... (2 Replies)
Discussion started by: ramkumarselvam
2 Replies

7. Shell Programming and Scripting

How do i find the first number in each line and insert dummy string into the missing columns?

Hi, I have one input file with the following content: MY_inpfile.txt Aname1 Cname1 Cname2 1808 5 Aname2 Cname1 1802 47 Bname1 ? 1819 22 Bname2 Cname1 1784 11 Bname3 1817 9 Zname1 Cname1 1805 59 Zname2 Cname1 Cname2 Cname3 1797 27 Every line in my input file have a 4 digit... (5 Replies)
Discussion started by: Szaffy
5 Replies

8. Shell Programming and Scripting

Find missing string with bash

If I have a file called file A with a list of filenames. How do I find all the filenames in file A that aren't contained in file B? I want to use bash scripting. (2 Replies)
Discussion started by: locoroco
2 Replies

9. UNIX for Dummies Questions & Answers

cmd sequence to find & cut out a specific string

A developer of mine has this requirement - I couldn't tell her quickly how to do it with UNIX commands or a quick script so she's writing a quick program to do it - but that got my curiousity up and thought I'd ask here for advice. In a text file, there are some records (about half of them)... (4 Replies)
Discussion started by: LisaS
4 Replies

10. Shell Programming and Scripting

cut subsrting from string depending on pattern

Dears, i'm glad to be a member of this big UNIXer's group :( ,hoping to be one of'm. my question: how i get the subsrting ahmad "from" the string ".ahmad." for example ?? Thank you all. (5 Replies)
Discussion started by: Muslem_it
5 Replies
Login or Register to Ask a Question