LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-02-2013, 12:10 PM   #1
Pinkk
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
Bash script


HI



i have this code where i want the mean of words per line of the second half(lines) that contain a word choosen by the user

count=`wc -l < $1`
if [ $count%2 == 0 ]; then
let cnt=$count/2
else
let cnt=$count/2+1
fi

lala=`tail -$cnt $1`
echo "$lala"|grep "$2" | wc -w
echo "$lala" | grep "$2" | wc -l


now my problem is that i need to divide those two outputs instead of "ouputting" them. but i can't assign them to avariable T_T
lala is the second half of lines. $2 is the word given by the user.

Sorry for my bad english!

Thank you ;D
 
Old 04-02-2013, 12:44 PM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
I have no idea what you want but you can assign the output of a command to a variable with var=$(command) example T_T=$(echo "$lala" | grep "$2" | wc -l)
 
1 members found this post helpful.
Old 04-02-2013, 12:49 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please also be aware that bash only performs integer operations and truncates (not rounds) anything after the decimal point. This will affect your results.
 
1 members found this post helpful.
Old 04-02-2013, 01:24 PM   #4
Pinkk
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by whizje View Post
I have no idea what you want but you can assign the output of a command to a variable with var=$(command) example T_T=$(echo "$lala" | grep "$2" | wc -l)
tried it but doesnt work.

here is what i want


#!/bin/bash

count=`wc -l < $1`
if [ $count%2 == 0 ]; then
let cnt=$count/2
else
let cnt=$count/2+1
fi

lala=`tail -$cnt $1`
word=$("$lala"|grep "$2" | wc -w)
lines=$("$lala" | grep "$2" | wc -l)

let tot=$word+$lines
echo $tot



but it gives the No such file or directory on these:
word=$("$lala"|grep "$2" | wc -w)
lines=$("$lala" | grep "$2" | wc -l)

T_T
 
Old 04-02-2013, 02:52 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Pinkk View Post
but it gives the No such file or directory on these:
word=$("$lala"|grep "$2" | wc -w)
lines=$("$lala" | grep "$2" | wc -l)
You didn't specify any input file.
This shows lines containing "raven" in the famous Edgar Allen Poe poem...
Note the < /home/daniel/Desktop/LQfiles/raven.txt identifies the input file.
Code:
daniel@daniel-desktop:~$ grep raven < /home/daniel/Desktop/LQfiles/raven.txt
In there stepped a stately raven of the saintly days of yore.
'Though thy crest be shorn and shaven, thou,' I said, 'art sure no craven.
Ghastly grim and ancient raven wandering from the nightly shore -
Quoth the raven, 'Nevermore.'
But the raven, sitting lonely on the placid bust, spoke only,
But the raven still beguiling all my sad soul into smiling,
Quoth the raven, 'Nevermore.'
Quoth the raven, 'Nevermore.'
Quoth the raven, 'Nevermore.'
Quoth the raven, 'Nevermore.'
And the raven, never flitting, still is sitting, still is sitting
Now let's go one step farther.
This counts those lines.
Code:
daniel@daniel-desktop:~$ grep raven < /home/daniel/Desktop/LQfiles/raven.txt |wc -l
11
Continuing this example ...
This stuffs the numeric value 11 into the variable TT.
Code:
daniel@daniel-desktop:~$ TT=$(grep raven < /home/daniel/Desktop/LQfiles/raven.txt |wc -l)
... and this displays the contents of variable TT.
Code:
daniel@daniel-desktop:~$ echo $TT
11
Daniel B. Martin
 
Old 04-02-2013, 03:44 PM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Quote:
word=$("$lala"|grep "$2" | wc -w)
lines=$("$lala" | grep "$2" | wc -l)
You forgot the echo
Code:
word=$(echo "$lala"|grep "$2" | wc -w)
lines=$(echo "$lala" | grep "$2" | wc -l)
 
Old 04-03-2013, 01:57 PM   #7
Pinkk
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
it worked!!!

Thanks very much
 
  


Reply



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
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:44 AM.

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
Open Source Consulting | Domain Registration