-4

Example:

>"one"
>"two"
>"three"
>"title"
>12 23 14
>...

I want to remove all lines at the beginning until I reach the one in which NF==3 (awk), but the line named "title", and just once at the beginning of the file, not repeatedly.

Thank you

Expected output:

>"title"
>12 23 14
>...
  • 2
    Please avoid *"Give me the codez"* questions. Instead show the script you are working on and state where the problem is. Also see [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/608639) – jww Jul 16 '18 at 11:21
  • I've trying this: sed -i '1,/^[0-9]*/d' $fich > temp But I don't know how to remove only first coincidence and maintain the last line – Juan Manuel García Jul 16 '18 at 11:45
  • 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 Jul 16 '18 at 12:05
  • Assuming you've already read the links provided, and [this one on providing examples](https://stackoverflow.com/help/mcve) too, try `sed '1,/^"title"/d'`. (Please edit your code to show your work rather than adding it in comments.) – Paul Hodges Jul 16 '18 at 15:40

1 Answers1

0

The way to do this is by using awk as you already suggested. As you say, you want to print the lines starting from the first occurrence where you have 3 fields, this can easily be done by setting a print flag (let's call it p)'

awk '(NF==3){p=1};p' file

This will print everything starting from the first line with 3 fields.

However, you would also like to print the line which contains the string "title". This can be done by matching this string :

awk '/title/{print}(NF==3){p=1};p' file

The problem with this is that it is possible that the word 'title' will be printed twice when your file looks like

a          < not printed
title      < printed
a b c      < printed
title      < printed twice
e f g      < printed
h          < printed

So you have to be a bit more careful here with your logic and place the check together with the check when to print:

awk '(NF==3){p=1};(p || /title/)' file

This again is not robust because you might have a file like:

a          < not printed
title 1    < printed
b          < not printed
title 2    < printed
a b c      < printed
h          < printed

and you only want "title 2" to be printed:

awk '/title/{s=$0}(NF==3){p=1;print s};p' file

If the "title" just refers to the line before the first line with 3 fields, then you do

awk '(NF==3){p=1;print s};p;{s=$0}' file

or for a minor speedup:

awk '(NF==3){p=1;print s};p{print; next}{s=$0}' file
kvantour
  • 25,269
  • 4
  • 47
  • 72
  • Hello kvantour, The first command you gave Works just fine, removing every title at the beginning of the file. However, in order to maintain the previous line before the string. the second command that you facilitated me doesn't fit me well, due to "Title" doesn't have to be called that way. I simply want what you first command did but, additionally taking the line before the matching pattern, whatever this line might be. Thanks! – Juan Manuel García Jul 16 '18 at 12:32
  • @JuanManuelGarcía Sorry, your question was not clear as I assumed the string "tittle" was part of what you wanted to be printed. I have updated the answer. – kvantour Jul 16 '18 at 12:37
  • The command you facilitated me doesn't do what I've been looking for. Let me try to be more specific. I have an input file like this: line 1 line 2 line 3 line 4 "the first title" bla 1 (NF==3) bla 2 (NF==3) bla 3 (NF ==3) "another title" bla 4 (NF==3) bla 5 (NF==3) bla 6 (NF ==3) "yet another title" ... And the expect output being: "the firs title" bla 1 (NF==3) bla 2 (NF==3) bla 3 (NF ==3) "another title" bla 4 (NF==3) bla 5 (NF==3) bla 6 (NF ==3) "yet another title" ... I want to delete all lines at the beginning of the file until NF==3 but the last one. Thanks – Juan Manuel García Jul 17 '18 at 05:53
  • @JuanManuelGarcía There was a typo in the code. It should work now. – kvantour Jul 17 '18 at 11:55