How to display and count

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to display and count
# 1  
Old 02-26-2009
Question How to display and count

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.
# 2  
Old 02-26-2009
'man wc'
$ echo 'hello world' | wc -w
$ echo 'hello world' | awk '{print NF, $0}'

# 3  
Old 02-26-2009
Thanks vgersh99 for your participation but this doesn't do anything more than my previous tries: It only gives the number of words.
Originally Posted by chebarbudo
I'd like to find a way to display a string and count the words in it.
For example:
supernova:~# magiccommand 'hello world'
hello world
supernova:~# magiccommand 'how are you'
how are you
supernova:~# magiccommand 'must be something pretty tricky'
must be something pretty tricky

Any other idea?
# 4  
Old 02-26-2009
$ echo 'hello world' | awk '{print $0 ORS NF}'
hello world

Last edited by vgersh99; 02-26-2009 at 08:26 PM.. Reason: sep lines
# 5  
Old 02-27-2009
Nice job, thanks a lot.
# 6  
Old 02-27-2009
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?

Last edited by chebarbudo; 02-27-2009 at 09:50 AM..
# 7  
Old 02-27-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies

2. Shell Programming and Scripting

Count the number of string occurrences to display 0 entries in output

Hello Friends, Can somebody assist an issue I am having? I have a separate file with a list of account ids XXX200B02Y01 XXX200B03Y01 XXX200B05Y01 XXX200B07Y01 XXX200B08Y01 I call the file, and run an egrep against a directory and logfiles AccountID=$(cat... (2 Replies)
Discussion started by: liketheshell
2 Replies

3. Shell Programming and Scripting

Extract count of string in all files and display on date wise

Hi All, hope you all are doing well! I kindly ask you for shell scripting help, here is the description: I have huge number of files shown below on date wise, which contains different strings(numbers you can say) including 505001 and 602001. ... (14 Replies)
Discussion started by: VasuKukkapalli
14 Replies

4. Programming

SQL*PLUS How to display a count of 0

Hi, I have been frantically googling and checking some sqlplus forums, but can't find the correct syntax. Basically within sqlplus I want to do a count on a table and if the count is 0 it displays 0 instead of "no rows found". For eample: select count(*) from tableA where... (3 Replies)
Discussion started by: chris01010
3 Replies

5. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

6. UNIX for Dummies Questions & Answers

Single UNIX command to display users and to count them

Hello everyone, I am new to Unix and I am stuck with a problem. I need only a single command to display the output of who and then add the total number of users and display at the bottom of that output. Example-: (Expected output) sreyan@debian:~$ <command> sreyan tty7 ... (7 Replies)
Discussion started by: sreyan32
7 Replies

7. Shell Programming and Scripting

Read File and Display The Count of a particular field

Hi Mates, I require help in the following: I have the following file snmp.txt Wed Mar 2 16:02:39 SGT 2011 Class : mmTrapBladeS origin : hostname : msg : IBM Blade Alert: Calendar Index : Fri Mar 4 07:10:54 SGT 2011 Class : mmTrapBladeS... (2 Replies)
Discussion started by: dbashyam
2 Replies

8. Shell Programming and Scripting

Display the count of files

I am new to shell programming. Can anyone help me out with anyone of these? Display a count of the number of regular files, the number of symbolic links, the number of sub-directories, the number of block-special files, and the number of character-special files in the directory. I don't... (4 Replies)
Discussion started by: wayne1411
4 Replies
Login or Register to Ask a Question

Featured Tech Videos