Ritesh Mishra
Ritesh Mishra

Reputation: 1

How to count spaces for 2nd column in any file

I want to count the lines which do not have any words separated by spaces.

Example in domainlist.txt:

Hi My name is Ritesh Mishra
my.name
my

There the script should should give the output: 2

I have witten below code

#!/bin/bash
param=" "
cat domainlist.txt | while read line
do
d=`echo $line | awk '{print $2}' `
if [[ $d == $param  ]];
then
let count++
fi
done
echo $count

It should count the lines which do not have any space-separated words. But its not showing any inputs.

Upvotes: 0

Views: 86

Answers (2)

Paul Hodges
Paul Hodges

Reputation: 15418

Seems like we are overcomplicating this. Why not just grep?

$: cat file
Hi My name is Ritesh Mishra
my.name
my
$: grep -vc ' ' file
2

Upvotes: 0

James Brown
James Brown

Reputation: 37464

Using awk to count the lines which doesn't have any space separated word:

$ awk 'NF==1{c++}END{print c}' file
2

Upvotes: 1

Related Questions