Insert column with awk or sed between two columns
Hi,
I have a two column file and I need to create a new column in between the first and second but the new column adds a value to the first. E.g. I have this 2999904 51.1 2999905 53.3 2999906 55.7 But I need this 2999904 2999929 51.1 2999905 2999930 53.3 2999906 2999931 55.7 I thought I had figured out how to do it with the following but it just hangs: awk -F '{print $0,$0+25,$1}' file_in > file_out Also tried the following to no avail: awk -F,-v OFS,'{print $0,$0+25,$1}' file_in > file_out I can add the new column with the added value to the last column easy ( awk '{print $0,$0+25}' file_in > file_out). Any suggestions? Thanks! |
Might need to go and re-read your favourite awk website of man page.
$0 - The entire line up until the record separator $1 - $NF - fields based on FS value |
Quote:
Quote:
|
What grail is attempting to clarify, is that the first field is $1, the second is $2. $0 is the entire record.
Were you to simply update your variables in the first attempt above, you might be pleasantly surprised ... easier than excel by far. |
Code:
# ruby -ane '$F[0]=$F[0]+" "+($F[0].to_i + 25).to_s;print "#{$F.join(" ")}\n"' file |
The info manual for gawk is a book, Gawk: Effective Gawk Programming.
I downloaded the source and used it to produce a print worthy pdf document from the .texi source. The O'Reilly Sed & Awk book is excellent. |
Quote:
Quote:
Had you bothered to search for 'fields' you would have found the following: Quote:
Quote:
Quote:
|
Quote:
|
Quote:
I see gnu.org has a guide for sed, I'll read that one too :) |
Insert column with awk or sed between two columns
awk '{ print $1,$1+=25,$2 }' abc
2999904 2999929 51.1 2999905 2999930 53.3 2999906 2999931 55.7 |
All times are GMT -5. The time now is 07:19 AM. |