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
# 8  
Old 04-24-2010
A sed one:
Code:
sed "1s/\(.*\|\).*/\1First Month/;\$s/\(.*\|\).*/\1Last Month/"

The solution from Franklin52 works fine, but if mean there may be more than 3 fields (but the value to change is always in the last field), you can modify it slightly:

Code:
awk -F"|" '/Jan/{$NF="First month"} /Dec/{$NF="Last month"}1' OFS="|" file

# 9  
Old 04-25-2010
Hello scottn,

Both solutions you gave and Franklin52´s work perfect.

The only thing is that in this case a would like to have an AWK script
to do this, in order to include it in a main AWK script.

Testing, testing and viewing the way awk assign equivalences, I get the correct code forcing awk to search and replace independently and without take like a reference the content in other columns.

It was only missing use "==" instead of "=" within the "if" statement as follow:

Code:
awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} {if($3 && NR==1) sub(/normal month/,"first month");print}' file
awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} {if($3 && NR==12) sub(/normal month/,"Last month");print}' file

With above script I´m able to replace first and last values in 3erd column, but separately.
Now I hope somebody may help me to improve and finish this script in this 2 last issues:

1-) How to join both awk lines within a unique AWK script?
I´ve tried with script below putting a END statement but only prints the last line:

Code:
awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} 
    {if($3 && NR==1) sub(/normal month/,"first month");
     if($3 && NR==12) sub(/normal month/,"Last month")} END{print}' file

2-) I can get the total number of lines with
Code:
LastLine=$(awk 'END{print NR}' file)

, but I don´t now how to include this variable in the part that searches and replaces last line (I mean, instead of NR==12, put a variable something like NR==LastLine).


Thanks again for your kindly help.
# 10  
Old 04-25-2010
Something like this?
Code:
awk -v lastline=$(wc -l < file) '
BEGIN {OFS=FS="|"; IGNORECASE=1} 
NR==1{...}
NR==lastline{...}
1' file

# 11  
Old 04-25-2010
Quote:
Originally Posted by cgkmal
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
Code:
awk -F'|' 'FNR==1{$NF="something";prev=$0;next}{print prev;prev=$0}END{match(prev, "[^|][^|]*$"); print substr(prev,1,RSTART-1) "something"}' OFS='|' myFile

# 12  
Old 04-25-2010
Great and precise. And it evaluates the last line internally, it works how I need it, perfect!

vgersh99
, many thanks for your support and kindly help.
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