[SOLVED] bash print out a paragraph if the second word
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 am trying to print out a paragraph of text if the second word in the paragraph matches a variable, is there anyway to do this? I.e:
If I have the following text:
Code:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit.
Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue.
Nam tincidunt congue enim, ut porta lorem lacinia consectetur.
Vivamus fermentum semper porta. Nunc diam velit, adipiscing ut tristique vitae,
sagittis vel odio. Maecenas convallis ullamcorper ultricies. Curabitur ornare, ligula
semper consectetur sagittis, nisi diam iaculis velit, id fringilla sem nunc vel mi. Nam dictum,
odio nec pretium volutpat, arcu ante placerat erat
Can I grab the whole paragraph if the second word I pass in matches:
Code:
bash $ ./script.sh ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris.
Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent
et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur.
Obviously this is not a very good way to do this, but has worked up until now.
I tried to use sed but couldn't work out how to do it, any help would be much appreciated.
Well I am not going to provide a solution as you need to do a little more investigating, however, I can tell you that the '$1' parameter in your alias does not work.
To help prove it for yourself, try replacing the file path/name:
Code:
alias para='cat $2 | grep -A 5 $1'
Additionally, just remove the '$1' all together and you will see it still functions perfectly fine
Can I grab the whole paragraph if the second word I pass in matches:
you are looking for the word anywhere in the file and getting the para..
show us some effort where you tried to get the second word as per your requirement..
Another thing.. what if a para has more than five lines.. ? with what you have written will only get 5 lines of both para's after the match found..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.