LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Insert column with awk or sed between two columns (https://www.linuxquestions.org/questions/linux-newbie-8/insert-column-with-awk-or-sed-between-two-columns-857158/)

captainentropy 01-18-2011 09:30 PM

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!

grail 01-18-2011 10:14 PM

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

captainentropy 01-18-2011 10:51 PM

Quote:

Originally Posted by grail (Post 4229575)
Might need to go and re-read your favourite awk website of man page.

And I might need to get a degree in Computer Science...I've read the awk websites and they weren't much help - they're basically MAN pages, not detailed tutorials - which is why I posted this in the NEWBIE forum.

Quote:

$0 - The entire line up until the record separator

$1 - $NF - fields based on FS value
yeah, that was no help. Sorry. Thankfully it was only ~750,000 lines. I got what I needed in Excel in like 2 minutes. I'd still like to learn more about sed and awk by example because those MAN pages assume too much prior knowledge.

syg00 01-18-2011 11:12 PM

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.

kurumi 01-18-2011 11:23 PM

Code:

# ruby -ane '$F[0]=$F[0]+" "+($F[0].to_i + 25).to_s;print "#{$F.join(" ")}\n"' file
2999904 2999929 51.1
2999905 2999930 53.3
2999906 2999931 55.7


jschiwal 01-18-2011 11:38 PM

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.

grail 01-19-2011 12:01 AM

Quote:

I've read the awk websites and they weren't much help
Not sure which ones you looked at but this one seems to work for me.

Quote:

- they're basically MAN pages
I see no issue here seeing man is the bread and butter for linux users.
Had you bothered to search for 'fields' you would have found the following:
Quote:

Each field in the input record may be referenced by its position, $1, $2, and so on. $0 is the whole record.
Quote:

not detailed tutorials
Again see answer 1 above

Quote:

which is why I posted this in the NEWBIE forum.
Which is why I was trying to educate and not spoon feed you an answer

captainentropy 01-19-2011 12:22 PM

Quote:

Originally Posted by syg00 (Post 4229620)
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.

Oh man, that was the difference. THAT was way faster than the alternative I used. Thanks syg00! You see, this is what I'm talking about with the manuals and guides, none of them made that clear. "0" is typically considered the first of whatever or so I was told by a programmer long ago. Sigh.

captainentropy 01-19-2011 11:03 PM

Quote:

Originally Posted by grail (Post 4229661)
Which is why I was trying to educate and not spoon feed you an answer

thanks for your assistance but with all due respect the best way to learn is by example/mimicry. You're VERY advanced. I'm not. I really had no idea what you meant. The man page you sent is more clear than the two I was reading. It was kind of like trying to learn German by reading der Speigel and only knowing a few German phrases.

I see gnu.org has a guide for sed, I'll read that one too :)

rafilay 11-27-2014 11:49 AM

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.