LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   merging two command in one command (https://www.linuxquestions.org/questions/linux-newbie-8/merging-two-command-in-one-command-789897/)

sumanch 02-18-2010 03:25 AM

merging two command in one command
 
Hi ,
I have a file and it has some content . Now I want to check for some text
in that file and if does not exist I want to append it . So I need to first grep and then do "echo "content" >> s.txt" . Is there any way to execute this two commands in a single command . Please let me know .

regards
Suman

druuna 02-18-2010 03:31 AM

Hi,

I'm sure that is possible (awk comes to mind), but why the need for one command, it would complicate things (just my 2c).

This grep something file || echo "text" >> file will do what you want. Short and elegant.

The || is an OR statement, if grep doesn't find anything ("fails" if you want) the echo statement is executed. There is also the && (AND) command (do something if previous command is successful).

Hope this helps.

Tinkster 02-18-2010 03:32 AM

Like in

Code:

grep -v "some text" s.txt && echo "some text" >> s.txt
?


P.S.: too slow ;D

evo2 02-18-2010 03:34 AM

Do you mean something like:
Code:

grep -q content s.txt || echo content >> s.txt
This should append "content" to the file s.txt if it is not there already.

Evo2.

PS. Much too slow!


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