LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-31-2008, 11:36 PM   #1
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Rep: Reputation: 16
how to add a column in vi editor ?


let my file having more than one columns and many rows .

2 45 6 8
9 4 12 7
31 5 44 89

how can make use of the vi/vim editor so that i can sum a particular column (let's 3rd column) sum i'll get .
how can i use submatch() function in this case.
 
Old 11-01-2008, 12:29 AM   #2
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
highlight a region and run
Code:
:'<,'>!perl -lpwae '$s+=$F[2]; END{print $s;}'
where the value inside the square bracket is the column.
 
Old 11-02-2008, 04:13 AM   #3
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
I suggested you use the GVIM editor. It's a lot more user friendly that VIM. That should solve the problem you have requested for.


Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 04:49 AM.
 
Old 11-02-2008, 02:53 PM   #4
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
Quote:
Originally Posted by nishamathew1980 View Post
I suggested you use the GVIM editor. It's a lot more user friendly that VIM. That should solve the problem you have requested for.
gvim has built in support for column addition?
 
Old 11-07-2008, 04:22 PM   #5
sharky
Member
 
Registered: Oct 2002
Posts: 384

Rep: Reputation: 37
awk is my favorite tool for quick column addition.

for example, to sum the first column

Code:
cat file | awk '{SUM += $1} END {print SUM}'
This came in handy when I had a file with too many lines for use in open office calc.

Another thing I was able to do was filter some lines and sum the remaining on a file that listed user CPU utilization.

example:

Quote:
user1 331.2
user5 1000.8
user1 603.8
user2 1131.1
user4 3320.3
user4 3015.1
user2 1118.5
.
.
.
I could get user1's utilization this way.

Code:
cat file | grep user1 | awk '{SUM += $2} END {print SUM/3600}'
I divide the sum by 3600 because the numbers given were in seconds and I wanted the result time in hours. (awk even did the math for me!)

It was a trivial csh foreach statement to the get the number for all users.

Code:
foreach U ( `cat file | awk '{print $1}' | sort | uniq` )
cat file | grep $U | awk '{SUM += $2} END {print SUM/3600}'
end
I didn't even have to know beforehand which users were listed in the file.
 
Old 11-07-2008, 05:16 PM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by sharky View Post
awk is my favorite tool for quick column addition.

for example, to sum the first column

Code:
cat file | awk '{SUM += $1} END {print SUM}'
This came in handy when I had a file with too many lines for use in open office calc.

Another thing I was able to do was filter some lines and sum the remaining on a file that listed user CPU utilization.

example:



I could get user1's utilization this way.

Code:
cat file | grep user1 | awk '{SUM += $2} END {print SUM/3600}'
I divide the sum by 3600 because the numbers given were in seconds and I wanted the result time in hours. (awk even did the math for me!)

It was a trivial csh foreach statement to the get the number for all users.

Code:
foreach U ( `cat file | awk '{print $1}' | sort | uniq` )
cat file | grep $U | awk '{SUM += $2} END {print SUM/3600}'
end
I didn't even have to know beforehand which users were listed in the file.
There's no need to cat files everywhere; most programs can read files in themselves:
Code:
prompt$ cat file | awk '{SUM += $1} END {print SUM}'
prompt$ awk '{SUM += $1} END {print SUM}' file

prompt$ cat file | grep user1 | awk '{SUM += $2} END {print SUM/3600}'
prompt$ awk '/user1/ {SUM += $2} END {print SUM/3600}' file

prompt$ foreach U ( `cat file | awk '{print $1}' | sort | uniq` )
cat file | grep $U | awk '{SUM += $2} END {print SUM/3600}'
end
prompt$ foreach U ( `awk '{print $1}' file | sort -u` )
awk '/'$U'/ {SUM += $2} END {print SUM/3600}' file
end
It's known as a Useless Use of cat

Last edited by pwc101; 11-07-2008 at 05:17 PM.
 
Old 11-08-2008, 08:40 PM   #7
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
[quote=sharky;3334866]awk is my favorite tool for quick column addition.

for example, to sum the first column

Code:
cat file | awk '{SUM += $1} END {print SUM}'
What you did , i mean you choose the field using the awk and get the sum of the column . But the same thing i want to try in vi/vim/gvim . i found the sum in vim

But this thing also makes some trouble about the submatch(0) function.
 
  


Reply

Tags
columns


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
vi editor - how to delete a character in each row of a column? flipwhy Linux - Newbie 6 01-22-2007 02:12 PM
How do I add an new column in Nautilus? hilliard Linux - General 2 12-29-2006 11:33 AM
Looking for Column Mode editor (like IBM's XEDIT or KEDIT) pcardout Linux - Desktop 9 12-10-2006 01:55 PM
add id in the first column alaios Linux - General 1 11-19-2004 06:49 AM
add new column Eddie9 Linux - General 2 04-09-2002 12:05 PM


All times are GMT -5. The time now is 12:29 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration