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 Answer 1

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.
1
  • That was quick, thanks! And it works for me :)
    – sjngm
    Commented Jan 27, 2016 at 7:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .