Linux - NewbieThis 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.
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.
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?
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.
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.
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
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.