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?

3
  • 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. Commented Nov 22, 2019 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. stackoverflow.com/questions/3005963/… Commented Nov 22, 2019 at 15:25
  • You could add these efforts in your post always, cheers and happy learning on this great site SO. Commented Nov 22, 2019 at 15:36

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

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, Sure edited it now, please check and lemme know then, cheers.
Thanks this was helpful, I used '-e' option to make this work for multiple patterns.
@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.
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

6 Comments

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.
To be honest, my code worked for me :) and didn't find control M characters, though it is good to mention here, cheers buddy.
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
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, I added command in sed to take care of control M characters, please check it once.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.