LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2014, 03:46 PM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
bash print out a paragraph if the second word


Hello all,

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.
Many thanks,
trscookie
 
Old 12-17-2014, 06:10 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
post the script.sh code, we need to see what you have done, there are a number of ways to do this
 
Old 12-17-2014, 08:03 PM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Hi, cheers for you response.

At the minute I am just using an alias, like so:

Code:
alias para='cat ~/textfile.txt | grep -A 5 $1'
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.
 
Old 12-18-2014, 01:56 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
is this homework?

Last edited by bigearsbilly; 12-18-2014 at 02:03 AM. Reason: might be homework
 
Old 12-18-2014, 02:15 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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
 
Old 12-18-2014, 02:30 AM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
Quote:
Originally Posted by trscookie View Post

Code:
alias para='cat ~/textfile.txt | grep -A 5 $1'
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..
 
Old 12-19-2014, 03:16 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by trscookie View Post
I am trying to print out a paragraph of text if the second word in the paragraph matches a variable ...
OP asked for a bash solution, so this post is not doing his homework for him.

As a learning exercise I wrote an awk solution and was pleased to see it could be done in one line.
Code:
v="ipsum"  # Set the value of variable "v"
awk -v v=$v 'BEGIN{ORS=RS="\n\n"} {if ($2==v) print}' $Latin >$OutFile
Daniel B. Martin
 
1 members found this post helpful.
Old 12-19-2014, 04:35 PM   #8
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by danielbmartin View Post
OP asked for a bash solution, so this post is not doing his homework for him.

As a learning exercise I wrote an awk solution and was pleased to see it could be done in one line.
Code:
v="ipsum"  # Set the value of variable "v"
awk -v v=$v 'BEGIN{ORS=RS="\n\n"} {if ($2==v) print}' $Latin >$OutFile
Daniel B. Martin


Cheers Daniel! I managed to cobble together a temporary fix, which worked for me. I will definitely try your solution:

Code:
cat ${CONFIG} | sed -n "/$VAR/,/^$/p" | grep -v "^$"

Also, I would like to add that parameters for aliases works for me (might not be good practice) but it does work.
 
  


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
Request for bash Script to comment Paragraph with a particular occourance. nidhintomson Programming 11 09-10-2014 03:12 PM
[SOLVED] Script to get the word count of a paragraph from a long message lakshminarayanan Linux - Newbie 7 11-10-2011 01:07 AM
print second word in 1st line along with 5th word in all the lines after the first bangaram Programming 5 08-31-2009 03:42 AM
grep and print paragraph B-Boy Programming 2 08-11-2008 02:30 AM
Word count in paragraph - Open Office, Sutekh Linux - Software 10 04-19-2003 10:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 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
Open Source Consulting | Domain Registration