|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
try something like this
Code:
cat testdata.dat| grep -v "#" | awk '{print $1 " " $2*2 " " $3 " " $4}' > testdata2.dat
|
|
#3
|
|||
|
|||
|
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
|
|
#4
|
|||
|
|||
|
Thanks Jim....The data is now in what I need it to be.
|
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
|
|
#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... |
|
#9
|
|||
|
|||
|
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... |
|
#10
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > extract, modify, and write column of data |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|