LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to add a column in vi editor ? (http://www.linuxquestions.org/questions/programming-9/how-to-add-a-column-in-vi-editor-680398/)

dina3e 11-01-2008 12:36 AM

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.

abolishtheun 11-01-2008 01:29 AM

highlight a region and run
Code:

:'<,'>!perl -lpwae '$s+=$F[2]; END{print $s;}'
where the value inside the square bracket is the column.

nishamathew1980 11-02-2008 05:13 AM

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

abolishtheun 11-02-2008 03:53 PM

Quote:

Originally Posted by nishamathew1980 (Post 3328933)
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?

sharky 11-07-2008 05:22 PM

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.

pwc101 11-07-2008 06:16 PM

Quote:

Originally Posted by sharky (Post 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}'
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 :)

dina3e 11-08-2008 09:40 PM

[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.


All times are GMT -5. The time now is 09:27 PM.