-1

I have a text file that looks like

file:/path/to/file
..
..
DA:34,0,0
DA:86,0,0
DA:87,0,0
..
DA:89,0,0
file:/path/to/file
..
DA:23,0,1
..
DA:24,0,1
DA:25,0,1
..

I just want to keep the first line beginning with "DA" after the line beginning with "file". Other lines starting with "DA" have to be deleted. There are a lot of other lines (I marked them with ".."), they also need to be kept.

The result should look like this:

file:/path/to/file
..
..
DA:34,0,0
..
file:/path/to/file
..
DA:23,0,1
..
..

Can anybody help me? I would be really grateful. Thanks

kvantour
  • 25,269
  • 4
  • 47
  • 72
  • 1
    What have you searched for, what did you find, and why didn't it work? – tripleee Oct 16 '18 at 10:24
  • Welcome to Stack Overflow! Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the [help] and read [ask], and especially read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – kvantour Oct 16 '18 at 11:11
  • @tripleee not really a duplicate! The difference is that some lines need to be printed. – kvantour Oct 16 '18 at 11:12
  • 1
    It would seem rather pointless to reopen just to have this reclosed for lack of attempt. Probably the OP should simply post a new and improved question with their best attempt if they still need further help. – tripleee Oct 16 '18 at 11:16
  • I used this command but I can't do that to check for the next "DA" line after line starting with "file" awk '!c && /^DA/{c=1;print;next} !/^DA/ {print} END{print l}' $i.info > temp.info && mv temp.info $i.info – Matus Spita Oct 16 '18 at 11:22
  • @MatusSpita, if you want to add code that demonstrates that your question isn't a duplicate, you should probably [edit your question](https://stackoverflow.com/posts/52833141/edit) and add the code to the question itself. – ghoti Oct 16 '18 at 11:25

1 Answers1

0

This is very closely related to Printing with sed or awk a line following a matching pattern.

What you are after is:

awk '/^file/{f=1}(f&&/^DA/){f=0;print}!/^DA/' file

How does this work?

  • /^file/{f=1}: If you find a line which starts with the word "file", set a flag f to 1
  • (f&&/^DA/){f=0;print}: If the flag f is not zero, and the line starts with DA, print the line and set the flag to zero. This makes sure you only print the first DA after file.
  • !/^DA/: print all the lines that do not start with DA

A shorter version:

awk '/^file/{f=1}(f--&&/^DA/);!/^DA/' file
kvantour
  • 25,269
  • 4
  • 47
  • 72
  • I appreciate your answer, very helpfull. Sorry, I am new here, I will try better posting the next time – Matus Spita Oct 16 '18 at 11:42
  • @MatusSpita There is no need to apologize. We have all been new here and all made the same mistakes. I strongly advise you to do the [tour] and have a look at [ask] and check out [mcve]. In any case, Welcome to Stack Overflow. – kvantour Oct 16 '18 at 12:16