LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-18-2011, 09:30 PM   #1
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Rep: Reputation: 0
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!
 
Old 01-18-2011, 10:14 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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
 
1 members found this post helpful.
Old 01-18-2011, 10:51 PM   #3
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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.
 
Old 01-18-2011, 11:12 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,099

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
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.
 
Old 01-18-2011, 11:23 PM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
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
 
Old 01-18-2011, 11:38 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 01-19-2011, 12:01 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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
 
Old 01-19-2011, 12:22 PM   #8
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by syg00 View Post
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.
 
Old 01-19-2011, 11:03 PM   #9
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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
 
Old 11-27-2014, 11:49 AM   #10
rafilay
LQ Newbie
 
Registered: Nov 2014
Posts: 1

Rep: Reputation: Disabled
Thumbs up 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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to grep awk or sed the first row and column Bone11409 Linux - Newbie 2 03-21-2010 08:18 PM
sed/awk group on first column Eddie Adams Linux - General 4 04-09-2009 10:23 AM
Moving columns with sed or awk? btm Linux - Newbie 4 09-27-2007 02:03 PM
Insert character by using sed/awk manish_meet_in Linux - General 3 04-05-2007 12:19 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration