UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old July 20th, 2004, 10:28 AM
linm linm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 linm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question extract, modify, and write column of data

Could you please give me some help with this? I'm new to awk. Thanks.

I would like to extract the 2nd column of a data file, multiply a constant to the extracted data, and then replace the original data with the modified data.

For example:
multiplicative constant = 2

Orignal data (testdata.dat)
#test data
#
1 2
2 4
3 6
#
#end of test data

Resulting data
#test data
#
1 4
2 8
3 12
#
#end of test data


I've gotten so far as to extract the 2nd column of data and doing the multiplication. However, I'm unable to reinsert it back. I can only put it back in a row at the end of the file. Here's what I have right now:

set fluence = `cat testdata.dat | grep -v "#" | awk '{print $2 * 1}'`

echo $fluence >> testdata.dat

result:
#test data
#
1 2
2 4
3 6
#
#end of test data
4 8 12

Reply With Quote
  #2  
Old July 20th, 2004, 10:43 AM
jim mcnamara jim mcnamara is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,299 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 41 m 52 sec
Reputation Power: 47
try something like this
Code:
 cat testdata.dat| grep -v "#" | awk '{print $1 " " $2*2 " " $3 " " $4}' > testdata2.dat

Reply With Quote
  #3  
Old July 20th, 2004, 11:07 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,059 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
yes linm, as you say you are new to awk, also to shell.
please discover: grep, sed, awk, more, less, lp, lpr and a lot other
cmds are able to open a file.
Code:
       cat fileA fileB fileC | grep ....  is correct
       cat -v fileA | grep .... is correct
       cat fileA | grep .... is stupid

Reply With Quote
  #4  
Old July 20th, 2004, 12:15 PM
linm linm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 linm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks Jim....The data is now in what I need it to be.

Reply With Quote
  #5  
Old July 20th, 2004, 12:22 PM
jim mcnamara jim mcnamara is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,299 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 41 m 52 sec
Reputation Power: 47
FWIW- IMO - These are one time scripts:

I'm not sure what stupid means in programming scripts. 'stupid' = Unnecessary process creation I suppose.

If these scripts were running day and night in production on a big multi-user system, then anything you can do to optimize a script is probably good. Up to the point where it becomes un-maintainable, I would guess.

If this is important to you, run your script(s) under tusc or strace and count the number of exec calls.

Reply With Quote
  #6  
Old July 22nd, 2004, 09:29 AM
linm linm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 linm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Jim,

An additional question...what is the correct syntax to make the multiplier a variable?
What I have below does not seem to work. Thanks.

set multfactor = 2
cat testdata.dat | grep -v "#" | awk '{print $1 " " $2 * $multfactor" " $3 " " $4}' > testdata2.dat

Reply With Quote
  #7  
Old July 22nd, 2004, 10:59 AM
jim mcnamara jim mcnamara is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,299 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 41 m 52 sec
Reputation Power: 47
There are two ways - it's simpler to read:

http://www.unix.com/showthread.php?s=&threadid=13955

Reply With Quote
  #8  
Old July 22nd, 2004, 12:55 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,059 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
so jim
cat testdata.dat | grep -v "#" | awk '{print $1 " " $2 * $multfactor" " $3 " " $4}' >output
is sure better maintainable as:
awk '$0 !~ /#/{ $2 *= $multfactor; print;}' >output
one should know awk to mantain this, that's the problem...

Reply With Quote
  #9  
Old July 22nd, 2004, 01:08 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,059 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
sorry, i forgot the inputfile, shoud be:
awk '$0 !~ /#/{ $2 *= $multfactor; print;}' input >output
and you still ignore
cat testdata.dat | grep -v "#" | awk ....
and
grep -v "#" testdata.dat | awk ....
are the same
this is what i call, scripting-stupidity
i also work on big multi-user system >x000 server && x0000 of clients
these scripts ARE running day and night in production AND cause a big.
very big stupid power wasting...

Reply With Quote
  #10  
Old August 3rd, 2004, 10:21 PM
linm linm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 linm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
some more stuff

I have the extract, multiply, and write part working. Something new...I would like to copy the commented parts as well in the output.

This is what I have so far.
cat testfile.tmp | grep -v "#" | awk '{print $1 " " $2*2}' > newtestfile.tmp

Some info about the comments:
1)there are always only 4 comment lines before the data though their contents may be different.
2)the last set of comments after the data always starts with "Attenuation History" but the comments after that have no set amount (there can be 1 or 20 comment lines afterwards).

I'm not sure how to go about copying only the first 4 lines of the file and copying all lines for the Attenuation History comments. Any help would be appreciated.


Input "testfile.tmp"
# spect_gen: 74 15.000 1 50.00
# 50 1.0000 72.0000
# 1 2 (unit flags)
# Energy #/mAs-cm^2-kev
8.000 1.0000E+00
9.000 2.0000E+00
10.000 3.0000E+00
# Attenuation History:
# 3
# glass_pyrex 1.4800E-01
# oil 3.0000E-01
# al_1100 2.8000E-01

Output file "newtestfile.tmp":
# spect_gen: 74 15.000 1 50.00
# 50 1.0000 72.0000
# 1 2 (unit flags)
# Energy #/mAs-cm^2-keV
8.000 2.0000E+00
9.000 4.0000E+00
10.000 6.0000E+00
# Attenuation History:
# 3
# glass_pyrex 1.4800E-01
# oil 3.0000E-01
# al_1100 2.8000E-01

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > extract, modify, and write column of data


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway