|
|
|
|
|
|
Subject: Re: referencing certain fields with egrep
Subject: Re: ? about grep
Subject: Re: awk delimiter
Subject: Re: AWK Question
Subject: Re: Awk?
Subject: Re: Awk?
Subject: Re: adding a line to the top of a file
Subject: Re: any way to overwrite files?
Date: Mon, 4 Mar 1996 03:04:31 -0600 (CST)
To: dufresne@darkstar.sysinfo.com
Newsgroups: comp.unix.questions
Newsgroups: comp.unix.questions
From: bmarcum@iglou.iglou.com (Bill Marcum)
Subject: Re: referencing certain fields with egrep
Date: Mon, 4 Mar 1996 02:00:38 GMT
Lines: 20
In article <4hcqbt$aod@gondor.sdsu.edu>,
Jason Larson <larsonj@rohan.sdsu.edu> wrote:
>I need to know how to refrence a certain field using egrep.
>I have a file with a certain number of words sparated by commas and
>spaces. ex:
>asfas, fas sdh, iemnc, feb28, ddhus, fddd
>
>How can I refer to say the second or third field *only*
>I can do this with sort easily but I need grep to weed out what
>I am not looking for.
>I have tried specifying the pattern with ', pattern' but then it
>is also refering to other other fields.
>Any help is greatly appreciated.
>
You asked how to do it with egrep, but with awk you can simply write
awk -F, '$2~/pattern/ {print}'
--
Bill Marcum bmarcum@iglou.com
"They're all lawyers, and think that the laws of physics can be amended with
a voice vote." -- Mary Shafer (shafer@ursa-major.spdcc.com) on Politicians
--
Date: Mon, 4 Mar 1996 19:53:16 -0600 (CST)
To: dufresne@darkstar.sysinfo.com
Newsgroups: comp.unix.questions
Newsgroups: comp.unix.questions
From: bmarcum@iglou2.iglou.com (Bill Marcum)
Subject: Re: ? about grep
Date: Mon, 4 Mar 1996 19:18:07 GMT
Lines: 14
In article <4hfbkd$3t7g@holly.ACNS.ColoState.EDU>,
Stuart Mcfarland <muggs@holly.ACNS.ColoState.EDU> wrote:
>can I specify the number of times a string should appear and have grep
return false
>if the string appears more often, or make grep indicate how often a
string occurs? >...or anything along those lines...thx
You could do it with awk
awk '/pattern/{counter++;if(counter>=5) exit 1}' filename
--
Bill Marcum bmarcum@iglou.com
"They're all lawyers, and think that the laws of physics can be amended with
a voice vote." -- Mary Shafer (shafer@ursa-major.spdcc.com) on Politicians
--
|
|
|
|
|
|
From: demas@sunspot.tiac.net (Charles Demas)
Newsgroups: comp.unix.questions
Subject: Re: awk delimiter
Date: 11 Mar 1999 18:02:18 GMT
Lines: 26
To: <GellerL@aetna.com>
In article <7c8u2v$b34$1@nnrp1.dejanews.com>,
<GellerL@aetna.com> wrote:
>Can I change the default delimiter of awk from a space to something else like
>a comma for example? What I am trying to do is the following:
>
>If the fourth field = "INA" then print the record.
RTFM, RTFM :-)
man awk
awk -F, '$4=="INA" {print}' infile
though this is shorter:
awk -F, '$4=="INA"' infile
Chuck Demas
Needham, Mass.
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@tiac.net | \___/ | http://www.tiac.net/users/demas
--
From: demas@sunspot.tiac.net (Charles Demas)
Newsgroups: comp.unix.questions
Subject: Re: AWK Question
Date: 20 Mar 1999 19:29:29 GMT
Lines: 37
To: Ralf Hefsteffenssen <kushtaka@bigfoot.com>
In article <921937117.15798.1.nnrp-12.c2defe8d@news.demon.co.uk>,
Ralf Hefsteffenssen <kushtaka@bigfoot.com> wrote:
>I have a file containing four colums of numbers eg.
>
>34 87 75 85
>97 67 85 75
>03 76 97 54
>98 75 95 59
>
>I would like to know how I can add the average of each row to the end of
>that row.
>
>I have some ideas using AWK, but I think I may need to use it with SED to
>process the file one line at a time?
no need for sed, awk can do it just fine:
awk '{print $0, $1+$2+$3+$4}' infile
which produces this from your data:
34 87 75 85 281
97 67 85 75 324
03 76 97 54 230
98 75 95 59 327
FWIW, this question would have been better asked in comp.lang.awk
Chuck Demas
Needham, Mass.
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@tiac.net | \___/ | http://www.tiac.net/users/demas
--
From: "Rev. Don Kool" <oldno7@home.com>
Newsgroups: comp.unix.admin
Subject: Re: Awk?
Lines: 26
Date: Wed, 10 Mar 1999 23:55:54 GMT
Emmanuel Dreyfus wrote:
>
> Mike Wilson <wmwilson1@go.com> wrote:
>
> > cat /etc/passwd | awk -F: '{print $1}' > /your/file
>
> I would have done it without the -F:
> What is it for?
That is the field descriptor (the character that deliminates the
various fields of the record). In the case of the password file,
the fields are deliminated by ":" so it is necessary to tell "awk"
that.
Hope this helps,
Don
--
********************** You a bounty hunter?
* Rev. Don McDonald * Man's gotta earn a living.
* Baltimore, MD * Dying ain't much of a living, boy.
********************** "Outlaw Josey Wales"
http://members.home.net/oldno7
--
From: nick@grendel.nmti.com (Nick Manka)
Newsgroups: comp.unix.admin
Subject: Re: Awk?
Date: 11 Mar 1999 20:48:52 GMT
Lines: 29
<1dogw2p.1a4lgfa1feuku0N@trotsky.hcpnet.net>
Reply-To: nick@baileynm.com
Mime-Version: 1.0
In article <1dogw2p.1a4lgfa1feuku0N@trotsky.hcpnet.net>,
p99dreyf@criens.u-psud.fr (Emmanuel Dreyfus) writes:
> Mike Wilson <wmwilson1@go.com> wrote:
>
>> cat /etc/passwd | awk -F: '{print $1}' > /your/file
> I would have done it without the -F:
> What is it for?
It specifies the field separator, sometimes called a delimiter. You
could also do
awk ' { FS=":" ; print $foo } '
with the same result.
Awk works by breaking up lines of input into fields to operate on, the
default separator is whitespace.
For instance, if you had a input consisting of pathnames and you
wanted to extract the last element, you could do:
awk ' { FS="/" ; print $NF } ' < myinput > myoutput
Awk is neat.
--
Proprietary software is modern alchemy.
--
From: damercer@mmm.com (Dan Mercer)
Newsgroups: comp.unix.questions
Subject: Re: adding a line to the top of a file
Date: 18 Apr 1999 16:52:21 GMT
Lines: 56
<3717604A.C4E7DDAF@lsl.co.uk> <7fbib9$aa9@news-central.tiac.net>
Mime-Version: 1.0
In article <7fbib9$aa9@news-central.tiac.net>,
demas@sunspot.tiac.net (Charles Demas) writes:
> In article <3717604A.C4E7DDAF@lsl.co.uk>,
> Nick Wagg <nickw@lsl.co.uk> wrote:
>>Tom Kralidis wrote:
>>>
>>> Hi,
>>>
>>> I am aware that one can add a line to the end of a file with the
>>> following:
>>>
>>> % echo ls -l >> new.file
>>>
>>> How can one invoke this to put this line at the top of the file (ie the
>>> first line)
>>
>>You can't literally insert. You have to write your line to a new file,
>>concatenate your old file to create a new file. Then rename:
>>
>>echo ls -l > new.file
>>cat old.file >> new.file
>>mv new.file old.file
>
>
> What you say is not quite correct, editors allow you to do this
> without using a temporary file.
>
> One can write on the command line a command sequence to do what's
> required using ex or vi.
>
>
>
> Chuck Demas
> Needham, Mass.
using awk to store the contents of standard in also works:
{ ls -l ; cat file; } |
awk '{X[++ct]=$0}END {for (i=1;i<=ct;i++) print X[i] >file' file=file
And, for files less than PIPBUF size, and using ksh
the following will usually work:
{ ls -l; cat file; } | (:&&(cat >file))
--
Dan Mercer
damercer@uswest.net
>
> --
> Eat Healthy | _ _ | Nothing would be done at all,
> Stay Fit | @ @ | If a man waited to do it so well,
> Die Anyway | v | That no one could find fault with it.
> demas@tiac.net | \___/ | http://www.tiac.net/users/demas
Opinions expressed herein are my own and may not represent those of my employer.
--
From: demas@sunspot.tiac.net (Charles Demas)
Newsgroups: comp.unix.questions
Subject: Re: any way to overwrite files?
Date: 7 May 1999 00:13:04 GMT
Lines: 46
In article <gSnY2.480$jw4.34640@burlma1-snr2>,
Barry Margolin <barmar@bbnplanet.com> wrote:
>In article <373206FB.E76CBC29@ccrs.nrcanDOTgc.ca>,
>Tom Kralidis <tom.kralidis@ccrs.nrcanDOTgc.ca> wrote:
>>Is there a way to write to files without renaming them, and deleting the
>>old ones?
>
>No.
I believe you're incorrect. There are editors that would do this
from the command line (vi, ex, or ed IIRC) and gawk is also capable
of it. e.g.:
gawk '{sub(/something/,"somethingelse");a[NR]=$0}
END{for(i=1;i<=NR;i++){print a[i] > "infile"}}' infile
I am NOT saying that this is a good idea!!!
>
>>sed 's/something/somethingElse/' old.file > new.file
>>
>>I would like to do this so that I can update the file in one step,
>>without having to write to a new file.
>>
>>Any ideas would be valued.
>
>If you just want avoid have to write the mv command, you can use Perl with
>the -i option.
>
>perl -p -i.bak -e 's/something/somethingelse/' file
>
>This will rename file to file.bak and then write the results to a new file.
This is a better and much safer approach.
Chuck Demas
Needham, Mass.
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@tiac.net | \___/ | http://www.tiac.net/users/demas
--
|
|
|
|
|
|

| Hosted by: |
|