0

Let's say I have a text file like this:

group1 hello
group1 there
group1 nice line
group2 here
group2 we
group2 go

What I want to do is insert a line when the group of lines changes, something like:

group1 hello
group1 there
group1 nice line
---------------------------------------
group2 here
group2 we
group2 go

I don't care if that line also appears as the first and/or last line. At this time I only need something that separates the information.

How do I do that with sed or awk? Or maybe there are other ways?

1

With awk:

awk 'NR!=1&&x!=$1{print "---"} {x=$1}1' file
  • NR!=1&&x!=$1: applies for all lines, except the first line and checks if the first field has changed.
  • print "---": if yes, print the dashes.
  • {x=$1}: set x for the next iteration.
  • 1: a true condition that awk prints each line.
  • That was quick, thanks! And it works for me :) – sjngm Jan 27 '16 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.