LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   log column in file with awk (https://www.linuxquestions.org/questions/programming-9/log-column-in-file-with-awk-4175472969/)

gav251 08-11-2013 05:18 AM

log column in file with awk
 
Hi
I have a file with 2 columns. What awk command logs the 2nd, forming a new column?
Thanx.
Gav.

druuna 08-11-2013 05:23 AM

Quote:

Originally Posted by gav251 (Post 5007149)
I have a file with 2 columns. What awk command logs the 2nd, forming a new column?

Does this new column need to be added to the original file (creating a third column) or does the output need to got to screen/new file?

Also: please provide a relevant input and output example (use [code] .. [/code] tags to keep the layout intact).

If all you want/need is the second column (assuming space is delimiter):
Code:

awk '{ print $2 }' infile

danielbmartin 08-11-2013 12:09 PM

Quote:

Originally Posted by gav251 (Post 5007149)
I have a file with 2 columns. What awk command logs the 2nd, forming a new column?

Please explain what you mean by "logs". It is helpful if you provide a sample input file (10-12 lines is enough) and the corresponding desired output file.

Daniel B. Martin

SAbhi 08-11-2013 10:54 PM

Quote:

Hi
I have a file with 2 columns. What awk command logs the 2nd, forming a new column?
Thanx.
Gav.
I must say i didnt got your question correctly, can you please give some example or sample output you are expecting from the "awk".

gav251 08-12-2013 05:40 AM

Hi all,thanks for input
the original is like
#Sim RMSD
0.00 0.00
1.00 1.61
2.00 1.78
3.00 1.81
4.00 1.89
5.00 1.79
6.00 1.91
7.00 2.20
8.00 2.30
9.00 2.52
10.00 2.50
11.00 2.47
12.00 2.63
13.00 2.77
14.00 2.70
15.00 2.83
16.00 2.72
a 3rd column or new 2nd or new file is great since it is gnuplot input.
gav.

druuna 08-12-2013 05:50 AM

My answer given in post #2 should do the trick. The output must be redirected to a (new) file:
Code:

awk '{ print $2 }' infile > outfile
If this is answered to your liking then please put up the [SOLVED] tag (upper right corner / Thread Tools menu).

Firerat 08-12-2013 05:56 AM

that still makes no sense

Code:

awk '{print $0" "$2}' Input
gives you

Code:

#Sim RMSD RMSD
0.00 0.00 0.00
1.00 1.61 1.61
2.00 1.78 1.78
..
13.00 2.77 2.77
14.00 2.70 2.70
15.00 2.83 2.83
16.00 2.72 2.72

Which fits your request.. but why bother?

druuna 08-12-2013 05:59 AM

Quote:

Originally Posted by Firerat (Post 5007722)
that still makes no sense

ROFL

Well, my solution seems to be option three ;)

Quote:

Originally Posted by gav251
a 3rd column or new 2nd or new file is great since it is gnuplot input.


Firerat 08-12-2013 06:14 AM

Quote:

Originally Posted by druuna (Post 5007726)
ROFL

Well, my solution seems to be option three ;)

I like option two
do nothing
Seriously , @gav251
are you certain you don't need to do something to column 2?

something like,
Code:

awk '{n=$2;print $0" "$2+n}' Input
giving you
Code:

#Sim RMSD 0
0.00 0.00 0
1.00 1.61 3.22
2.00 1.78 3.56
3.00 1.81 3.62
4.00 1.89 3.78
..
14.00 2.70 5.4
15.00 2.83 5.66
16.00 2.72 5.44

or,

Code:

awk '{n=$2+n;print $0" "$2+n}' Input
Code:

#Sim RMSD 0
0.00 0.00 0
1.00 1.61 3.22
2.00 1.78 5.17
3.00 1.81 7.01
4.00 1.89 8.98
5.00 1.79 10.67
..
13.00 2.77 30.95
14.00 2.70 33.58
15.00 2.83 36.54
16.00 2.72 39.15

if you just want column 2, then yes I understand ( and as druuna has already mentioned he gave that in #2 )

But the "new 2nd" has me very confused, if the new column 2 is the old column 2 it is still the old column 2
so why do anything?

gav251 08-14-2013 04:45 AM

Hi all
awk '{ print log $2 }' < rmsd.tab > log.fil
why does this give error?
thnx.

druuna 08-14-2013 04:53 AM

Quote:

Originally Posted by gav251 (Post 5009030)
Hi all
awk '{ print log $2 }' < rmsd.tab > log.fil
why does this give error?
thnx.

You don't mention the error, is it this one:
Code:

awk: { print log $2 }
awk:            ^ syntax error

Anyway, what is the purpose of log in { print log $2 }?

This should work:
Code:

awk '{ print $2 }' < rmsd.tab > log.fil
Infile used is rmsd.tab, outfile used is log.fil and it prints column 2 from the infile.

PS: You really need to tell us in more detail what it is you are trying to do:
- Give us a relevant example of the input used,
- Give us the output you expect,
- Tell us what didn't work (including the possible error message).

We have to assume too many thing as it is and this might lead to unwanted answers.

firstfire 08-14-2013 06:43 AM

Hi.

I'd like to add a bit to previous answers: If you're going to plot resulting file in gnuplot, then you should skip comments in input file, like this for example:
Code:

awk '!/^#/{print $2}' rmsd.tab > log.fil
otherwise you will get very strange plots (if any at all).

And, as Firerat mentioned, you can plot second column of this file directly from gnuplot, e.g.:
Code:

plot 'rmsd.tab' u 2
(This will handle comments correctly)

gav251 08-15-2013 04:15 AM

Hi, sorry for ambiguity
this prints col 2, I need log value eg

1.0 1.61 0.2068

this is x y logy

Gav.

Firerat 08-15-2013 04:25 AM

:) ahhh makes sense now

http://www.gnu.org/software/gawk/man...Functions.html

should help

konsolebox 08-15-2013 11:45 PM

Quote:

Originally Posted by gav251 (Post 5009030)
Hi all
awk '{ print log $2 }' < rmsd.tab > log.fil
why does this give error?
thnx.

The proper way to call that is
Code:

awk '{ print log($2) }' < rmsd.tab > log.fil
Or
Code:

awk '{ print $0 " " log($2) }' < rmsd.tab > another


All times are GMT -5. The time now is 02:16 AM.