1

I am beginner in unix programming and a way to automate my work
I want to run a list a grep commands and get the output of all the grep command in a in a single delimited file .

i am using the following bash script. But it's not working .

Mockup sh file:

!/bin/sh
grep -l abcd123
grep -l abcd124
grep -l abcd125

and while running i used the following command

$ ./Mockup.sh > output.txt

Is it the right command?

How can I get both the grep command and output in the output file?

how can i delimit the output after each command and result?

4
  • Things: you need #!/bin/sh to make it work. grep needs at least two arguments: the search string and the file where to look for. grep -l abcd123 needs more info. Commented Nov 5, 2014 at 12:54
  • You need to show us what your input and desired output look like. Commented Nov 5, 2014 at 12:55
  • when i give the following input inside a script' grep -l abcd123, grep -l abcd124' i want to get a output some thing like ' grep -l abcd123 <some kind of delimiter> sample1.txt , grep -l abcd124 <some kind of delimiter> sample2.txt Commented Nov 6, 2014 at 6:50
  • Please help me on this Commented Nov 6, 2014 at 6:53

4 Answers 4

1

How can I get both the grep command and output in the output file

You can use bash -v (verbose) to print each command before execution on stderr and it's output will be as usual be available on stdout:

bash -v ./Mockup.sh > output.txt 2>&1

cat output.txt

Working Demo

Sign up to request clarification or add additional context in comments.

5 Comments

@Downvoter: Question was How can I get both the grep command and output in the output file?
Disclaimer: the downvoter is someone else! That said, if I were forced to find a defect in your answer I'd say that Mockup.sh, as posted in the original question, would benefit from some small correction. Ciao
i tried but am getting only the output in that file. The input grep command is not available in the file.
That's not true, if you use bash -v it does print each and every command on stderr. Make sure you copy/paste command as is using > output.txt 2>&1
@Kaleem: I have included a working demo in my answer.
1

A suitable shell script could be

#!/bin/sh
grep -l 'abcd123\|abcd124\|abcd125' "$@"

provided that the filenames you pass on the invocation of the script are "well behaved", that is no whitespace in them. (Edit Using the "$@" expansion takes care of generic whitespace in the filenames, tx to triplee for his/her comment)

This kind of invocation (with alternative matching strings, as per the \| syntax) has the added advantage that you have exactly one occurrence of a filename in your final list, because grep -l prints once the filename as soon as it finds the first occurrence of one of the three strings in a file.


Addendum about "$@"

% ff () { for i in "$@" ; do printf "[%s]\n" "$i" ; done ; }
% # NB "a s d" below is indeed "a SPACE s TAB d"
% ff "a s d" "    ert  " '345
345'
[a s    d]
[    ert  ]
[345
345]
% 

1 Comment

+1 but you should be using "$@" instead of $*. Both the different variable name and the double quotes are significant. That way, you no longer have any constraint on what file names you can use with this script.
0
cat myscript.sh 
########################
#!/bin/bash
echo "Trying to find the file contenting the below string, relace your string with below string"
grep "string" /path/to/folder/* -R -l
########################

save above file and run it as below

sh myscript.sh > output.txt 

once the command prmpt get return you can check the output.txt for require output.

Comments

0

Another approach, less efficient, that tries to address the OP question

How can I get both the grep command and output in the output file?

% cat Mockup
#!/bin/sh
grep -o -e string1 -e string2 -e string3 "$@" 2> /dev/null | sort -t: -k2  | uniq

Output: (mocked up as well)

% sh Mockup file{01..99}
file01:string1
file17:string1
file44:string1
file33:string2
file44:string2
file48:string2
% 

looking at the output from POV of a consumer, one foresees problems with search strings and/or file names containing colons... oh well, that's another Q maybe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.