0

I have a variable which contains output from a shell command, I'm trying to 'grep' the variable but it seems that the output is all in one line without linebreaks. So when I 'grep' the variable everything is returned.

For example my variable contains this:

 standalone 123 abc greefree ten1 stndalone 334 abc fregree ten1 stndalone 334 abc fregree ten2 stndalone 334 abc fregree ten2 stndalone 334 abc fregree ten1 stndalone 334 abc fregree ten2 stndalone 334 abc fregree ten1 stndalone 334 abc fregree ten1 stndalone 334 abc fregree ten2 

I'd like to insert a line break everywhere there is ten1 or ten2. So that when I 'grep' the variable I am only returned a particular line.

Output similar to this

standalone 123 abc greefree ten1 
stndalone 334 abc fregree ten1 
stndalone 334 abc fregree ten2 
stndalone 334 abc fregree ten2 
stndalone 334 abc fregree ten1 
stndalone 334 abc fregree ten2 
stndalone 334 abc fregree ten1 
stndalone 334 abc fregree ten1 
stndalone 334 abc fregree ten2

Is there a bash command I can use or some type of regex expression?

Sylvester
  • 27
  • 5
  • Welcome to SO, on SO we do encourage users to use search functionality of SO and add their efforts too in their question which they have put in order to solve their own problems, so kindly do so and let us know then. – RavinderSingh13 Nov 22 '19 at 15:20
  • Hi @RavinderSingh13 I've looked at some of the existing problems similar to mine on SO but there are none quite as similar to mine. My variable contains a lenghty output so I don't think I can use the method outlined in this problem for example. https://stackoverflow.com/questions/3005963/how-can-i-have-a-newline-in-a-string-in-sh – Sylvester Nov 22 '19 at 15:25
  • You could add these efforts in your post always, cheers and happy learning on this great site SO. – RavinderSingh13 Nov 22 '19 at 15:36

2 Answers2

1

Could you please try following once.

sed 's/\r//g;s/ten[12]/&\n/g' Input_file

To remove spaces at last try(which will be remaining in above code):

sed -E 's/\r//g;s/ten[12]/&\n/g'  Input_file | sed -E 's/^ +//'

To edit based on multiple patterns

sed -e 's/\r//g;s/ten[12]/&\n/g' -e 's/\r//g;s/abc[12]/&\n/g' Input_file
Sylvester
  • 27
  • 5
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Thanks Ravi, your solution is inputting a new line but 'ten1 and ten2' should be the last things printed. I would like the new line to be created after those characters. Also if the pattern i would like to work with is not similar like 'ten1,ten2'. How would I go about making this solution work for multiple patterns for example, 'abc1, ten1, xyz2' ? – Sylvester Nov 22 '19 at 16:13
  • @Sylvester, Sure edited it now, please check and lemme know then, cheers. – RavinderSingh13 Nov 22 '19 at 16:14
  • 1
    Thanks this was helpful, I used '-e' option to make this work for multiple patterns. – Sylvester Nov 22 '19 at 16:28
  • 1
    @Sylvester, Sure, thank you for improving it, appreciate that. Feel free to add it in my solution and I will approve your EDIT, cheers and happy learning on this great site SO. – RavinderSingh13 Nov 22 '19 at 16:29
1

Here's a combination of sed and tr that might get you closer to what you want:

sed -e 's/ten1/^M/g' -e 's/ten2/^M/g' | tr \\r \\n

So, let's say you have a variable called LONG, you can use the above code as follows:

echo "$LONG" | sed -e 's/ten1/^M/g' -e 's/ten2/^M/g' | tr \\r \\n

Above, we are converting all of the ten1 and ten2 strings to CR (carriage return) and then we are using tr to change those CR's into newlines. Put your grep in after statement:

echo "$LONG" | sed -e 's/ten1/^M/g' -e 's/ten2/^M/g' | tr \\r \\n | grep whatever

After re-reading your question and the other answer, I am providing this bit of code to also include the original ten1 or ten2:

 echo "$LONG" | sed 's/ten[12] /&^M/g'  | tr \\r \\n

And this is the output:

standalone 123 abc greefree ten1 
stndalone 334 abc fregree ten1 
stndalone 334 abc fregree ten2 
stndalone 334 abc fregree ten2 
stndalone 334 abc fregree ten1 
stndalone 334 abc fregree ten2 
stndalone 334 abc fregree ten1 
stndalone 334 abc fregree ten1 
stndalone 334 abc fregree ten2
Mark
  • 4,249
  • 1
  • 18
  • 27
  • 1
    Oh, by the way, you can't just paste the ^M. That's really a chr(13) sitting in the middle of the line. You can type it by first typing Ctrl-V and then Ctrl-M. I tried to do this with \n as Ravinder is suggeseting, but it just put n's in my output. – Mark Nov 22 '19 at 15:38
  • To be honest, my code worked for me :) and didn't find control M characters, though it is good to mention here, cheers buddy. – RavinderSingh13 Nov 22 '19 at 16:00
  • Thanks for your help guys, @Mark I have tried to implement your solution but I don't seem to be getting a new line printed at that point, instead 'ten1' or 'ten2' no longer appear in the output, which is still all on one line – Sylvester Nov 22 '19 at 16:08
  • your latest solution which is similar to @RavinderSingh13 how would i get that to take the variable as you are currently hardcoding it in, in your solution. – Sylvester Nov 22 '19 at 16:09
  • @Sylvester, I added command in `sed` to take care of control M characters, please check it once. – RavinderSingh13 Nov 22 '19 at 16:11
  • @Sylvester, I modified the post to show you how to take the variable instead of hard coding the input. You could have also just replaced the long string with $LONG as well. It is my understanding that `echo "$LONG" | sed` and `sed <<< "$LONG"` are equivalent. – Mark Nov 22 '19 at 16:30