What am I doing wrong in awk with multiple tests?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What am I doing wrong in awk with multiple tests?
# 1  
Old 03-11-2014
What am I doing wrong in awk with multiple tests?

Code:
awk -v pat="$pattern" 'NR == 1 {print $0}; $0 ~ pat {print $0}' infile.csv > outfile.csv

The first row of my file contains headers so I want them. Otherwise, I want only lines containing the BASH variable pattern which I am passing to awk with -v.

This is giving me all the lines containing the pattern but not the first line.

Mike
# 2  
Old 03-11-2014
What operating system are you using?
# 3  
Old 03-11-2014
Quote:
Originally Posted by bartus11
What operating system are you using?
Bash 4.1.10(4)
GNU Awk 4.0.2
CYGWIN_NT-6.1 1.7.17
# 4  
Old 03-11-2014
Can you show us:
Code:
head infile.csv | cat -ev

# 5  
Old 03-11-2014
Except for a little IP yes. csv file is unix type EOL

Code:
timestamp,sequence_number,alarm_id,alarm_class,resource,logged_by,reference,prev_state,log_action,final_state,alarm_message,generation_time,Comments,Duration,Name,alarm_code $
26-FEB-2013 08:44:08.000,xxxxxx,xxxxxx,EVENT,xxxxxx,xxxxxx,xxxxxx,G,R,R,xxxxxx,26-FEB-2013 08:43:04.000,,64,,NONE,xxxxxx$

Mike

---------- Post updated at 04:30 PM ---------- Previous update was at 04:20 PM ----------

Just to be clear the second rule is working. The first rule (NR == 1) is not.

Mike
# 6  
Old 03-11-2014
What is the output of:
Code:
awk 'NR==1' infile.csv

# 7  
Old 03-11-2014
I had a stupid error in a directory pointer in the surrounding script. Turns out the awk part was working just fine. I can't believe how much time I wasted on that error.

Mike
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Pattern Matching for PING tests

I have a script that logs into a server and pings several other servers in order to verify IP path between servers. The output can look like this, if good pings: Response from 1.1.1.4;_id=0, vlan_prio=0): seq=0 time=91.547 ms. Response from 1.1.1.4;_id=0, vlan_prio=0): seq=1 time=61.176 ms.... (7 Replies)
Discussion started by: he204035
7 Replies

2. Shell Programming and Scripting

What is wrong with my awk code?

Hi there, I am trying to select a number of lines based on the lat. lon columns in a file but my awk code gives me empty result. Here is my file: 20100213 102212 33.1185 39.4078 2.9 20100214 141753 33.1058 39.9068 2.9 20100218 115828 33.1907 39.3575 2.9 20100225 220001 33.1932 39.9448... (10 Replies)
Discussion started by: johankor
10 Replies

3. Shell Programming and Scripting

What's wrong with my awk

file1: maximum_delay time: 102.794 ms maximum_delay time: 92.977 ms maximum_delay time: 98.895 ms maximum_delay time: 96.891 ms maximum_delay time: 86.966 ms maximum_delay time: 95.91 ms maximum_delay time: 98.921 ms maximum_delay time: 89.881 ms maximum_delay time: 92.931 ms... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

4. Shell Programming and Scripting

What's wrong with this awk?

I have a file and if I'm doing following action on that file it's coming up correctly awk -F"," '/DATA_TYPE/ { cnt += $3 } END { print " DATA_TYPE count=" cnt}' inter DATA_TYPE count=19593131 now if I'm changing same AWK, so that now it can accept variable, then it's somehow not working.... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

5. Shell Programming and Scripting

what's wrong with my awk

echo "abc:bcd" |awk '{split($0,b,":");print "b" is good }' I want to "bcd" is good , anything is wrong with my, please don't change my thought with split, I just want to know what's wrong :o (1 Reply)
Discussion started by: yanglei_fage
1 Replies

6. Shell Programming and Scripting

what's wrong with my awk line?

] && { echo "The free mem need to be more than 2G" } (3 Replies)
Discussion started by: yanglei_fage
3 Replies

7. Shell Programming and Scripting

What's wrong with this awk?

ZSCORE=$(awk "BEGIN {if($STDEVIATE>0) {print ZSCORER=$(awk "BEGIN{print (${ALL} - ${AVERAGE}) / ${STDEVIATE}}")}else print "0"}") awk: fatal: division by zero attempted awk: BEGIN {if(0==0) {print ZSCORER=}else print 0} awk: ^ syntax error ALL=9 STDEVIATE=0... (2 Replies)
Discussion started by: SkySmart
2 Replies

8. Shell Programming and Scripting

Prevent wrong user from using shell script for multiple remote servers

Hi, I am running a shell script from a central server to multiple remote servers using the following code: application_check() { # Linux/UNIX box with ssh key based login SERVERS=`cat /tmp/server-details` # SSH User name USR="user" # create new file > /tmp/abc.log # connect... (2 Replies)
Discussion started by: mystition
2 Replies

9. Shell Programming and Scripting

awk... what is wrong?

Hi there, what is wrong with this? grep ">>" alarms_temp | awk '{print substr($5, 2), $7}' | read var1 var2 echo "$var1" echo "$var2" Both variable are empty, but if i run: grep ">>" alarms_temp | awk '{print substr($5, 2), $7}' I have: 0 9 as a result. (2 Replies)
Discussion started by: marimovo
2 Replies

10. UNIX for Dummies Questions & Answers

Combining awk tests

It would be convenient to be able to combine awk tests. For example, suppose that I do this query: awk '$1 != "Bob" || $1 != "Linda" {print $2}' datafileIs there a reasonable way to combine the conditions into a single statement? For example, in egrep, I can do: egrep -v "Bob|Linda"... (4 Replies)
Discussion started by: treesloth
4 Replies
Login or Register to Ask a Question