awk with multiple regex and substring

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with multiple regex and substring
# 1  
Old 07-21-2011
awk with multiple regex and substring

Hi Experts,

I have a file on which i want to print the line which should match following criterias.

Line should not start with 0 or 9
Line should start with 1
576th character should not be 1 or 2
576-580 postion should not be NIPPF or CDIPB
576-581 postion should not be CDUSPF

I have written below code,but it is not displaying correct result though not giving any error. Please help me and thanks in advance.

/usr/bin/nawk '!/^[09]/ && /^1/ && ((substr($0,576,1) !~ "[12]")||(substr($0,576,5) !~ "NIPPF|CDIPB")||(substr($0,576,6) !~ "CDUSPF"))' a.txt >> result.txt

Last edited by Franklin52; 07-21-2011 at 04:58 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-21-2011
Line should not start with 0 or 9
Line should start with 1

If you put the condition /^1/ it will take care of the above two conditions

provide some sample data and the output
# 3  
Old 07-21-2011
I am not worried for this part as i tested it is working fine.But the problem i am facing in below portion
 ((substr($0,576,1) !~ "[12]")||(substr($0,576,5) !~ "NIPPF|CDIPB")||(substr($0,576,6) !~ "CDUSPF"))

---------- Post updated 07-21-11 at 02:00 AM ---------- Previous update was 07-20-11 at 10:56 PM ----------

I got the ans.

((substr($0,576,1) !~ "[12]")&&(substr($0,576,5) !~ "NIPPF|CDIPB")&&(substr($0,576,6) !~ "CDUSPF"))

During not operator it should be and not or.

Last edited by millan; 07-21-2011 at 05:11 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple regex in sed

I am using the following sed script to remove new lines (\r\n and \n), except from lines starting with >: sed -i ':a /^>/!N;s/\r\n\(\)/\1/;s/\n\(\)/\1/;ta' Is there a way to include both \r\n and \n in one regex to avoid the second substitute script (s/\n\(\)/\1/)? (4 Replies)
Discussion started by: Xterra
4 Replies

2. Shell Programming and Scripting

Regex: Extract substring between 2 separator

Hi Input: Output: dd I want to get the word after the last '-' until the first dot I have tried with regex lookbehind and lookahead like this: (?<=-).*(?=\.) but his returns too much (7 Replies)
Discussion started by: chitech
7 Replies

3. UNIX for Advanced & Expert Users

awk if/substring/append help

Hi All, I need some help with an awk command: What I'm trying to do is append "MYGROUP: " to text with the substring "AT_" the input file follows this format: AT_xxxxxx Name1 Name2 AT_xxxxxx NameA NameB I want the output to be: MYGROUP: AT_xxxxx Name1 Name2 MYGROUP:... (2 Replies)
Discussion started by: bikecraft
2 Replies

4. Shell Programming and Scripting

AWK: Substring search

Hi I have a table like this I want to know how many times the string in 2nd column appears in the first column as substring. For example the first string of 2nd column "cgt" occurs 3 times in the 1st column and "acg" one time. So my desired output is THank you very much in advance:) (14 Replies)
Discussion started by: polsum
14 Replies

5. UNIX for Dummies Questions & Answers

awk to match multiple regex and create separate output files

Howdy Folks, I have a list that looks like this: (file2.txt) AAA BBB CCC DDD and there are 24 of these short words. I am matching these patterns to another file with 755795 lines (file1.txt). I have this code for matching: awk -v f2=file2.txt ' BEGIN { while(... (2 Replies)
Discussion started by: heecha
2 Replies

6. UNIX for Dummies Questions & Answers

Multiple Substring Outputs

Hello, I am reading a file with millions of lines in it. Each line is big line containing several xml tags. I need to Output just the value of two tags in a seperate flat file. For eg- I need to output whats present in <ComponentName> something </ComponentName> and another tag is... (2 Replies)
Discussion started by: sunnybehl
2 Replies

7. Shell Programming and Scripting

Getting substring with awk

Hi Team, How to get the last 3 characters of a String irrespective of their length using awk? Thanks Kinny (5 Replies)
Discussion started by: kinny
5 Replies

8. Shell Programming and Scripting

Substring using sed or awk

I am trying to get a substring from a string stored in a variable. I tried sed with a bit help from this forum, but not successful. Here is my problem. My string is: "REPLYFILE=myfile.txt" And I need: myfile.txt (everything after the = symbol). My string is: "myfile.txt.gz.20091120.enc... (5 Replies)
Discussion started by: jamjam10k
5 Replies

9. UNIX for Dummies Questions & Answers

substring using AWK

can we do substring fuctionality using AWK say I have string "sandeep" can i pick up only portion "nde" from it. Thanks and Regards Sandeep Ranade (3 Replies)
Discussion started by: mahabunta
3 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question