LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to write a script containg more than 1 command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-write-a-script-containg-more-than-1-command-939087/)

ahmed_as8 04-10-2012 08:54 AM

How to write a script containg more than 1 command
 
Hi all,

I have a file that I want to do some operations on, 1st I want to grep certain number then I want to use awk to get numbers between 0 - 9 then I am going to use egrep to get the values X or Y

I have all commands but all they work separate, I want to see the script that run all these commands then output me the result :)

Thanks

vickyk 04-10-2012 09:02 AM

Use the pipe |

Ex:

Quote:

cat filename|awk {'print $2'}|grep ^[0-9]

yoK0 04-10-2012 09:12 AM

Or you can play a little bit and simply put all your commands into a file

do chmod u+x <your_file>

you now created script "your_file", you can run it like this

./your_file

ahmed_as8 04-10-2012 09:23 AM

Quote:

Originally Posted by yoK0 (Post 4649269)
Or you can play a little bit and simply put all your commands into a file

do chmod u+x <your_file>

you now created script "your_file", you can run it like this

./your_file

That is actually what I want to do, but not all commands are being processed


grep 012[01-2]=x data.txt
egrep -w 'A|N' data.txt
awk -v x=5 ' $2 <= x ' data.txt


here only grep & awk are done the egrep result is not processed

yoK0 04-10-2012 09:53 AM

is the data file like

A bla bla bla
A bla bla bla
N bla bla bla
?

or

Ablablabla
Nblablabla

?

grail 04-10-2012 09:54 AM

Quote:

here only grep & awk are done the egrep result is not processed
And what evidence do you have to support this claim? Providing vague references and no examples make it virtually impossible to assist you.

ahmed_as8 04-10-2012 09:59 AM

Solved thanks

yoK0 04-10-2012 10:40 AM

I run it and egrep works fine, instead the grep didnt executed, im not even sure what it does

i solved it changing grep 012[01-2]=x data.txt to grep 012[01-2] data.txt, have no idea why '=x' was there for ! ??

to make your script more flexible you can use arguments instead static path in your script

change data.txt to $1 and run script as ./script.sh some_file

David the H. 04-10-2012 11:36 AM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


It's a good idea to quote your grep patterns, especially if it contains characters that are special to the shell.


Actually, there's generally no need to use grep and awk (or sed) together. awk is a full text-processing scripting language and can do all the pattern matching on its own.

Code:


#instead of:
grep 'expression' infile | awk '{print $2}'

#use
awk '/expression/ {print $2}' infile


Again, if you would post an actual example of the text and the part you want to extract from it, we could help you design the proper command for it.


Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

grail 04-10-2012 11:41 AM

Quote:

Solved thanks
So are you going to share the solution so others may benefit?

SpaceGoat 04-10-2012 12:07 PM

Wow! This is a prime example of why this forum is so awesome. The speed and clarity which this issue was solved was phenomenal. I agree with Grail, that ahmed_as8 should consider sharing the solution. Peace out, penguinos!


All times are GMT -5. The time now is 01:06 PM.