LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-12-2014, 07:04 AM   #1
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka
Distribution: Arch Linux
Posts: 69

Rep: Reputation: 13
Bash : How to print words between brackets


This is my text file.

Code:
Blah.. Blah.. Blah.. Blah.. Blah.. 
Blah.. Blah.. Blah.. Blah.. Blah.. 
Blah.. Blah.. Blah.. Blah.. Blah.. 

TEXT=('ABC'
      'DEF'
      'GHI')

Blah.. Blah.. Blah.. Blah.. Blah.. 
Blah.. Blah.. Blah.. Blah.. Blah.. 
Blah.. Blah.. Blah.. Blah.. Blah..
I want to print "ABC", "DEF" and "GHI" in terminal, like this.

Code:
[enindu@enindu-pc ~]$ ABC DEF GHI
Still learning Bash. Is there any way to do that. Thanks in advance.
 
Old 05-12-2014, 07:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
sure, there is a way. it is not bash, but sed, awk or something.
For example:
Code:
awk ' BEGIN { FS="[()]"; RS="QQQQQQ" } { print $2 } ' yourfilename
 
Old 05-12-2014, 08:56 AM   #3
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka
Distribution: Arch Linux
Posts: 69

Original Poster
Rep: Reputation: 13
Well, please can you describe that command? What if I have two bracket fields in text document? Like this.

Code:
TEXT_1=('ABC'
        'DEF'
        'GHI')

TEXT_2=('JKL'
        'MNO'
        'PQR')
And I want to print "JKL", "MNO" and "PQR". How can I do it using that "awk" command? Thanks again.
 
Old 05-12-2014, 10:05 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You asked for help and were provided a possible solution. It is now up to you to do some work. Of course ask questions when stuck, but please put in some effort.
 
Old 05-13-2014, 02:33 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Ok, so see man awk about general syntax. FS is a field separator, it is set to ( or ). RS is set to QQQQQ which probably will never occur in your source, therefore all the input file taken as one single line. It is splitted by FS and printed the second part. You can freely improve it for example by using gsub to replace parts of the text and add print $4 ....
But first you would need to investigate, understand how it works and try to adjust it according to your real needs. That will give you a much usable result.
 
1 members found this post helpful.
  


Reply



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
[SOLVED] bash to print words/lines horizontally nitya Linux - Newbie 12 02-21-2013 05:32 AM
[SOLVED] bash to print words/lines vertically nitya Linux - Newbie 6 02-20-2013 06:19 AM
[SOLVED] bash print only found words newbie0101 Linux - Software 5 01-13-2012 03:09 AM
brackets in bash shell lipun4u Programming 3 06-19-2010 08:24 AM
Two square brackets in bash condition tirengarfio Programming 1 07-07-2009 12:36 PM

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

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