OR operator syntax question in AWK script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting OR operator syntax question in AWK script
# 1  
Old 04-24-2010
OR operator syntax question in AWK script

Hi guys,

I confused about syntax used in OR script as follow:

I have this sample file separated by "|" containing:

Code:
January|Month No. 1
February|Month No. 2
March|Month No. 3
April|Month No. 4
May|Month No. 5
June|Month No. 6
July|Month No. 7
August|Month No. 8
September|Month No. 9
October|Month No. 10
November|Month No. 11
December|Month No. 12

And I´m excluding 6 lines containing the first 6 months with this awk script (and works fine):
Code:
awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} $1 !~ /january|february|march|april|may|june/' file

July|Month No. 7
August|Month No. 8
September|Month No. 9
October|Month No. 10
November|Month No. 11
December|Month No. 12

The problem is when I try to write the same one-line command in 2 or more lines, because I get errors, I mean:
Code:
awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} $1 !~ /january|february|
march|april|may|june/' file

awk: BEGIN {OFS=FS="|"; IGNORECASE=1} $1 !~ /january
awk:                                         ^ unterminated regexp
awk: cmd. line:1: |february|march|april|may|june/
awk: cmd. line:1: ^ syntax error


What I´m doing wrong? how can I fix it?

* I try to do this because in the real file I need to exclude more than 20 patterns from a column and the one-line command becomes too long.

Any help would be appreciated.

Thanks.
# 2  
Old 04-24-2010
add '\' at end

Quote:
#awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} $1 !~ /january|february| \
> march|april|may|june/' 1.txt
July|Month No. 7
August|Month No. 8
September|Month No. 9
October|Month No. 10
November|Month No. 11
December|Month No. 12

Thanks,
penchal
# 3  
Old 04-24-2010
Hey penchal,

Many thanks, the magic character "\" was missing for me, great to know now.


One more question regarding this:


Independently and without use what I have in other columns, how can

I replace a field value in specific line using AWK?

Inputfile (In 3rd column all lines contain the same pattern)

Code:
January|Month No. 1|Normal month
February|Month No. 2|Normal month
March|Month No. 3|Normal month
April|Month No. 4|Normal month
May|Month No. 5|Normal month
June|Month No. 6|Normal month
July|Month No. 7|Normal month
August|Month No. 8|Normal month
September|Month No. 9|Normal month
October|Month No. 10|Normal month
November|Month No. 11|Normal month
December|Month No. 12|Normal month

I would like to replace only the first and last line in column 3 as follow:
Code:
January|Month No. 1|First month
February|Month No. 2|Normal month
March|Month No. 3|Normal month
April|Month No. 4|Normal month
May|Month No. 5|Normal month
June|Month No. 6|Normal month
July|Month No. 7|Normal month
August|Month No. 8|Normal month
September|Month No. 9|Normal month
October|Month No. 10|Normal month
November|Month No. 11|Normal month
December|Month No. 12|Last month

I´m not sure how to use the "if" and "and" conditions together. I´m trying for the first part with this code, but the result is not what I need, because this script replaces all ocurrences:
Code:
awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} {if($3 && NR=1) sub(/Normal month/,"First month",$3); print}' inputfile

January|Month No. 1|First month
February|Month No. 2|First month
March|Month No. 3|First month
April|Month No. 4|First month
May|Month No. 5|First month
June|Month No. 6|First month
July|Month No. 7|First month
August|Month No. 8|First month
September|Month No. 9|First month
October|Month No. 10|First month
November|Month No. 11|First month
December|Month No. 12|First month

Then, how can I replace values for first and last lines within same AWK script?

Maybe somebody could help me with this.

Thanks in advance.
# 4  
Old 04-24-2010
Something like this?
Code:
awk -F"|" '/Jan/{$3="First month"} /Dec/{$3="Last month"}1' OFS="|" file

# 5  
Old 04-24-2010
Hi Franklin,

Thanks for your reply. This works, but is there a way to force awk to see
in specific line and column?

I think something like and IF statement similar to
Code:
if($3 && NR=1) 
{code to replace string}
print $0


I ask in this way because the content in other columns different to column 3 is variable depending the file I´m processing.

Thanks in advance.
# 6  
Old 04-24-2010
Code:
nawk -F'|' 'FNR==1{$NF="somethingElse";prev=$0}{print prev;prev=$0}END{match(prev, "[^|][|]*$"); print substr(prev,1,RSTART-1) OFS "somethingElse"}' OFS='|' myFile

# 7  
Old 04-24-2010
Thanks verseg for your help.

I´ve tested and the output It´s almost complete, but it looks that
modifies the second line and last line in different way.

