-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

6
  • 1
    What have you searched for, what did you find, and why didn't it work? Commented Oct 16, 2018 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 center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question? Commented Oct 16, 2018 at 11:11
  • @tripleee not really a duplicate! The difference is that some lines need to be printed. Commented Oct 16, 2018 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. Commented Oct 16, 2018 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 Commented Oct 16, 2018 at 11:22

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

I appreciate your answer, very helpfull. Sorry, I am new here, I will try better posting the next time
@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 How to Ask and check out minimal reproducible example. In any case, Welcome to Stack Overflow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.