LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-06-2012, 11:49 AM   #1
gqk503
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: Disabled
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?
 
Old 07-06-2012, 12:12 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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
 
Old 07-06-2012, 12:48 PM   #3
gqk503
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-06-2012, 01:19 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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.
 
Old 07-06-2012, 01:20 PM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
Code:
grep -n "tiger" animal | cut -c 1 | awk ...
 
Old 07-06-2012, 01:54 PM   #6
gqk503
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-06-2012, 02:22 PM   #7
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
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.
 
Old 07-06-2012, 02:34 PM   #8
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,969
Blog Entries: 1

Rep: Reputation: 208Reputation: 208Reputation: 208
Quote:
Originally Posted by gqk503 View Post
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
 
Old 07-07-2012, 12:54 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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.
 
Old 07-08-2012, 07:56 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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
Code:
cmd1 | cmd2
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
 
  


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
Redirecting command output to input of another command mightymouse2045 Linux - Newbie 3 07-02-2011 01:28 PM
use output of previous command as parameter to another command raviluchmun Linux - Newbie 4 11-14-2010 01:35 AM
[SOLVED] append command output to file by giving command in terminal sumeet inani Linux - Newbie 4 07-03-2009 11:36 AM
how to get the absolute path information of a command from ps command output ratul_11 Linux - General 1 08-06-2008 03:10 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 05:35 PM


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

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