*I´ve used AWK instead nawk, my cygwin doesn´t know nawk.


Code:
awk -F'|' 'FNR==1{$NF="First month";prev=$0}{print prev;prev=$0}END{match(prev, "[^|][|]*$"); print substr(prev,1,RSTART-1) OFS "Last month"}' OFS='|' Dzdi_temp

January|Month No. 1|First month
January|Month No. 1|First month --> 2nd line was modified
February|Month No. 2|Normal month
March|Month No. 3|Normal month
April|Month No. 4|Normal month
May|Month No. 5|Normal month
June|Month No. 6|Normal month
July|Month No. 7|Normal month
August|Month No. 8|Normal month
September|Month No. 9|Normal month
October|Month No. 10|Normal month
November|Month No. 11|Normal month
December|Month No. 12|Normal month|Last month-->Added a new column without replace


How to fix this?

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting syntax error with awk ternary operator

split($7,a," "); date = a; time = a split(date,d,"/"); month = sprintf("%02d",d); day = sprintf("%02d",d); year = 2000 + d % 100 split(time,t,":"); hour=t; min=t hour >= 12? { hour=hour-12; amPm=" PM" } : amPM=" AM" hour == 0? hour=12 time=sprintf("%02d",hour)":"sprintf("%02d",min)amPm ... (4 Replies)
Discussion started by: Michael Stora
4 Replies

2. Shell Programming and Scripting

syntax question in regards to nested awk statements

Hello all, I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop. Here's my input file # cat databases.lst #NOTE: These entries are delimited by tabs "\t" #oracleSID name/pass # db11 ... (2 Replies)
Discussion started by: Keepcase
2 Replies

3. UNIX for Dummies Questions & Answers

syntax error: invalid arithmetic operator

hi, i have a bash script that i want to receive a a string from another bash file. But because the string has a dot in the middle it gives me an error. The error is in this line: let valor=$1 and the value passed is rules.txt the error is: let: valor=rules.txt: syntax error: invalid... (2 Replies)
Discussion started by: limadario
2 Replies

4. Shell Programming and Scripting

awk syntax question

Hi I use awk command to delete the first blanc line of a file: awk '/^$/ && !f{f=1;next}1' infile > outfile can somebody please explain me what the last "1'" in !f{f=1;next}1' stands for... Thansk a lot -A (3 Replies)
Discussion started by: aoussenko
3 Replies

5. Shell Programming and Scripting

zsh ternary operator syntax help

Hi, Can someone give me an example of how to use zsh's ternary operator? I tried: # a=1 # c=( a ? "true" : "false" ) and got: zsh: no matches found: ? I'm running zsh 4.2 on RHEL AS 4. Thanks! Paul (1 Reply)
Discussion started by: paul99
1 Replies

6. Shell Programming and Scripting

syntax error in shell test: unknown operator

Hi All, can some one figure out the syntax issue here. How to overcome this? #!/bin/sh $ HFR_MAIL=NO $ PRP_MAIL=NO $ MC_MAIL=NO $ if && && ]; then > echo "NO " > else > echo "YES" > fi test: unknown operator NO $ if && && ]; then > echo "NO" > else > echo "YES" >... (4 Replies)
Discussion started by: shellscripter
4 Replies

7. Shell Programming and Scripting

syntax error: `-a' unexpected operator/operand in IF

When i tyr this, it gives me a syntax error...i tried removing quotes,removing spaces,replacing -eq with '='.. Can somebody suggest that is the problem? if ]; then (4 Replies)
Discussion started by: dba.admin2008
4 Replies

8. UNIX for Dummies Questions & Answers

AWK syntax question

Hi, Have to check file names in some given directory. SO, What is the right syntax here: *$3*=="'$object_list'" - just wanted to check if $3 is in the object_list. And also, Do I need so many quotes around? (5 Replies)
Discussion started by: Leo_NN
5 Replies

9. Shell Programming and Scripting

yet another awk field syntax question

I am trying to print the remaing fields and field numbers beginning with a field 'xyz' #cat abc test1:test2:xyz:test3:test4:test5 #cat def test1:test2:test3:xyz:test4:test5 desired output is to be able to print NF and any trailing fields separated by':' test3 3 or test4 3 or test5... (4 Replies)
Discussion started by: prkfriryce
4 Replies

10. Shell Programming and Scripting

awk syntax question

Hi there could someone explain what is happening in the following function/statement for me, im just a little confused code = 'BEGIN{FS=","} { printf ("%-11s,%s%s%s,%07.2f,%14s,%-3s\n",$1,substr($2,9,2),substr($2,6,2),substr($ 2,3,2),$9,$10,$12) } this function is called later in the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question