Hi there,
I'd like to find a way to display a string and count the words in it.
supernova:~# echo 'hello world' | tee - | wc
Unfortunately, this doesn't work.
Any idea?
Thanks in advance.
Santiago
Hi there,
I'd like to find a way to display a string and count the words in it.
supernova:~# echo 'hello world' | tee - | wc
Unfortunately, this doesn't work.
Any idea?
Thanks in advance.
Santiago
'man wc'
$ echo 'hello world' | wc -w
2
$ echo 'hello world' | awk '{print NF, $0}'
2
Thanks vgersh99 for your participation but this doesn't do anything more than my previous tries: It only gives the number of words.
For example:
supernova:~# magiccommand 'hello world'
hello world
2
supernova:~# magiccommand 'how are you'
how are you
3
supernova:~# magiccommand 'must be something pretty tricky'
must be something pretty tricky
5
Any other idea?
Santiago
$ echo 'hello world' | awk '{print $0 ORS NF}'
hello world
2
Nice job, thanks a lot.
A bit trikier now.
From the result of a mysql query I actually need to:
1) save the number of rows into one variable ($count)
2) save first field of last line into another variable ($id)
Can you assign variables inside a awk script?
The query:
santiago:~$ mysql -e 'SELECT id, name FROM terminal'
+----+------------------+
| id | name |
+----+------------------+
| 3 | John Smith |
| 18 | Alan Parker |
| 41 | Bob Johnson |
+----+------------------+
In that case, $count = 4 and $id = 41.
I could do that by running the query two times but that's what I'd like to avoid.
santiago:~$ count=$(mysql -e 'SELECT id, name FROM terminal' | wc -l)
santiago:~$ id=$(mysql -e 'SELECT id, name FROM terminal' | tail -1 | cut -f1)
Any advice?
Santiago
Assuming that the output of mysql command would be :
id name
3 John Smith
18 Alan Parker
41 Bob Johnson
you can try this.
result=$(mysql -e 'SELECT id, name FROM terminal' | awk '{count=NR;id=$1} END{print count" "id}')
count=$(echo $result | awk '{print $1}')
id=$(echo $result | awk '{print $2}')
You might have to play around with count=NR;id=$1 if there are formatters/separators in the output.
You may try something like this:
eval "$(mysq -Ne'select concat("count=",count(1),";id=",max(id)) from terminal')"
P.S. The count is 3, why you want to count the column headers?
Great job rrk001!
I think I'm not even gonna use two different variables.
I can write my script like this:
r=$(mysql -e "SELECT id, name FROM terminal WHERE name LIKE '%$v%'" | awk '{count=NR-1;id=$1} END{print count"-"id}')
echo "There are ${r%%-*} results. The last one is ${r##*-}."
I modified count=NR-1 so I don't count the header as a result.
Thanks radoulov as well. I just noticed your post. Working on it.
Hi radoulov,
I like your solution. Eval looks great but there's something wrong with the quoting. I can't make it work.
supernova:~$ eval "(mysq -Ne'select concat("count=",count(1),";id=",max(id)) from terminal')"
-bash: unexpected EOF while looking for matching `''
-bash: syntax error: unexpected end of file
I got it!
You need to put a dollar sign around the evaluated expression.
eval $(mysql ndtv -Ne 'select concat("count=",count(1),";id=",max(id)) from terminal')
Yes, of course, sorry for the typo. I'll correct my post.
There's no need for an external command:
string="hello world"
set -f
set -- $string
printf '%d words in "%s"\n' "$#" "$string"
Hi Johnson,
Can you please modify your word count script "to count each word a file and display the corresponding count for that word".
For example, I have the following data in a text file.
A
B
B
C
D
E
E
E
......etc.
My script output should be
A 1
B 2
C 1
D 1
E 3
Generally my application requires counting the distinct words in a particular column and displaying along with their count.
Thanks in advance...
for that you can use awk
awk '{A[$0]+=1}END{for(i in A){print I" "A}}' file
awk '{A[$0]++}END{for(i in A) print i OFS A}' file