Bash script - Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script - Grep
# 1  
Old 04-17-2011
Bash script - Grep

Hi,

I am very new to bash scripting and I need to write a bash script that takes two arguments, a string and a file. The output should be each line which matches the string *from the beginning of the line*. For example, given a string "ANA" the line starting with "ANABEL" will be printed, but the line starting with "SUSANA" will not. I need to use grep.

what I have tried is as follows:

Code:
 
#!/bin/bash
grep $1 $2

But this doesn't work, any help or direction will be very help, thanks in advance.
# 2  
Old 04-17-2011
You should use it this way

grep ^$1 $2

^$1 indicated to search at the beginning of the line


regards,
Ahamed
# 3  
Old 04-17-2011
Thanks, but this doesn't work without using echo command inside the script file and I don't know how to make it work.
# 4  
Old 04-17-2011
Can you show us the code (with echo)?
# 5  
Old 04-17-2011
Yes, here is what I have tried inside the script file but it does not work:

Quote:
a = $1 ^$2 grep
echo a
thanks.
# 6  
Old 04-17-2011
Something like this

Code:
echo "move" important.data | while read pattern filename
pipe while> do
pipe while> grep "^$pattern" $filename
pipe while> done

# 7  
Old 04-17-2011
Try this

Code:
a=`grep ^$2 $1`        #assuming, $1 is the file and $2 is the search pattern
echo $a

regards,
Ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Bash: Need help with grep

Hi, I want to write a bash that can turn an application on if it is off or on if it is off. I found a script for LibreELEC, but my OS is OSMC (a Linux distribution, too). I already modiefied lines 5 and 7: #!/bin/sh SERVICE='hyperiond' if ps | grep -v grep | grep $SERVICE > /dev/null then... (3 Replies)
Discussion started by: Chuchu211
3 Replies

3. Shell Programming and Scripting

Is there a BASH script allowing me to grep specifics from /var/log/messages?

I am wondering if there is a script (if one exists, not confident in my own scripting ability) that is able to bring up specified information from the /var/log/messages. I need to show logged traffic on specific dates and times and protocols (ie. Show all insecure FTP traffic (most likely via... (13 Replies)
Discussion started by: vgplayer54
13 Replies

4. Shell Programming and Scripting

How to use grep in a loop using a bash script?

Dear all, Please help with the following. I have a file, let's call it data.txt, that has 3 columns and approx 700,000 lines, and looks like this: rs1234 A C rs1236 T G rs2345 G T Please use code tags as required by forum rules! I have a second file, called reference.txt,... (1 Reply)
Discussion started by: aberg
1 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Grep in bash script

Hi Experts, I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script. Error message: "grep: sales.txt: No such file or directory" #!/bin/bash var=1 var1=`awk '{n++} END {print n}' sales.txt` while ] do prod=$var... (6 Replies)
Discussion started by: parpaa
6 Replies

6. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

7. Shell Programming and Scripting

pipe to grep doesn't work in bash script

Hi, I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script: cmnd="gvfs-mount -l | grep -i... (4 Replies)
Discussion started by: kcstrom
4 Replies

8. Shell Programming and Scripting

Problem using grep in bash script

When it comes to programing and UNIX, I know just enough to be really really dangerous. I have written a python script to parse through a file that contains ~1 million lines. Depending on whether a certain string is matched, the line is copied into a particular file. For the sake of brevity,... (4 Replies)
Discussion started by: errcricket
4 Replies

9. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

10. Shell Programming and Scripting

Bash script (using find and grep)

I'm trying to make a simple search script but cannot get it right. The script should search for keywords inside files. Then return the file paths in a variable. (Each file path separated with \n). #!/bin/bash SEARCHQUERY="searchword1 searchword2 searchword3"; for WORD in $SEARCHQUERY do ... (6 Replies)
Discussion started by: limmer
6 Replies
Login or Register to Ask a Question