-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
  • 22,845
  • 4
  • 45
  • 58
  • 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

2 Answers2

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
  • 22,845
  • 4
  • 45
  • 58
  • 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
-1

You can find line numbers for all lines for example like that

$ a=$(cat test.txt | grep -n DA |cut -f1 -d: | awk 'NR>1' |xargs| tr " " "|" ); echo $a;
4|5|7

and then you can use sed to remove all of this

$ awk 'NR!~/^(4|5|7)$/' test.txt;
kvantour
  • 22,845
  • 4
  • 45
  • 58
rkrankus
  • 72
  • 1
  • This will probably not work. I do not see any check that searches for the first `DA` after `file` – kvantour Oct 16 '18 at 11:10
  • oh sorry, my mistake i thought we were talking about different files. – rkrankus Oct 16 '18 at 11:12
  • I tried this, but it only keeps the first occurrence of the line beginning with "DA". What I want is to keep the first occurrence of the line beginning with "DA" AFTER EACH line beginning with "file". But thanks either – Matus Spita Oct 16 '18 at 11:15