Re: Search & replace, but increment with every replace
- From: Tonagon <aroberts@xxxxxxxxxx>
- Date: Thu, 29 Nov 2007 12:18:06 -0800 (PST)
On Nov 29, 2:51 pm, Tonagon <arobe...@xxxxxxxxxx> wrote:
On Nov 29, 8:17 am, Ed Morton <mor...@xxxxxxxxxxxxxx> wrote:
On 11/28/2007 1:13 PM, Michael Tosch wrote:
Ed Morton wrote:
On 11/28/2007 11:39 AM, Ed Morton wrote:
On 11/28/2007 10:47 AM, Tonagon wrote:
On Nov 27, 5:35 pm, Bill Marcum <marcumb...@xxxxxxxxxxxxx> wrote:
On 2007-11-27, Tonagon <arobe...@xxxxxxxxxx> wrote:
awk 'BEGIN{c=1000000}{sub($1,++c)}'- Hide quoted text -
- Show quoted text -
Thanks Bill, you rock!
I gave it a whirl though and have not had any luck yet.
I simplified my script to contain just this line now:
awk 'BEGIN{c=1000000}{sub($1,++c)}' $1 > $1.new
So it is run with an arument (the file name to be parsed by awk) and
then redirects the output to the same filename with a .new extension.
Sadly it just creates a zero byte file.
Right, you didn't tell it to output anything.
However, something is missing, as just running the awk command against
a file
awk 'BEGIN{c=1000000}{sub($1,++c)}' master.out
Gives me no results.
I tried throwing a print in there, but no luck.
Really? That's surprising.
I am sure this is something simple that I am missing, but after five
years I have gotten extremely rusty with these things!
What is missing?
Try this:
awk 'BEGIN{c=1000000}{sub($1,++c); print}' "$1" > "$1".new
Note, however, that that's dangerous if $1 contains an RE wildcard, e.g.:
$ echo "a b c" | awk 'sub($1,9)'
9 b c
$ echo ".* b c" | awk 'sub($1,9)'
9
If you don't care about preserving the spacing between fields you could do this
instead:
awk 'BEGIN{c=1000000}{$1=++c; print}' "$1" > "$1".new
If you do care about the field separators and an RE wildcard in $1 is a
possibility, you need a more complex solution...
Ed.
"complex" like this:
awk 'BEGIN{c=1000000} $1~/^[0-9]*$/ {sub($1, ++c); print}'
So there can be non-numeric $1s but we're only supposed to convert $1 if it's an
integer? That requirement, if it exists, has been snipped somewhere along the
line and I don't see it in the few earlier posts in this thread that I looked
at, but then I don't see how we got to just operating on $1 either, so I'm
probably just not looking hard enough (and not inclined to look any harder!).
I'll just leave it to the OP to put us back on track if we've wandered off track...
Ed.
Or use a sub-string expression; this works on old awk's, too:
awk 'BEGIN{c=1000000} {print ++c substr($0, length($1)+1)}'- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Thanks Ed and Michael.
It turns out that when I tried to add the print I did not put in a
semicolon, so it did not work.
Either you know the syntax or not, and obviously I do not.
It turns out that my file is comma delimited, which I think is a
problem for the standard awk statements I have used in the past.
Field one of the file is strictly integers however, and they are
already in numeric order - with some number having been skipped.
The file starts with record 4000 and ends at 8500, but there are only
60 lines in the actual file.
I can pull it out again using tabs instead of commas and that may
help.
I will then play with your suggestions again and see how it goes.- Hide quoted text -
- Show quoted text -
Got it!
This works:
awk 'BEGIN{c=8001}{sub($1,++c);print}' "$1" > "$1".new
I just had to get the commas out of there. I did and now it looks
like we are golden. I will have to import the file back into the
database to be sure, but it looks good to me.
Now it is just a quick change to my output scripts so they stop
getting the data out in a comma delimited format, then maybe let it
take an argument for the beginning number range, and I can write up
instructions and distribute it to the folks who need it.
This will save some folks many hours of work and they will be
thrilled. Previously they have been manually retyping the numbers to
be the new ranges that they want.
Ed Morton is a cool arse dude. I don't care what his ex says about
him.
.
- References:
- Re: Search & replace, but increment with every replace
- From: Tonagon
- Re: Search & replace, but increment with every replace
- From: Tonagon
- Re: Search & replace, but increment with every replace
- From: Bill Marcum
- Re: Search & replace, but increment with every replace
- From: Tonagon
- Re: Search & replace, but increment with every replace
- From: Ed Morton
- Re: Search & replace, but increment with every replace
- From: Ed Morton
- Re: Search & replace, but increment with every replace
- From: Michael Tosch
- Re: Search & replace, but increment with every replace
- From: Ed Morton
- Re: Search & replace, but increment with every replace
- From: Tonagon
- Re: Search & replace, but increment with every replace
- Prev by Date: Removing numbers
- Next by Date: Re: Removing numbers
- Previous by thread: Re: Search & replace, but increment with every replace
- Next by thread: Re: Search & replace, but increment with every replace
- Index(es):
Relevant Pages
|
Loading