-1

I have this file:

lol.txt:hungrylol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungrylol6.txt:hungry

And I would like to turn in into this one:

lol.txt:hungry
lol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungry
lol6.txt:hungry

I already investigated with sed, tr and awk but did not find a convienient way to do it.

Thank you

aurelSon
  • 79
  • 3
  • 12
  • 4
    So what's the specific character? _After_ a `y`, or _before_ a `l`? Please clarify by directly updating the question. Showing a failed solution attempt may help. – mklement0 Mar 15 '17 at 12:09

3 Answers3

2

try:

awk '{gsub(/hungrylol/,"hungry\nlol");print}'   Input_file

I am globally substituting the string hungrylol with hungry then new line and lol and then printing the line of Input_file.

mklement0
  • 382,024
  • 64
  • 607
  • 775
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
2

With sed using BRE:

sed 's/\(hungry\)\(.\)/\1\n\2/' file

When hungry is followed by one character, output it with a new line and the captured character.

SLePort
  • 15,211
  • 3
  • 34
  • 44
1

Just add a new line after "hungry" if there was not one:

$ sed -r 's/(hungry)([^\n])/\1\n\2/g' file
lol.txt:hungry
lol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungry
lol6.txt:hungry

This matches hungry followed by a character that is not a new line. When this happens, it replaces it by hungry + new line + that captured character.

fedorqui
  • 275,237
  • 103
  • 548
  • 598