awk file subtracted by a fix value - conditioned


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk file subtracted by a fix value - conditioned
# 1  
Old 03-31-2015
Error awk file subtracted by a fix value - conditioned

Hi all...
i have been trying to make this work but I have been failing for 6 hours ..
I know it should be something simple that I am missing to it would be great if you can help me ...

I want to subtract a fixed value (lets set 1) from any value >=1 from the whole file
my file looks like
Quote:
0,1,0,0
0,0,1,3,0,0
0,0,0,1,2,0
0,1,0,0
0,0,1,3,0,0
0,0,0,0,0
and all I need is
Quote:
0,0,0,0
0,0,0,2,0,0
0,0,0,0,1,0
0,0,0,0
0,0,0,2,0,0
0,0,0,0,0
I have tried millions of things
Code:
awk -F, '{print $'${0:-1}'}'

Code:
awk 'p{print ($0-p)}{p=1}'

Code:
awk -v -F, s=1'{print $0-s}'

can someone help please ?
Thanks a lot
# 2  
Old 03-31-2015
Code:
awk -F, -v s=1 '{for (i=1;i<=NF;i++) if($i>=s) $i-=s}1' OFS=, myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-31-2015
Thanks a lot - it worked perfectly
how interesting... I tried -v and the for loop but not together

may I ask to explain it a bit so I can learn... specially
Code:
i$i-=s)1

????
# 4  
Old 03-31-2015
Code:
# set input FieldSeparator (FS) to ,
# set the value to be be subtracted to 1 and assigned it to the internal awk variable s
awk -F, -v s=1 '
  # for each record/line, execute block within {}
  {
    # iterate through all the fields/columns
    for (i=1;i<=NF;i++) 
      if($i>=s) $i-=s         # if current field ($i) is great-or-equal to variable s, 
                                     # subtract s from current field and assign it back to $i
                                     # shorthand for: $i = $i - s
   }
   # print the current record/line - shorthand for {print}
   1' OFS=, myFile

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 03-31-2015
Thanks you so much ... that is very useful
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk if condition match and fix print decimal place

Hi All, I have problem in the middle of implementing to users, whereby the complaint is all about the decimal place which is too long. I need two decimal places only, but the outcome from command is always fixed to 6. See the sample : before: Sort Total Site Sort SortName Parts ... (3 Replies)
Discussion started by: horsepower
3 Replies

2. Shell Programming and Scripting

awk getline 8 times and if $3 = 8 when subtracted from 1st line,print

I have kind of a strange one here. I have a file of consecutive /24 ip blocks. If there are 8 consecutive ip blocks which represent a /20 then I need to print the first line. I played around and did not get the results I need, especially when considering that the highest $3 will be is 255 and then... (6 Replies)
Discussion started by: numele
6 Replies

3. UNIX for Dummies Questions & Answers

Please help to fix awk script

Good morning, fellows. I would need to ask for your help in editing my awk script. Here is the original version: BEGIN { printf ("CRYST1 200.000 200.000 200.000 90.00 90.00 90.00 P 1 1\n") maxatoms=1000 natom=0 found_struct = 0 found_bond = 0 } { if( NF == 5 ) { foundff=0 natom++... (9 Replies)
Discussion started by: snysmumrik
9 Replies

4. UNIX for Dummies Questions & Answers

Delete directory conditioned

Hi, I am trying to make a script that finds if inside a folder there are more than 2 subfolders; if so, I want to erase the older subfolder. I thought this would be useful: find /sdcard/backup -mtime +1 -exec rm-r {} /; But it only erases subfolders older than one day, and I would want... (9 Replies)
Discussion started by: Hammerhand
9 Replies

5. Shell Programming and Scripting

Fix timestamp with Sed or Awk

Hi I am dealing with the following string: Date: Thur, 13 March 2011 01:01:10 +0000 I asked for help in another topic that converted a similar string: Date: Thur, 13 March 2011 9:50 AM To a 24 hr standard. The problem is that it comes out as: Date: Thur, 13 March 2011 9:50:00 +0000... (4 Replies)
Discussion started by: duonut
4 Replies

6. Shell Programming and Scripting

How to fix this awk

I have a script which will mask the 9th and 15th column in a record starting with BPR. The record looks like below before my script BPR*C*160860.04*C*ACH*CTX*01*072000326*DA*1548843*3006968523**01*071000013*DA*5529085*100323*VEN The record will be masked after my script parses this... (19 Replies)
Discussion started by: Muthuraj K
19 Replies

7. Shell Programming and Scripting

how to fix the column length in a file using Awk Prog

Hi I use the following code to read the file and to fix the length of the column of the record in the file 'Sample.txt' ls Samp* | awk ' { a=$1 } END{ FS="n" for(i=1;i<=NR;i++) { while( getline < a ) { f1=$0; print("Line::",f1); f2=substr(f1,1,10) print("Field1::",f2);... (10 Replies)
Discussion started by: meva
10 Replies

8. Shell Programming and Scripting

how to fix this awk script?

i have a log file while looks like this ++ user_a blabla blabla nas_b blabla user_d this is a user_a junk line another junk line user_c nas_m blabla ++ basically most of the lines contain a "user" keywords, and the rest of the lines do not have "user" at all. So I have the... (17 Replies)
Discussion started by: fedora
17 Replies

9. Shell Programming and Scripting

AWK record length fix

Hi Friends, Need some help in AWK. Working on AIX 5 Have been trying the following functionality to make the record length fixed: if( length(record) < 300 ) { printf("%-300s\n", record); } In my opinion it will apply some fillers in the end. Its is not making any... (4 Replies)
Discussion started by: kanu_pathak
4 Replies

10. Shell Programming and Scripting

awk / shell - Fix broken lines and data

Gurus, I am struggling with a issue and thought I could use some of your expertise. Need Help with this I have a flat file that has millions of records 24|john|account ~ info |56| 25|kuo|account ~ journal |58| 27|kim|account ~ journal |59| 28|San|account ~ journal |60|... (3 Replies)
Discussion started by: rimss
3 Replies
Login or Register to Ask a Question