LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-10-2012, 09:54 AM   #1
ahmed_as8
Member
 
Registered: Nov 2008
Location: Egypt
Distribution: Ubuntu, SuSE
Posts: 101

Rep: Reputation: 16
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
 
Old 04-10-2012, 10:02 AM   #2
vickyk
Member
 
Registered: Dec 2009
Posts: 38

Rep: Reputation: 6
Use the pipe |

Ex:

Quote:
cat filename|awk {'print $2'}|grep ^[0-9]
 
Old 04-10-2012, 10:12 AM   #3
yoK0
LQ Newbie
 
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29

Rep: Reputation: 0
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
 
Old 04-10-2012, 10:23 AM   #4
ahmed_as8
Member
 
Registered: Nov 2008
Location: Egypt
Distribution: Ubuntu, SuSE
Posts: 101

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by yoK0 View Post
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
 
Old 04-10-2012, 10:53 AM   #5
yoK0
LQ Newbie
 
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29

Rep: Reputation: 0
is the data file like

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

or

Ablablabla
Nblablabla

?
 
Old 04-10-2012, 10:54 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899
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.
 
Old 04-10-2012, 10:59 AM   #7
ahmed_as8
Member
 
Registered: Nov 2008
Location: Egypt
Distribution: Ubuntu, SuSE
Posts: 101

Original Poster
Rep: Reputation: 16
Solved thanks

Last edited by ahmed_as8; 04-10-2012 at 11:36 AM.
 
Old 04-10-2012, 11:40 AM   #8
yoK0
LQ Newbie
 
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29

Rep: Reputation: 0
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
 
Old 04-10-2012, 12:36 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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/

Last edited by David the H.; 04-10-2012 at 12:39 PM. Reason: fixed tag
 
Old 04-10-2012, 12:41 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899
Quote:
Solved thanks
So are you going to share the solution so others may benefit?
 
Old 04-10-2012, 01:07 PM   #11
SpaceGoat
LQ Newbie
 
Registered: Apr 2008
Location: Char*Meck, NC
Distribution: Debian, Slackware, Fedora
Posts: 7
Blog Entries: 2

Rep: Reputation: 0
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!
 
  


Reply


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
Write a script which copies content of file 1 to file 2 without using cp command. aashka Linux - Newbie 5 04-10-2012 04:55 PM
How write a script with AT command for modem enaud Linux - Hardware 26 03-28-2012 09:53 AM
[SOLVED] Trying to write a command line script that will make symbolic links dave247 Programming 5 10-21-2010 01:24 PM
List all folders containg headers [KIA]aze Programming 3 04-17-2007 06:55 AM
Trying to write a bash script -- :Command not found? bpottle Linux - Software 7 06-10-2006 07:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:52 AM.

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