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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-06-2012, 11:49 AM
|
#1
|
LQ Newbie
Registered: Jul 2012
Posts: 3
Rep:
|
use output from one command to another command
I'm writing a script to take the line numbers from a command to another command such as awk. Here is what i'm trying to do. Here is what is in the animal file.
dog
cat
tiger
lion
horse
tiger
pig
grep -n "tiger" animal|cut -c 1 returns the line numbers.
3
6
I want to take those line numbers to pass it into awk to remove all the lines starting from 3 to 6. Any ideas?
|
|
|
07-06-2012, 12:12 PM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030
|
Ummm ... why? Just use awk from the start. To do what you already have:
Code:
awk '/tiger/{print NR}' animal
I'll let you try the next part
|
|
|
07-06-2012, 12:48 PM
|
#3
|
LQ Newbie
Registered: Jul 2012
Posts: 3
Original Poster
Rep:
|
Thanks for your reply. But i don't want to just print the line number. I want to pass in the line number to another command to remove those line numbers.
|
|
|
07-06-2012, 01:19 PM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030
|
I think you have missed the point a little. Yes my example currently only displays the line numbers, but your original requirement was to identify lines (line numbers) using several
commands which you would then redirect to awk (somehow) and use awk to modify the file. What I have shown you is that awk has now already identified the necessary lines, so remove the
current print option and employ the changes you wished to make.
|
|
|
07-06-2012, 01:20 PM
|
#5
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
Rep:
|
Code:
grep -n "tiger" animal | cut -c 1 | awk ...
|
|
|
07-06-2012, 01:54 PM
|
#6
|
LQ Newbie
Registered: Jul 2012
Posts: 3
Original Poster
Rep:
|
I guess i wasn't very clear. Please ignore the content. My real question is how would i use an output from the first command such as this.
3
5
6
8
And use it as an input to like awk to loop through each out put.
Sorry guys if i wasn't very clear
|
|
|
07-06-2012, 02:22 PM
|
#7
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
Rep:
|
Code:
grep -n "tiger" animal | cut -c 1 | awk ...
Use file animal as input for grep, | this is a pipe symbol that means use the output of the previous command as input for the next command. So the output of grep is piped as input for the cut command and the output of the cut command is piped as input for the awk command.
|
|
|
07-06-2012, 02:34 PM
|
#8
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
|
Quote:
Originally Posted by gqk503
I'm writing a script to take the line numbers from a command to another command such as awk. Here is what i'm trying to do. Here is what is in the animal file.
dog
cat
tiger
lion
horse
tiger
pig
grep -n "tiger" animal|cut -c 1 returns the line numbers.
3
6
I want to take those line numbers to pass it into awk to remove all the lines starting from 3 to 6. Any ideas?
|
I think sed would do what you want...
Let's say you want to delete all occurences of "tiger"
Code:
sed -e '/tiger/d' animal
But It might help us if you tell us WHAT you're trying to do, then we can help you
Give us the problem...not the solution ;-)
-C
|
|
|
07-07-2012, 12:54 PM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030
|
I agree with the above, you are trying to bend a solution in a direction it would seem it does not need to go. Instead of asking us to forget the input, supply input and required output
so we may assist you better.
|
|
|
07-08-2012, 07:56 PM
|
#10
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,419
|
As whizje has pointed out, the 'general soln' is to use the pipe operator '|' to send the output from one cmd to the input of another and you can extend that as many times as you want.
However, if you have a specific qn, then a more efficient soln may be available as per the other comments above.
HTH
|
|
|
All times are GMT -5. The time now is 02:10 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|