awk conditional assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk conditional assignment
# 1  
Old 04-14-2011
awk conditional assignment

Dear all,

I want to use awk to read the three columns in a file called "test" and change them to ( 1.5 1.5 1.5) if any element is found to be greater than three. A part of the file is shown below:
Code:
(0.478318 0.391032 -0.14054)
(0.45562 0.392523 -0.121685)
(0.437204 0.392811 -0.106158)
(0.420991 0.391615 -0.0944351)
(0.407585 0.389046 -0.0856931)
(-9.64808 6.512 -11.9177)
(-21.2522 4.2609 -15.8548)
(-36.7927 5.73519 -25.5674)
(-56.1474 14.0543 -43.4295)


I tried the following but it didn't work. Please help.
Code:
sed 's/[()]//g' test | awk -F " " '{if($1>2) $1 = $1/$1 + 1.5}'

Thanks,
Fairus

---------- Post updated at 03:35 AM ---------- Previous update was at 03:27 AM ----------

Quote:
Originally Posted by mfmohdyasin

and change them to ( 1.5 1.5 1.5) if any element is found to be greater than three.
I should say " (2.5 2.5 2.5) "

With apology,
Fairus

---------- Post updated at 03:38 AM ---------- Previous update was at 03:35 AM ----------

Quote:
Originally Posted by mfmohdyasin
is found to be greater than three.
should be "greater than two"

Early morning mistake,

Sorry again,
Fairus

Last edited by Franklin52; 04-14-2011 at 05:52 AM.. Reason: Please use code tags
# 2  
Old 04-14-2011
Hi mfmohdyasin,

Try with:

Code:
awk -F"[( )]" '{if($2>2 || $3>2 || $4>2) print "(2.5 2.5 2.5)";else print $0}' inputfile
(0.478318 0.391032 -0.14054)
(0.45562 0.392523 -0.121685)
(0.437204 0.392811 -0.106158)
(0.420991 0.391615 -0.0944351)
(0.407585 0.389046 -0.0856931)
(2.5 2.5 2.5)
(2.5 2.5 2.5)
(2.5 2.5 2.5)
(2.5 2.5 2.5)

Regards
# 3  
Old 04-14-2011
Code:
cat input.txt | awk 'BEGIN{ 
  while (getline  > 0){
    F1 = $1
    F2 = $2
    F3 = $3
    gsub (/\(/,"",F1)
    gsub (/\(/,"",F3)
    if (F1 > 2 || F2 > 2 || F3 > 2){
      print "(2.5 2.5 2.5)"
    }else{
      print $0
    }
  }
}'

# 4  
Old 04-14-2011
Thanks e/one

Fairus
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimize multiple awk variable assignment

how can i optimize the following: TOTALRESULT="total1=4 total2=9 total3=89 TMEMORY=1999" TOTAL1=$(echo "${TOTALRESULT}" | egrep "total1=" | awk -F"=" '{print $NF}') TOTAL2=$(echo "${TOTALRESULT}" | egrep "total2=" | awk -F"=" '{print $NF}') TOTAL3=$(echo... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Conditional awk

Hello All, I have a file like this: bash-3.00$ cat 1.txt 201112091147|0|1359331220|1025 201112091147|0|1359331088|1024 201112091144|0|1359331172|1025 201112091147|0|1359331220|1021 201112091149|0|1359331088|1027 201112091144|0|1359331172|1029 and a list of MSISDNs in another file... (9 Replies)
Discussion started by: EAGL€
9 Replies

4. Shell Programming and Scripting

Variable assignment inside awk

Hi, Was hoping someone could help with the following: while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets? Thanks... (14 Replies)
Discussion started by: CiCa
14 Replies

5. Shell Programming and Scripting

awk variable assignment-- inside braces or not?

So, in awk, I've always put my variable assignments inside of the curly braces, just like dad, and grandpa, and the 26 generations before them. But today I came upon an awk statement that had them outside the braces-- blasphemy! Seriously, though, is there any practical difference? I was... (3 Replies)
Discussion started by: treesloth
3 Replies

6. Shell Programming and Scripting

variable assignment using awk

Guys, Could you please help me out. I need two values in two variables using awk from the o/p of grep. example:- grep sdosanjh <filename> sdosanjh myhostname myfilename NOW WHAT I WANT IS :- sdosanjh should be in variable (say NAME) myhostname should be in variable (say... (8 Replies)
Discussion started by: sdosanjh
8 Replies

7. Shell Programming and Scripting

Awk Conditional

Hi Guys, i have this files: xyz20080716.log opqrs20080716.log abcdef20080716.log xyz20080717.log oprs20080717.log abcde20080717.log currentdate: 20080717.log I want to make script to zip the file for past day. Can anyone help for this? i've just learn awk scripting & still confused with... (3 Replies)
Discussion started by: icy_blu_blu
3 Replies

8. Shell Programming and Scripting

AWK - conditional cause

Hello guys, I want to make a conditional cause in the following file using awk: awk '{ if ($2 != 0) print $1, $2, $3}' test.csv > test2.csv FILE EXAMPLE = test.csv string,number,date abc,0,20050101 def,1,20060101 ghi,2,20040101 jkl,12,20090101 mno,123,20020101 ... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

9. Shell Programming and Scripting

awk variable assignment in a file

Hi All, I have a little awk script which uses a variable (x): awk -v x=0 'NF != 6 { ++x } END { print "This batch had " x " errors out of ", NR" records"}' But when I've tried to put the command in a file I can't seem to declare the variable. I've managed to simplify the code so that I... (4 Replies)
Discussion started by: pondlife
4 Replies

10. Shell Programming and Scripting

awk - value assignment.

I am facing a strange situation with awk. If I do variable substitution for substr it gives value starting from 9th char to the end of record. Following is the command. awk -v offset=9 -v length=4 '{ print substr($0,offset,length) }' filename But for the same if I ommit assignment & harcode... (2 Replies)
Discussion started by: videsh77
2 Replies
Login or Register to Ask a Question