I have a text file like this
line1
line2
line3
line4
line5
line6
....
....
....
....
....
line4 and line13 and line22 (every 9th line after 4the line is empty).
So i want to delete these lines and wanted to insert a blank line
before line1 and line12 and line23 so on)
i could successfully insert a blank line as i required with the
following code.
awk 'BEGIN{print "" } # which insert a blank line before line1
{ print $0 # starts printing line1 onwards
if((NR-1)%11==0){print ""}' input file > outputfile # prints blank
line before 11th 22nd 33rd .... so on
I tried to delete required line with following code but it did not
work...
if(NR==4 || (NR-4)%9==0){next}' input > output
what must be the wrong in my code...
give me a single solution for my entire problem.
|
|
0
|
|
|
|
Reply
|
nag
|
10/26/2008 11:34:15 AM |
|
On 10/26/2008 6:34 AM, nag wrote:
> I have a text file like this
>
>
> line1
> line2
> line3
> line4
> line5
> line6
> ...
> ...
> ...
> ...
> ...
>
> line4 and line13 and line22 (every 9th line after 4the line is empty).
> So i want to delete these lines and wanted to insert a blank line
> before line1 and line12 and line23 so on)
>
> i could successfully insert a blank line as i required with the
> following code.
>
> awk 'BEGIN{print "" } # which insert a blank line before line1
> { print $0 # starts printing line1 onwards
> if((NR-1)%11==0){print ""}' input file > outputfile # prints blank
> line before 11th 22nd 33rd .... so on
>
> I tried to delete required line with following code but it did not
> work...
>
> if(NR==4 || (NR-4)%9==0){next}' input > output
>
> what must be the wrong in my code...
>
> give me a single solution for my entire problem.
>
Get the book "Effective Awk Programming, Third Edition By Arnold Robbins".
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
10/26/2008 1:31:25 PM
|
|
On Oct 26, 6:31=A0pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 10/26/2008 6:34 AM, nag wrote:
>
>
>
> > I have a text file like this
>
> > line1
> > line2
> > line3
> > line4
> > line5
> > line6
> > ...
> > ...
> > ...
> > ...
> > ...
>
> > line4 and line13 and line22 (every 9th line after 4the line is empty).
> > So i want to delete these lines and wanted to insert a blank line
> > before line1 and line12 and line23 so on)
>
> > i could successfully insert a blank line as i required with the
> > following code.
>
> > awk 'BEGIN{print "" =A0} =A0 =A0# which insert a blank line before line=
1
> > { print $0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# starts printing=
line1 onwards
> > if((NR-1)%11=3D=3D0){print ""}' input file > outputfile # prints blank
> > line before 11th 22nd 33rd .... so on
>
> > I tried to delete required line with following code but it did not
> > work...
>
> > if(NR=3D=3D4 || (NR-4)%9=3D=3D0){next}' input > output
>
> > what must be the wrong in my code...
>
> > give me a single solution for my entire problem.
>
> Get the book "Effective Awk Programming, Third Edition By Arnold Robbins"=
..
>
> =A0 =A0 =A0 =A0 Ed.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Hi all,
Actually i am trying to re-adjust a printfile. The actual printfile
cannot
be adjusted in our bank cheque printing stationery. So I want to
modify the output.
For example (here i am giving a sample real output before
modification.)
line1 12.0 123456789 28/11/2008
Neel Anderson
Ten Thousand Dollars ****10000.00
line10 from here next bill starts...
My requirements are:
1) I want to move the date field towards right by 4 spaces.
2) Insert one blank line before first line
3) Remove the 4the blank line.
4) Insert two blank lines between name and amount in words.
|
|
0
|
|
|
|
Reply
|
nag
|
10/28/2008 2:27:40 AM
|
|
Ed Morton <morton@lsupcaemnt.com> wrote:
> Get the book "Effective Awk Programming, Third Edition By Arnold Robbins".
The problem that I had with that book is that it is very gawk centred,
and sometimes it is difficult to tell which bits are awk, and which bits
relate only to gawk. The book makes some attempt to differentiate, but I
don't think it does this well, and maybe uses the word "awk" when
it should really be using the word "gawk". On the plus side, it is under
the free documentation licence, so at least it can be corrected.
However, at this time, I would go for a different awk book.
Regards,
Mark.
--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/
|
|
0
|
|
|
|
Reply
|
markhobley
|
11/9/2008 10:47:38 AM
|
|
Mark Hobley wrote:
> Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>>Get the book "Effective Awk Programming, Third Edition By Arnold Robbins".
>
>
> The problem that I had with that book is that it is very gawk centred,
I don't think so. But, of course, it describes the GNU awk superset of
the standard awk.
Have a look at the Appendix which might support some of your demands.
> and sometimes it is difficult to tell which bits are awk, and which bits
> relate only to gawk. The book makes some attempt to differentiate, but I
> don't think it does this well, and maybe uses the word "awk" when
I think GNU awk specifics are well documented in the book.
> it should really be using the word "gawk". On the plus side, it is under
> the free documentation licence, so at least it can be corrected.
>
> However, at this time, I would go for a different awk book.
Yes, if that book doesn't fit your personal style that's the way to go.
If you want to know about just the "standard" awk functionality have a
look at the book from the three awk inventors, Aho, Kernighan, and,
Weinberger
Janis
>
> Regards,
>
> Mark.
>
|
|
0
|
|
|
|
Reply
|
Janis
|
11/9/2008 11:21:33 AM
|
|
Mark Hobley wrote:
> Ed Morton <morton@lsupcaemnt.com> wrote:
>
>> Get the book "Effective Awk Programming, Third Edition By Arnold Robbins".
>
> The problem that I had with that book is that it is very gawk centred,
> and sometimes it is difficult to tell which bits are awk, and which bits
> relate only to gawk. The book makes some attempt to differentiate, but I
> don't think it does this well, and maybe uses the word "awk" when
> it should really be using the word "gawk". On the plus side, it is under
> the free documentation licence, so at least it can be corrected.
If you are interested in the differences between GAWK
and AWK, have a look at the Reference Card that was
written by Arnold Robbins:
http://www.cs.mtsu.edu/~neal/awkcard.pdf
Very few people know about this card because the
official doc page at the FSF doesnt mention the card.
Differences between GAWK and AWK can easily be
recognized by the coloring of the words.
|
|
0
|
|
|
|
Reply
|
ISO
|
11/9/2008 1:43:08 PM
|
|
Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
> I think GNU awk specifics are well documented in the book.
Ok, lets look at a randomly chosen example:
Section 4.7.1 Special Files for Standard Descriptors
The proper way to write an error message in a gawk program is to use
/dev/stderr, like this:
print "Serious error detected!" > "/dev/stderr"
Right. I am not using gawk, but I do have the following device files on
my system:
/dev/stdin
/dev/stdout
/dev/stderr
So can I use that method or not? Who knows?
The book seems to imply "no" because it is a gawk extension. So will it work
or not? I have the device files. The only way to know is to try it.
I try it, and it works. So I could use that method.
This problem occurs throughout the book. You have to test all the code
to determine whether or not it works.
Sometimes, it goes the other way, and implies code works with
conventional awk implementations, but in reality it only works with
gawk.
Mark.
--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/
|
|
0
|
|
|
|
Reply
|
markhobley
|
11/9/2008 3:47:05 PM
|
|
In article <6no7ncFmh6giU1@mid.individual.net>,
J�rgen Kahrs <Juergen.KahrsDELETETHIS@vr-web.de> wrote:
>Mark Hobley wrote:
>
>> Ed Morton <morton@lsupcaemnt.com> wrote:
>>
>>> Get the book "Effective Awk Programming, Third Edition By Arnold Robbins".
>>
>> The problem that I had with that book is that it is very gawk centred,
>> and sometimes it is difficult to tell which bits are awk, and which bits
>> relate only to gawk. The book makes some attempt to differentiate, but I
>> don't think it does this well, and maybe uses the word "awk" when
>> it should really be using the word "gawk". On the plus side, it is under
>> the free documentation licence, so at least it can be corrected.
>
>If you are interested in the differences between GAWK
>and AWK, have a look at the Reference Card that was
>written by Arnold Robbins:
People (like "Mark Hobley") who post these kinds of diatribes against
extended versions of software are rarely interested in actually knowing
the differences. That's not their concern. Rather, they want those
details censored out of the reading material. It is very much like an
ascetic reading a sexy novel; out of personal preference, they have
denied themselves the benefits and so they don't want to read about them.
Note, incidentally, that Mark seems to be posting from Linux, where GAWK
is the default (what you get when you type in "awk" at the shell prompt),
so it seems a little weird to see him complaining about it.
|
|
0
|
|
|
|
Reply
|
gazelle
|
11/9/2008 5:24:00 PM
|
|
In article <anriu5-m3i.ln1@neptune.markhobley.yi.org>,
Mark Hobley <markhobley@hotpop.donottypethisbit.com> wrote:
>Ed Morton <morton@lsupcaemnt.com> wrote:
>
>> Get the book "Effective Awk Programming, Third Edition By Arnold Robbins".
>
>The problem that I had with that book is that it is very gawk centred,
>and sometimes it is difficult to tell which bits are awk, and which bits
>relate only to gawk. The book makes some attempt to differentiate, but I
>don't think it does this well, and maybe uses the word "awk" when
>it should really be using the word "gawk". On the plus side, it is under
>the free documentation licence, so at least it can be corrected.
The book is very careful to use "awk" when talking about anything in
POSIX awk and "gawk" for things that are gawk-only. If you find a case
where this is not so, please tell me so that I can fix it.
>However, at this time, I would go for a different awk book.
Your choice.
--
Aharon (Arnold) Robbins arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381
Nof Ayalon Cell Phone: +972 50 729-7545
D.N. Shimshon 99785 ISRAEL
|
|
0
|
|
|
|
Reply
|
arnold
|
11/10/2008 5:17:00 PM
|
|
On Nov 10, 11:17=A0am, arn...@skeeve.com (Aharon Robbins) wrote:
> >Ed Morton <mor...@lsupcaemnt.com> wrote:
>
> >> Get the book "Effective Awk Programming, Third Edition By Arnold Robbi=
ns".
I've been meaning to ask: now that the link I normally use
http://www.oreilly.com/catalog/awkprog3/
says the book is out of stock with no plans to reprint, is there
anywhere else we should refer people to to get a copy?
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
11/11/2008 2:52:06 AM
|
|
On Sunday 9 November 2008 16:47, Mark Hobley wrote:
> Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
>
>> I think GNU awk specifics are well documented in the book.
>
> Ok, lets look at a randomly chosen example:
>
> Section 4.7.1 Special Files for Standard Descriptors
>
> The proper way to write an error message in a gawk program is to use
> /dev/stderr, like this:
>
> print "Serious error detected!" > "/dev/stderr"
>
> Right. I am not using gawk, but I do have the following device files on
> my system:
>
> /dev/stdin
> /dev/stdout
> /dev/stderr
>
> So can I use that method or not? Who knows?
>
> The book seems to imply "no" because it is a gawk extension. So will it
> work or not? I have the device files. The only way to know is to try it.
If you read a bit more of that, you'll immediately see that the /dev/*
naming is entirely a gawk convention, which, in some cases, by coincidence
happens to follow the names in the host system's /dev.
"gawk recognizes certain special file names internally." (from the man
page).
That becomes even more apparent when you see that gawk can recognize names
as "/dev/pid", "/dev/ppid", "/dev/user", which you certainly don't have
in /dev, or even "/inet/tcp/lport/rhost/rport".
To reinforce that, a bit of common sense suggests that gawk should correctly
process those special names even on systems that do not have a /dev
directory, so they must be unrelated to their homonymous in /dev.
(Of course, on systems that do have a /dev directory, awk may directly map
the names to files in /dev if possible, but that is a matter of
implementation and is transparent to the user).
> I try it, and it works. So I could use that method.
It works because you most likely have an awk implementation that, albeit non
GNU (but are you sure of that?), recognizes and supports the /dev/* file
names, which might be a widely available feature at this time (but probably
not when the book was written). Nonetheless, those filenames are not
standard (ie, they are not specified by POSIX awk), so the book correctly
refers to them as a GNU awk feature.
|
|
0
|
|
|
|
Reply
|
pk
|
11/11/2008 12:28:08 PM
|
|
pk wrote:
>
> That becomes even more apparent when you see that gawk can recognize names
> as "/dev/pid", "/dev/ppid", "/dev/user", which you certainly don't have
Just note that those "devices" are marked as obsolete in the reference
(link posted by J�rgen Kahrs in this thread), in favour of PROCINFO[].
> in /dev, or even "/inet/tcp/lport/rhost/rport".
Janis
|
|
0
|
|
|
|
Reply
|
Janis
|
11/11/2008 12:35:40 PM
|
|
On Tuesday 11 November 2008 13:35, Janis Papanagnou wrote:
> pk wrote:
>>
>> That becomes even more apparent when you see that gawk can recognize
>> names as "/dev/pid", "/dev/ppid", "/dev/user", which you certainly don't
>> have
>
> Just note that those "devices" are marked as obsolete in the reference
> (link posted by Jürgen Kahrs in this thread), in favour of PROCINFO[].
Yes, it's also noted in the man page. I was only mentioning them as example
of filenames that have no /dev counterpart.
|
|
0
|
|
|
|
Reply
|
pk
|
11/11/2008 12:41:11 PM
|
|
In article <p8dju5-66j.ln1@neptune.markhobley.yi.org>,
Mark Hobley <markhobley@hotpop.donottypethisbit.com> wrote:
>Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
>
>> I think GNU awk specifics are well documented in the book.
>
>Ok, lets look at a randomly chosen example:
>
>Section 4.7.1 Special Files for Standard Descriptors
>
>The proper way to write an error message in a gawk program is to use
>/dev/stderr, like this:
>
> print "Serious error detected!" > "/dev/stderr"
>
>Right. I am not using gawk, but I do have the following device files on
>my system:
>
>/dev/stdin
>/dev/stdout
>/dev/stderr
The core problem here is that "Mark Hobley" needs to take a course in
mathematical logic. He is making what could be termed the fundamental
error in logic, namely assuming that:
(a => b) => (~a => ~b)
This is often true in common English usage, but it does not hold in formal
logic. Here, the symbols can be interpreted thusly:
a is the proposition that we are using GAWK
b is the proposition that we should use the /dev/* special files (to
print error messages).
>So can I use that method or not? Who knows?
Indeed. Who knows? Neither math logic nor the GAWK book make any claim
or statement about it.
>The book seems to imply "no" because it is a gawk extension. So will it work
>or not? I have the device files. The only way to know is to try it.
No, the book does not imply that. See above.
|
|
0
|
|
|
|
Reply
|
gazelle
|
11/11/2008 1:24:52 PM
|
|
On Nov 9, 9:47=A0am, markhob...@hotpop.donottypethisbit.com (Mark
Hobley) wrote:
> Janis Papanagnou <janis_papanag...@hotmail.com> wrote:
> > I think GNU awk specifics are well documented in the book.
>
> Ok, lets look at a randomly chosen example:
>
> Section 4.7.1 Special Files for Standard Descriptors
>
> The proper way to write an error message in a gawk program is to use
> /dev/stderr, like this:
>
> =A0 =A0 =A0print "Serious error detected!" > "/dev/stderr"
>
> Right. I am not using gawk, but I do have the following device files on
> my system:
>
> /dev/stdin
> /dev/stdout
> /dev/stderr
>
> So can I use that method or not? Who knows?
Whoever created the awk on your system knows. For you to find out, you
need to buy a book describing THAT awk and see if that THAT awk
supports an extension to use them like GNU awk does, or you need to
try it and see. In this particular case the OS seems to be helping out
by providing specific file names that do what you need.
The obvious point is that just because there's a GNU awk extension
doesn't mean what it describes will ONLY work with GNU awk - other
awks (or combinations of awks and OSs) might support it as well, but
you can't rely on all awks on all OSs supporting it.
> The book seems to imply "no" because it is a gawk extension. So will it w=
ork
> or not? I have the device files. The only way to know is to try it.
>
> I try it, and it works. So I could use that method.
>
> This problem occurs throughout the book. You have to test all the code
> to determine whether or not it works.
That's not a problem with the book - it can't possibly describe the
differences between GNU awk and every other awk/OS combination, all it
can do is describe the features that are available in GNU awk but may
not be available in whatever awk/OS you have available.
> Sometimes, it goes the other way, and implies code works with
> conventional awk implementations, but in reality it only works with
> gawk.
Could you post an example of that?
Ed
>
> Mark.
>
> --
> Mark Hobley
> Linux User: #370818 =A0http://markhobley.yi.org/
|
|
0
|
|
|
|
Reply
|
Ed
|
11/11/2008 1:40:38 PM
|
|
In article <b3633019-5e5c-4d57-bfc6-44172c740a27@f37g2000pri.googlegroups.com>,
Ed Morton <mortonspam@gmail.com> wrote:
>On Nov 10, 11:17�am, arn...@skeeve.com (Aharon Robbins) wrote:
>> >Ed Morton <mor...@lsupcaemnt.com> wrote:
>>
>> >> Get the book "Effective Awk Programming, Third Edition By Arnold Robbins".
>
>I've been meaning to ask: now that the link I normally use
>
> http://www.oreilly.com/catalog/awkprog3/
>
>says the book is out of stock with no plans to reprint, is there
>anywhere else we should refer people to to get a copy?
Not at the moment. You can see if Amazon still has stock. I have a few
copies of the ORA edition as well as the previous SSC editions that I
would be happy to sell, but I don't know off the top of my head how many.
I will talk to O'Reilly about perhaps making it available via some kind
of print-on-demand system, or else going with Lulu or something.
In the meantime, with the gawk distribution in hand and if you have a
reasonable TeX distribution, you should be able to do
cd doc
make pdf
and create a printable copy of the book. It won't be as nicely formatted
as the ORA edition but otherwise it'll do the trick.
And of course, it can be read on-line from the GNU web site.
Arnold
--
Aharon (Arnold) Robbins arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381
Nof Ayalon Cell Phone: +972 50 729-7545
D.N. Shimshon 99785 ISRAEL
|
|
0
|
|
|
|
Reply
|
arnold
|
11/11/2008 7:32:26 PM
|
|
|
15 Replies
186 Views
(page loaded in 0.167 seconds)
Similiar Articles: Remove duplicate fields in one line - comp.lang.awkHello all, in short I have this input like (much-much longer, it's just a little part for sake of simplicity): U112233=AAA=U112233=AAB=U123456=AAB=U... single line command to remove a nonempty folder - comp.unix ...I want to remove folder(including subfolders and files) with a single command without to type yes/no for each individual file deletion. what can I do?... Remove last lines - comp.unix.shellHelp Me Remove/Delete Thousands of Emails AT ONCE - comp.mail.misc ... delete one line... - comp.lang.awk 3) Remove the 4the blank line. 4) Insert two blank ... Email addresses want to delete from the Address Line - comp.mail ...Help Me Remove/Delete Thousands of Emails AT ONCE - comp.mail.misc ... delete one line... - comp.lang.awk 3) Remove the 4the blank line. 4) Insert two blank ... How to delete zero rows from a matrix - comp.soft-sys.matlab ...It may seem very simple. But, I, actually, am having trouble trying to figure out how I can delete zero rows with a single line of command. Does anybo... @ symbol deleting all preceeding input at command line? - comp.sys ...Removing duplicated lines - comp.lang.awk Removing duplicated lines - comp.lang.awk delete one line... - comp.lang.awk Removing duplicated lines - comp.lang.awk @ symbol ... sed, remove last new line - comp.lang.awk] No one has still told me a way to remove last new-line character. To remove the last newline character in a file, just slurp up the entire file into memory and remove ... awk: bailing out near line 1 - comp.lang.awk-- T.E.D. (tdavis@gearbox.maem.umr.edu) Remove "gearbox.maem ... comp.lang.awk Problem with awk / awk: bailing out near line 1 - comp.unix.shell ..... lang.fortran... one ... How can I remove one row from a matrix ?? - comp.soft-sys.matlab ...It may seem very simple. But, I, actually, am having trouble trying to figure out how I can delete zero rows with a single line of command. Does anybo... Difference beetwen awk and gawk? - comp.lang.awkDifference beetwen awk and gawk? - comp.lang.awk delete one line... - comp.lang.awk official doc page at the FSF doesnt mention the card. Differences between GAWK and ... Removing trailing blank lines - comp.lang.awkThis will remove trailing blank space from every line. How about this (not entirely awk): tac | awk ... Seriously folks, the code is, in fact, just one line ... Removing duplicates from within sections of a file - comp.lang.awk ...... line in spool file - comp.mail.misc Help Me Remove/Delete Thousands of Emails AT ONCE - comp.mail.misc ... Removing duplicates from within sections of a file ... one line ... sed: how to remove all duplicated white lines? - comp.unix.shell ...... question - comp.lang.awk How does one delete duplicate adjacent ... of any characters ... the final text now...(made it all one sed line and also set it to remove ... Removing duplicated lines - comp.lang.awkEmail addresses want to delete from the Address Line - comp.mail ... Removing duplicated lines ... comp.cad.pro-engineer What's the limit for being F enough to be ... one line ... Help: Delete a single carriage return in a file, but not a double ...How to Remove a Carriage Return With SED | eHow.com To remove a carriage return (or new line ... by typing a single quote mark (') and type the name of the file to ... Sed - Delete one or more lines from a fileUnix utility SED provides an effective and a versatile way of deleting one or more lines from a designated file to match the needs of the user. This Unix command is ... How do I remove one line from a file? - Welcome to the Tclers Wiki!How do you delete one line from a file? What kind of file are you talking about? There aren't, in general, lines in MP3 files, or GIF files, etc. 7/9/2012 6:43:25 PM
|