awk script, find pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users awk script, find pattern
# 1  
Old 06-03-2008
awk script, find pattern

I have a .csv file with columns. In column I like to find which rows contain a certain pattern. That is, my column is:
CNR
CP
CSN
CTC
EF.DB.U
CTC.A
D.DB

Here, I like to do one thing with EF.DB.U and another with all other. So my question is; how should my if statement look like to detect rows with ".DB" ?

if (name=="./.DB.") {
do something
} else {
{ do another thing
}

This is what I tried but it did not work.
lulle
# 2  
Old 06-03-2008
There is a tool missing, that is scanning the lines for your patterns like grep, or awk, or sed, ...
Depends on what you want to do with it, there are several ways.
# 3  
Old 06-03-2008
@zaxxon: Thread subject suggests "awk"

In idiomatic awk, the condition is put before the action, like this:

Code:
awk '/\.DB\./ { do things with .DB. ; next }
{ this is the else part }

The next forces the script to bypass the default branch, and so that is only taken when \.DB\. is not matched.

Of course, if you are inside the default action already for other reasons, you can use something like

Code:
if ($1 ~ /\.DB\/) { yada yada } else { no yada }

$1 is the first field, $0 is the entire input line. If your example is representative, you could use either of those in the condition.
# 4  
Old 06-04-2008
@era
That's a point, indeed. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find specific pattern and change some of block values using awk

Hi, Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ? I got the keys and the values from a command similar to: echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1... (2 Replies)
Discussion started by: alex2005
2 Replies

2. Shell Programming and Scripting

sed and awk to find pattern and add priffix

Original File Server1|poweredOn|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Need Output Server1|poweredOn|DR|T1|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Conduction to check find the string "SFCHT1" and "SR" and add prefix has... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

3. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

4. Shell Programming and Scripting

awk to find pattern in file

Hi I am using awk to find the pattern from the file, but it is not displaying anything. Probably I am missing some syntax in expression. FILE xxxx.Merge.exchMon.BY.qTime 120 read xxxx.Merge.exchMon.BY.qStart 09:55 read... (9 Replies)
Discussion started by: sdosanjh
9 Replies

5. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

6. 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

7. Shell Programming and Scripting

[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills": I need to find a pattern /VEL/ in an input file that looks like this: 1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736 ... (3 Replies)
Discussion started by: origamisven
3 Replies

8. Shell Programming and Scripting

[Solved] Find duplicate and add pattern in sed/awk

<Update> I have the solution: sed 's/\{3\}/&;&;---;4/' The thread can be marked as solved! </Update> Hi There, I'm working on a script processing some data from a website into cvs format. There is only one final problem left I can't find a solution. I've processed my file... (0 Replies)
Discussion started by: lolworlds
0 Replies

9. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

10. Shell Programming and Scripting

awk command to find particular pattern in file.

Hi I am using the following command to look for anything other than "0000" in a comma seperated file on 11th field. Note: I am looking for "0000" including the double quotes. nawk -F"," '$11!='"0000"'{print $11}' file This is showing incorrect result. Help is appreciated (2 Replies)
Discussion started by: pinnacle
2 Replies
Login or Register to Ask a Question