small question of echo | grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting small question of echo | grep command
# 1  
Old 04-30-2005
small question of echo | grep command

Hi, i've got the following:

a=`echo $b | grep '^.*/'`

i'm storing in the variable the value of the variable b only if it has a / somewhere.
It works, but i don't want to print the value. How do i give the value of b to the grep command without the echo?

thanks!
# 2  
Old 04-30-2005
It is not printing the value of 'b' with echo and grep in the above command you gave.

But , if it happening on your machine...
you may try the following.

Code:
echo $b >tmp
a=`grep '^.*/' tmp`
rm tmp
echo $a

# 3  
Old 04-30-2005
Quote:
grep '^.*/'
Also why complicate it? The expanded regular expression grep command given, is functionally identical to grep '/'

Last edited by reborg; 05-02-2005 at 12:22 PM..
# 4  
Old 05-01-2005
thanks for the help!
# 5  
Old 05-02-2005
Quote:
Originally Posted by kfad
Hi, i've got the following:

a=`echo $b | grep '^.*/'`

i'm storing in the variable the value of the variable b only if it has a / somewhere.
It works, but i don't want to print the value. How do i give the value of b to the grep command without the echo?

thanks!
To answer the original question....

It will not "print" the value to the terminal (if that's what you're getting at) - the output of echo is captured and stored in the variable.

I always do validation like this with an if/else construct...
Code:
my_string="/foo/bar/"
echo "$my_string" | grep '/' >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
  echo "Yay"
  # use my_string for whatever you want
else
  echo "Nay"
fi

This is a (slightly verbose) piece of code that can be modified for any validation purpose....
Cheers
ZB
# 6  
Old 05-02-2005
try ...

a=`echo $b | grep /` > /dev/null
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question on grep command (extract a string)

Dear all, I have a file a.txt like below: 1_234560_A_G_b37 1 2 1 2 2 2 ... 1_35465767_C_T_b37 2 1 1 2 2 2 ... 2_490638010_A_T_b37 1 2 1 2 2 2 ... 10_4567899_T_G_b37 2 2 1 2 2 2 ... ... what I want to do is extracting rows starting with "10_" like : 10_4567899_T_G_b37 2 2 1 2 2... (1 Reply)
Discussion started by: forevertl
1 Replies

2. Programming

Ok i have a small assembly question

I have this small program that runs with the flat assembler. My problem is that at the receive line function it receives the line and if there isn't a $ typed at the end of the user input the program displays a lot of strange stuff, sometimes beeps and then it seems to terminate without causing any... (13 Replies)
Discussion started by: Errigour
13 Replies

3. Shell Programming and Scripting

Small fast question

just to confirm du from sh show sizes as multiples of 512 byte right? (4 Replies)
Discussion started by: Nick1097
4 Replies

4. Shell Programming and Scripting

Question in the command of Grep

I have the following command using Grep in a complex shell script. Can you please advise what does the below expression mean? # grep ^${File1}\| $Textfile > /deve/null 2>&1 Questions, 1) Wanted to know the significance of "^" in this line 2)What does a backslash"\" before the pipeline... (2 Replies)
Discussion started by: sreedharust
2 Replies

5. Programming

A small question about fork()

Hello experts, I am using fork() in my code but I am confused which output comes first child or parent? I did the following code .My book shows parent first but my linux shows child first.Can anyone tell me why? #include <stdio.h> int main(){ int pid; printf("I am original process with pid... (5 Replies)
Discussion started by: mlhazan
5 Replies

6. UNIX for Dummies Questions & Answers

grep on file of small size

Hi everybody, I would like to carry out a grep on files below a certain size. First I thought to find the files with "find" and then pipe the result to grep. e.g. find /my_dir -size -20 -print | grep hello but in this way grep searches only the string hello in the file names. Is there a... (7 Replies)
Discussion started by: f_o_555
7 Replies

7. UNIX for Dummies Questions & Answers

Small question regarding SSH

I am looking for some model like this: My Computer ------------- Intermediate Server (IS) ------------- Own Server I must be able to ssh into the Intermediate Internet Server which is generally an online version of SSH service through which I will connect to Own Server. I was the IS to... (2 Replies)
Discussion started by: Legend986
2 Replies

8. Shell Programming and Scripting

A small minix question

First af all hi. i want to create a batch script which inform when users log in last time on system or if they are online when they logged in. I want ot use a file .users which has the usernames of users. i want to print for example peter is ONLINE: Logged in on Wed Feb 11 07:47 alex... (2 Replies)
Discussion started by: sasa
2 Replies

9. Shell Programming and Scripting

small question regarding substr()

Hello.. I am doing some awk-ing and among all I use substr inside it.. I have: ....substr($0,60,37) meaning as U all know take from 37 char. from point 60.. can I put it like this substr($0,60,end of line) meaning take it from point 60 and take all characketrs in that line until line... (2 Replies)
Discussion started by: amon
2 Replies

10. Shell Programming and Scripting

small question

Hi there, I found the following script on the net, i like to use it as a standard template for new scripts. But i do not understand the meaning of the last line, can anybody explain what going on on the last line vflag=off filename= while getopts vf: opt do case "$opt" in v)... (9 Replies)
Discussion started by: janr
9 Replies
Login or Register to Ask a Question