LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 05-22-2018, 01:03 PM   #1
gigiux
LQ Newbie
 
Registered: Feb 2018
Posts: 19

Rep: Reputation: Disabled
Maintain newline in grep's output


Hello,

I am parsing a file using grep and the output on screen contains newline, as here:
Code:
    $ grep 'gene' sequence.gb
         gene            89..1483
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
         gene            complement(1987..2763)
                         /gene="nucleocapsid protein"
                         /gene="nucleocapsid protein"
I can assign this to a variable and print out still with the newlines:

Code:
    $ gene=$(grep 'gene' sequence.gb)
    echo "$gene"
         gene            89..1483
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
         gene            complement(1987..2763)
                         /gene="nucleocapsid protein"
                         /gene="nucleocapsid protein"
but this does not contain real newlines, since if I grep again for the lines containing '..' I get the whole lot:

Code:
    $ echo "$gene" | grep '..'
         gene            89..1483
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
                         /gene="non-structural protein"
         gene            complement(1987..2763)
                         /gene="nucleocapsid protein"
                         /gene="nucleocapsid protein"
We can see that this is a single string by not using the quotes:

Code:
    $ echo $gene
    gene 89..1483 /gene="non-structural protein" /gene="non-structural protein" /gene="non-structural protein" /gene="non-structural protein" /gene="non-structural protein" /gene="non-structural protein" /gene="non-structural protein" gene complement(1987..2763) /gene="nucleocapsid protein" /gene="nucleocapsid protein"
So my question is, how can I maintain newline formatting or introducing it?

Thank you
 
Old 05-22-2018, 02:54 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The newlines are there.
But .. in grep means two any-characters. Gets every line with two or more characters.
You need \.\. or [.][.] to match literal ..
 
1 members found this post helpful.
Old 05-23-2018, 02:28 PM   #3
gigiux
LQ Newbie
 
Registered: Feb 2018
Posts: 19

Original Poster
Rep: Reputation: Disabled
I completely forgot about the regex issue . Of course it does, thank you! Now it is, as I wanted:
Code:
$ echo "$gene" | grep '\.\.'
gene            89..1483
gene            complement(1987..2763)
 
  


Reply

Tags
bash, grep, newline



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] Conky: Maintain Curl Output When Off-line? jakfish Linux - General 6 09-24-2016 10:19 PM
[SOLVED] Need to insert a newline after 3rd line of grep pattern search zabi Linux - Newbie 12 12-24-2013 12:39 PM
insert a newline after output command micronemo Linux - Newbie 1 09-13-2012 10:16 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:01 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