Row separator

Hello All,
I need help with the below, I would appreciate any tip.

I have a file as below

 
 Input file
 Apple: Green
 Banana: Yellow
 Grapes: Black
 Apple: Red
 Banana: Green
 Grapes: Green
 Grapes: Brown
 Apple: Pale Red
 Banana: Greenish yellow
 Grapes: Brown
 Apple: Yellowish Red
 Banana: Pale Brown
 Grapes: BlackSeedless
  
 Output file:
  
 ---------------
 Apple: Green
 Banana: Yellow
 Grapes: Black
 ---------------
 Apple: Red
 Banana: Green
 Grapes: Green
 Grapes: Brown
 ---------------
 Apple: Pale Red
 Banana: Greenish yellow
 Grapes: Brown
 ---------------
 Apple: Yellowish Red
 Banana: Pale Brown
 Grapes: BlackSeedless
  
 

i.e. I want to add a header "-------" at start of the line with Apple, like the file should add header when it sees Apple.

Any help is much appreciated.

You could use sed for this simple replacement:

sed 's/^Apple:/---------------\n&/' infile > outfile

Nope, I am still seeing the same

 root@test1:/tmp# cat fru
 Apple: Green
 Banana: Yellow
 Grapes: Black
 Apple: Red
 Banana: Green
 Grapes: Green
 Grapes: Brown
 Apple: Pale Red
 Banana: Greenish yellow
 Grapes: Brown
 Apple: Yellowish Red
 Banana: Pale Brown
 Grapes: BlackSeedless
  
 
 root@test1:/tmp# sed 's/^Apple:/---------------\n&/' fru
 Apple: Green
 Banana: Yellow
 Grapes: Black
 Apple: Red
 Banana: Green
 Grapes: Green
 Grapes: Brown
 Apple: Pale Red
 Banana: Greenish yellow
 Grapes: Brown
 Apple: Yellowish Red
 Banana: Pale Brown
 Grapes: BlackSeedless

That should had worked, unless that there is white space in front of `Apple'.
In that case, remove the `^' from it.

Yahoo! it works but, I still see that Apple is not taking a new line?
It is on same line as dashes (----------).

 root@test1:/home/user1# sed 's/Apple:/---------------\n&/' /tmp/fru
 ---------------nApple: Green
 Banana: Yellow
 Grapes: Black
 ---------------nApple: Red
 Banana: Green
 Grapes: Green
 Grapes: Brown
 ---------------nApple: Pale Red
 Banana: Greenish yellow
 Grapes: Brown
 ---------------nApple: Yellowish Red
 Banana: Pale Brown
 Grapes: BlackSeedless

Does this work better?

perl -wlpe '/Apple:/ and print "----------------";' fru

Oops! I don't have perl :frowning:

That's most unfortunate.
You have a version of sed that does not like or support escape characters.
You do not have Perl.
What else do we have?
Do you have awk?

awk '/Apple:/{print "--------------"}1' fru
2 Likes

Some sed support \n some don't try:

sed 's/Apple:/---------------\
&/' /tmp/fru
1 Like
awk '{sub("^ *Apple:","---\n&")}1' myFile
1 Like

Thanks Aia,
The awk worked for me :slight_smile:

Also, thanks to XL, but sed is still giving me same output.

Thanks a bunch guys!

Thanks vgersh99, that works too!

There is a space at the start of each line (including the line with the hyphens). This sed script seems to work with the given data:

sed 's/^.*Apple:/ ---------------\
&/' file

sed has insertion:

sed '/Apple:/i\
---------------' /tmp/fru

This simple awk command works fine:

$ awk '/Apple:/{print "------------"} {print }' fru

Jean-Paul

but aia's answer is even shorter :

To only use one line with sed:

sed '/Apple/{h;s/.*/---------------/;G;}' file

\n in the replacement part to designate a newline is a GNU extension..