Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


Closed Thread
  Search this Thread
Old 09-28-2012, 08:07 AM   #1
LQ Newbie
Registered: Sep 2012
Posts: 20

Rep: Reputation: Disabled
Bash- grep and loops

Hello all, I am in week two of learning Bash and I am lost!
My first task is to search the current directory for files with the extensions .c and .cc, and search for the strings printf and fprintf within those files. IF the print statements are found, then I am to add in a header if it's not already there.

This is where I am at so far-- I am finding the man pages hard to understand and googling everything has become overwhelming!

Can anyone maybe just give me a few hints? My errors that I have run into so far is that I can't seem to get the right syntax when I am piping my 'ls' results to my grep in the outer for I eventually decided to work from the inside out...but now I can't seem to get the right type of arguements for my inner grep :-(


for FILE in `ls *.cc *.c` # | egrep "printf\ | fprintf"

        echo $FILE;                                    #to make sure ls worked
        if [ grep -Fqx '#include <stdio.h>' "$FILE" ]  
                sed '1i\
                #include <stdio.h> ' file > new_file;
                mv new_file file;
Old 09-28-2012, 09:54 AM   #2
Registered: Oct 2011
Distribution: CentOS 5.9
Posts: 39

Rep: Reputation: 6
You may want to try using exit codes for your if statement. Something along the lines of:

grep -Fqx '#include <stdio.h>' $FILE
if [[ $? -eq 0 ]]; then
sed '1i\
#include <stdio.h> ' file > new_file;
mv new_file file;

This will do the quiet grep, and then only continue if the command was successful. If you'd like some error checking, you could put an else before the fi and echo error or something.
Old 09-28-2012, 10:23 AM   #3
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

Follow thread here:

Closed Thread

bash, grep, loop

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
[SOLVED] Bash- grep and loops calvarado777 Linux - Newbie 7 09-28-2012 04:00 PM
Bash loops verse123 Linux - Newbie 3 10-22-2011 05:25 PM
Bash Loops multiplex A/V lpn1160 Linux - General 1 02-23-2008 07:01 AM
Bash Loops UnitedWeFall Programming 8 08-21-2007 07:33 AM
bash scripting and loops phoeniks Programming 5 01-24-2005 05:00 PM

All times are GMT -5. The time now is 09:54 AM.

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