How to count the number of lines in a file using Bash?

Let’s learn how to count the number of lines in a file using Bash. The most accurate or helpful solution is served by Stack Overflow.

There are ten answers to this question.

Best solution

Count number of lines under each header in a text file using bash shell script

I can do this easily in python or some other high level language. What I am interested in is doing this with bash. Here is the file format: head-xyz item1 item2 item3 head-abc item8 item5 item6 item9 What I would like to do is print the following output: head-xyz: 3 head-abc: 4 header will have a specific pattern similar to the example i gave above. items also have specific patterns like in the example above. I am only interested in the count of items under each header.

Answer:

You can use awk: awk '/head/{h=$0}{c[h]++}END{for(i in c)print i, c[i]-1}' input.file

Read more

powerrox at Stack Overflow Mark as irrelevant Undo

Other solutions

Answer:

Use the following: awk 'END { print NR }' Awk will count the lines and print it out.

Read more

wiki.answers.com Mark as irrelevant Undo

Sorting a text file visual basic?

Imports System.IO Public Class Form1 Public Structure ExamGrades Dim name As String Dim grade As Integer End Structure Dim Values() As String Dim Count As String = 0 Dim grades As New List(Of ExamGrades) Private Sub Form1_Load(ByVal sender As System...

Answer:

If you go an easy way, you can create a one-dimensional array (each element is string like this: &ldquo...

Read more

Petro at Yahoo! Answers Mark as irrelevant Undo

Perl: Calculating distance between atoms in pdb file?

Hi, I am creating a program that calculates the distance between the x, y, z coordinates of atoms listed in a pdb file. So far i have this: #!/usr/bin/perl -w $num = 0; $count = 0; while (<>) { # Find x, y, z coordinates and store in separate arrays...

Answer:

First of all, add the following to the top of your program, it's common courtesy BEFORE coming here...

Read more

monica at Yahoo! Answers Mark as irrelevant Undo

Answer:

As i know there is no kernel support for changing the start position of a file in place, you still have...

Read more

Jimmy Wennlund at Quora Mark as irrelevant Undo

Answer:

wc -l wc stands for "word count". The -l switch (that's a lower case "L") is to...

Read more

Freedom at Yahoo! Answers Mark as irrelevant Undo

Answer:

Try this:::**dir /a-d | find /c ":" > NUMfiles.###set /p count=<NUMfiles.

Read more

Anonymous at ChaCha Mark as irrelevant Undo

Answer:

Will wc -l not do?

Read more

Eunji Choi at Quora Mark as irrelevant Undo

Extracting data

QUESTION: Thanks for your help I finally managed to make all the changes and now it's working fine. Could you please help me so that when the user clicks the button it opens the file automatically using a file location rather than "FD" File...

Answer:

Srinivasa, Thank you for the more thorough explanation. I will study tomorrow and try to offer a solution...

Read more

Miningco.com Mark as irrelevant Undo

C++ program and i am stuck!

QUESTION: Here is my program. I cannot get it to work ..following that are the instructions..any advice would be appreciated using namespace std; // function prototypes void calculateAverage (ifstream&, ofstream& , double&); void calculateGrade...

Answer:

Hello Jason Well, I think I got the output you want. I had to change many of the setw calls, and left...

Read more

Miningco.com Mark as irrelevant Undo

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.