|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello -
I need to find a line in a file that starts with a % and then delete the very next line leaving the line that starts with the %. I can find the line by using ^% but no idea how the delete the next line. Any ideas how to do this? HNB |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2006-08-17, heynetboy wrote:
> Hello - > > I need to find a line in a file that starts with a % and then delete > the very next line leaving the line that starts with the %. I can find > the line by using ^% but no idea how the delete the next line. awk 'n != 1 { print $0 } { n = 0 } /^%/ { n = 1 } ' -- Chris F.A. Johnson, author <http://cfaj.freeshell.org> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
heynetboy wrote:
> Hello - > > I need to find a line in a file that starts with a % and then delete > the very next line leaving the line that starts with the %. I can find > the line by using ^% but no idea how the delete the next line. > > Any ideas how to do this? > > HNB if there is no adjacent lines starting with '%' then perl -pe '<> if /^%/' sed '/^%/N;s/\n[^\n]*$//' awk '/^%/{print;getline;next}1' else redefine the rule Xicheng |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
heynetboy wrote:
> I need to find a line in a file that starts with a % and then delete > the very next line leaving the line that starts with the %. I can find > the line by using ^% but no idea how the delete the next line. > Any ideas how to do this? sed '/^%/{n;d}' -- pgancarz, at, o2, pl |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 17 Aug 2006 09:39:54 -0700, heynetboy
<heynetboy@gmail.com> wrote: > Hello - > > I need to find a line in a file that starts with a % and then delete > the very next line leaving the line that starts with the %. I can find > the line by using ^% but no idea how the delete the next line. > > Any ideas how to do this? > > HNB > awk '{print} /^%/{getline}' What if two or more consecutive lines begin with %? -- Graduate students and most professors are no smarter than undergrads. They're just older. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
heynetboy wrote: > Hello - > > I need to find a line in a file that starts with a % and then delete > the very next line leaving the line that starts with the %. I can find > the line by using ^% but no idea how the delete the next line. > > Any ideas how to do this? WOW - what great response. The last one sed '/^%/{n;d;}' was the shortest and easiest to understand (I don't know AWK or PERL very well). I just had to add the last ";" to make the single line command work Thanks everyone! HNB |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
2006-08-17, 17:28(+00), Pawel Gancarz:
> heynetboy wrote: >> I need to find a line in a file that starts with a % and then delete >> the very next line leaving the line that starts with the %. I can find >> the line by using ^% but no idea how the delete the next line. >> Any ideas how to do this? > > sed '/^%/{n;d}' sed '/^%/{n;d;}' -- Stéphane |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Stephane CHAZELAS wrote:
> sed '/^%/{n;d;}' Sure, thanks. -- pgancarz, at, o2, pl |
|
![]() |
| Outils de la discussion | |
|
|