nixCraft Linux / UNIX / Shell Scripting Forum

nixCraft

Linux / UNIX Tech Support Forum

Subtracting different numbers from different columns of a file by awk

This is a discussion on Subtracting different numbers from different columns of a file by awk within the Shell scripting forums, part of the Development/Scripting category; Hi, I tried to subtract a specific number which I have already read from another file to whole numbers of ...


Register free or login to your account to remove all advertisements.

Go Back   nixCraft Linux / UNIX / Shell Scripting Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 10th March 2010, 11:26 PM
Junior Member
 
Join Date: Mar 2010
OS: Debian
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
sheikh is on a distinguished road
Default Subtracting different numbers from different columns of a file by awk

Hi,

I tried to subtract a specific number which I have already read from another file to whole numbers of a column each time and then I want to have my whole file by these changes. I used below command but it did not work properly.
I wonder how I could solve the problem??
I want that each time it could read a number from medium_magnitude_test.param file and then subtract the number from the given column and then it saves the results.

for FILTER in MB416 MB461 MB485 MB518 MB571 MB604 MB646 MB696 MB753 MB815 MB856 MB914
do
for i in $(seq 6 17);
do
k=`grep "MAGZP_${FILTER}" ${MainD}/medium_magnitude_test.param | awk '{print $3}'`
echo ${k}
awk 'BEGIN { $i=$i-k print $0 }' ${MD}/seventeen_filters_10.asc > test00.asc
done
done
Reply With Quote
  #2 (permalink)  
Old 21st March 2010, 12:54 PM
Senior Member
 
Join Date: May 2009
OS: Mandriva
Posts: 104
Thanks: 0
Thanked 28 Times in 22 Posts
Rep Power: 5
cfajohnson is a jewel in the roughcfajohnson is a jewel in the roughcfajohnson is a jewel in the roughcfajohnson is a jewel in the rough
Default

Quote:
Originally Posted by sheikh View Post
I tried to subtract a specific number which I have already read from another file to whole numbers of a column each time and then I want to have my whole file by these changes. I used below command but it did not work properly.
I wonder how I could solve the problem??
I want that each time it could read a number from medium_magnitude_test.param file and then subtract the number from the given column and then it saves the results.

for FILTER in MB416 MB461 MB485 MB518 MB571 MB604 MB646 MB696 MB753 MB815 MB856 MB914
do
for i in $(seq 6 17);
do
k=`grep "MAGZP_${FILTER}" ${MainD}/medium_magnitude_test.param | awk '{print $3}'`
echo ${k}
awk 'BEGIN { $i=$i-k print $0 }' ${MD}/seventeen_filters_10.asc > test00.asc
done
done

I have no idea what you are trying to do, but calling three external programs in a nested loop is almost certainly the wrong way to go about it.

No only that, but you are trying to use shell variables inside an awk script.

The variables will not be expanded inside single quotes.
Reply With Quote
  #3 (permalink)  
Old 6th April 2010, 05:30 PM
Junior Member
 
Join Date: Apr 2010
OS: Debian, Fedora
Scripting language: Bash
Posts: 13
Thanks: 0
Thanked 6 Times in 5 Posts
Rep Power: 0
nnsjunior will become famous soon enough
Default Try this

awk does not process data in the BEGIN block, hence, printing wouldn't work there. This is where you handle operation before awk start processing the data in the main section.

Try changing the following:
awk 'BEGIN { $i=$i-k print $0 }' ${MD}/seventeen_filters_10.asc > test00.asc

to:
awk -v d=$k -v c=$i 'BEGIN { deduct=d; column=c; } { $column = $column - deduct; print $0 }' ${MD}/seventeen_filters_10.asc > test00.asc

See if this works.


Thanks,
- NNS
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Grep numbers from file. eawedat Shell scripting 3 16th August 2008 08:23 AM
Create output in columns rakeshrhn Shell scripting 5 7th December 2007 06:27 PM
help me in editing this columns sureshbup Shell scripting 1 12th December 2006 02:29 PM
rearranging columns in a text file sureshbup Shell scripting 2 6th December 2006 09:43 AM
Calculations across different lines & columns of a file Guest Shell scripting 2 16th September 2005 04:18 AM


All times are GMT +5.5. The time now is 09:04 PM.


Powered by vBulletin® Version 3.8.6 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40