compare minus symbol "-" with a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare minus symbol "-" with a column
# 1  
Old 02-12-2009
compare minus symbol "-" with a column

Need to compare minus symbol "-" with a column that contains minus on few rows.

the sample script awk I have written is

if ( '$COLUMN2' ~ "\-" ) {}

COLUMN2 is a shell variable. I just need the if condition checking the shell variable within the awk script. Below is the entire awk part of the script in case if my question is not clear.

newline=$(echo | awk '{
case1=0;
case2=0;
case3=0;
case4=0;
if ( '$COLUMN3' == 0 ) {
if ( case1 == 0 ){
if ( '$COLUMN5' > 0 ){
printf("%.0f,",'$COLUMN1')
printf("%c,",$COLUMN2)
printf("%.2f,",'$COLUMN3')
printf("%c,",$COLUMN4)
printf("%.2f,",'$COLUMN5')
printf("%s,",'$COLUMN6')
printf("%s,",'$COLUMN7')
case1++
printf("%s,","case1")
printf("%d\n",case1)}}
else if ( case4 == 0 ){
if ( '$COLUMN5' == 0 ) {
printf("%.0f,",'$COLUMN1')
printf("%c,",$COLUMN2)
printf("%.2f,",'$COLUMN3')
printf("%c,",$COLUMN4)
printf("%.2f,",'$COLUMN5')
printf("%s,",'$COLUMN6')
printf("%s,",'$COLUMN7')}
case4++
printf("%s,","case4")
printf("%d\n",case4)}}

else if ( '$COLUMN3' > 0.00 ){
if ( '$COLUMN2' ~ "\-" ){
if ( case3 == 0 ){
if ( '$COLUMN5' == 0 ) {
printf("%.0f,",'$COLUMN1')
printf("%c,",$COLUMN2)
printf("%.2f,",'$COLUMN3')
printf("%c,",$COLUMN4)
printf("%.2f,",'$COLUMN5')
printf("%s,",'$COLUMN6')
printf("%s,",'$COLUMN7')}
case3++
printf("%s,","case3")
printf("%d\n",case3)}}}
else if ( '$COLUMN3' < 0.00 ) {
if ( '$COLUMN2' ~ \\- ){
if ( case2 == 0 ){
if ( '$COLUMN5' == 0 )
{printf("%.0f,",'$COLUMN1')
printf("%c,",$COLUMN2)
printf("%.2f,",'$COLUMN3')
printf("%c,",$COLUMN4)
printf("%.2f,",'$COLUMN5')
printf("%s,",'$COLUMN6')
printf("%s,",'$COLUMN7')}
case2++
printf("%s,","case2")
printf("%d\n",case2) }}}}')
# 2  
Old 02-13-2009
I think your trying to use a shell variable in your awk script. Check the -v option in the awk man page.
# 3  
Old 02-13-2009
I was able to access the shell variable inside in awk. My actual question is how to compare with the symbol like minus "-" or "$" etc


~ "/-/"

or

~ \\-

But none works.
# 4  
Old 02-13-2009
No need to quote the dash (between forward slashes or not). It worked just like this:

Code:
-bash-3.00$ cat text
this is just a - test
-bash-3.00$ awk '{ if ( $5 ~ /-/ ) { print "Dash is #5"; } }' text
Dash is #5
-bash-3.00$

# 5  
Old 02-13-2009
awk: cmd. line:32: if ( - ~ /-/ ){
awk: cmd. line:32: ^ syntax error

I get this error at the line -- if ( '$COLUMN2' == /-/ ){
# 6  
Old 02-13-2009
Code:
bash-2.05$ echo '1,2,3,-,5' | nawk -F, '$4 ~ /-/ {print "dash in 4"}'
dash in 4
bash-2.05$ echo '1,2,3,-,5' | nawk -F, '$4 ~ "-" {print "dash in 4"}'
dash in 4

Work fine either way on Solaris. If on Solaris, try 'nawk' istead of 'awk'.
If not, try quoble-quoting the dash, e.g. "-"
# 7  
Old 02-18-2009
Please let me know a sample code to use a shell variable that contains the value of "-" to use it inside awk script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Replacing "." with "GG" in a certain column of a file that has heading

Hi, all, I have a file that looks like: ## XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ## YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY #AA AB AC AD AE AF AG AH AI AJ AK AL 20 60039 60039 ... (5 Replies)
Discussion started by: kush
5 Replies

3. Shell Programming and Scripting

How to allign output data in UNIX that is separated with a pipe "|" symbol ?

Experts , In the given output of the log file, the 2nd field that is separated by "|" pipe is not aligned well due to the uneven data length, I would like it to align the 2nd column with 37 length (that is disturbed in the output) including the pipe . The two pepe "|" would be in a aligned way... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

Substituting comma "," for dot "." in a specific column when comma"," is a delimiter

Hi, I'm dealing with an issue and losing a lot of hours figuring out how i would solve this. I have an input file which looks like this: ('BLABLA +200-GRS','Serviço ','TarifaçãoServiço','wap.bla.us.0000000121',2985,0,55,' de conversão em escada','Dia','Domingos') ('BLABLA +200-GRR','Serviço... (6 Replies)
Discussion started by: poliver
6 Replies

5. Programming

"Symbol referencing errors" On Socket programming

Dear all, I had the "Symbol referencing errors" while compiling a C socket code. It said "Undefined Symbol: socketpair", but I already copy the two head files (#include "types.h", #include "socket.h") into my current directory. Could anyone help me with it? Thanks. By the way, I'm using... (4 Replies)
Discussion started by: beyondaymk
4 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

8. Shell Programming and Scripting

How to cut a line with the delimeter "pipe symbol"

Hi I am unable to cut a line with the delimeter as a pipe symbol "|" COuld you please help me ? below is the code i am using right now ************ for i in `cat xyz` do source=`echo $i | cut -f 1 -d |` echo $source done ********* Error i am recieving while exceuting the above... (7 Replies)
Discussion started by: Tasha_T
7 Replies

9. HP-UX

ld: (Warning) Symbol "XXX" is not exported but is imported by a shared

Hi, I am trying to build the package for my build tree built with HP UX ecom compiler. I added the flags +check=all to enable run time checks. I compile the tree successfully, but while making the packages I am getting following error: === vxms tests = Generating pgncpio ld: (Warning)... (5 Replies)
Discussion started by: prits31
5 Replies

10. Programming

shared object "undefined symbol: fstat" error

Didn't have this problem in AIX, but ported to Linux with GCC compiler and am now getting a runtime error: tssutil: symbol lookup error: /work/agility/devel/bin/libagam.so: undefined symbol: fstat I'm sure most of you know that fstat is an intrinsic function just like printf, memcpy, etc. When I... (5 Replies)
Discussion started by: marcus121
5 Replies
Login or Register to Ask a Question