LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2009, 08:23 AM   #1
tatoosh
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Rep: Reputation: 1
write line count to a variable?


I want to count my postmap duplicates, but didnt get it to work.

here is my shell code. Can u give me an advice ?
Code:
postmap test 2> duplicates.tmp
var_duplicates='wc duplicates.tmp | awk '{print $1}''
echo $var_duplicates " duplicates found"
 
Old 07-30-2009, 08:32 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
var_duplicates=$(wc -l duplicates.tmp)
 
Old 07-30-2009, 08:38 AM   #3
tatoosh
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Original Poster
Rep: Reputation: 1
great that works. thanks a lot.

now i want to substract to variables.
the last line didnt work. can u help me there, too ?

Code:
var_duplicates=$(wc -l duplicates.tmp | awk '{print $1}')
var_original=(wc -l original.tmp | awk '{print $1}')
var_converted=(wc -l convert3.tmp | awk '{print $1}')

echo "$var_duplicates duplucates found."
echo "removed lines: $var_original-$var_converted"
 
Old 07-30-2009, 08:40 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by tatoosh View Post
Code:
var_duplicates='wc duplicates.tmp | awk '{print $1}''
The syntax for command substitution is either `command` (with back quotes) or $(command) as in the example by Agrouf
 
Old 07-30-2009, 08:41 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Agrouf View Post
var_duplicates=$(wc -l duplicates.tmp)
This returns also the file name. To retrieve only the line count, better to do cat file | wc -l.
 
Old 07-30-2009, 08:43 AM   #6
tatoosh
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Original Poster
Rep: Reputation: 1
thx colucix.
that's why i used: | awk '{print $1}

it's the same, or?
 
Old 07-30-2009, 08:44 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by tatoosh View Post
now i want to substract to variables.
the last line didnt work. can u help me there, too ?
Syntax for arithmetic expansion in BASH:
Code:
echo "removed lines: $(($var_original - $var_converted))"
If using the Bourne Shell (or for compatibility with /bin/sh):
Code:
echo "removed lines: `expr $var_original - $var_converted`"
 
Old 07-30-2009, 08:46 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by tatoosh View Post
thx colucix.
that's why i used: | awk '{print $1}

it's the same, or?
Of course. Using cat or awk implies you use an external command in both cases. The result is exactly the same.
 
Old 07-30-2009, 09:01 AM   #9
tatoosh
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Original Poster
Rep: Reputation: 1
thx a lot colucix.

my script runs fine now - with your help
 
Old 07-30-2009, 04:44 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Or, avoiding a second command:
Code:
var_duplicates=$(wc -l < duplicates.tmp)[

Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 04:44 PM
write a program to count files within a directory bio71 Programming 1 10-03-2008 06:36 PM
tape write count xian_hu Linux - Hardware 2 04-01-2005 11:00 PM
word count in a line pantera Programming 4 08-25-2004 02:14 PM
Count number of line in a file. philipina Programming 7 03-18-2004 06:04 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration