How's it coming?
Here is a way to change the "5th" character to "X"....Quote:>I'm trying to see how to use nawk to change the 5th character of
>a string from what it is to 'X' (for example).
nawk '{ORS = ""; a = $0; b = substr(a, 1, 4); c = (length(a) - 5); d = substr(a
, 6, c); print b; print "X"; print d; print "\n"}' FILENAME
[note the continuation from line 1 to line 2 of program]
Hope it helps....
Nasir Ahmed Noor
p.s. if you are processing multiple records in the same program, make sure
to set ORS back to "\n" at the end of command, i.e., after printing "\n".
Much faster and easier to use sed for such basic string operations, e.g:
sed 's/^\(....\)./\1X/' file1 file2 ...
Reads from named files, or if no files specified, from stdin. Lines with
less than 5 characters are untouched, all others have the 5th character
changed to 'X'.
--
Conrad Kimball | Deliv. Sys. Tech Support, Boeing Computer Services
(206) 865-6410 | Seattle, WA 98124-0346
1. two simple nawk questions, Sunos 4.1.2 and nawk
I am trying to nawk through a file and change in the 20th field the value
of the date from mo/da/year, as in 02/30/1993, to mo/ld/year, as in
02/28/1993( where da is any particular day, even wrong ones, and ld is the
correct last day of the month), and the two problems I can't solve are,
that I can't get the shell wrapper to work like I would like without having
to specify a file on the command line, and I am having trouble finding a
way to print out the line when I am done, using ranges, like $1-$19.
I would like to do:
1)
#! /bin/nawk -f # Call the nawk script without any parameters
BEGIN { FS = "|";
OFS = "|" }
{ while ( getline <"Data.file" > 0 )
...rest of script...
How can I run this program without having to use:
mysys: ~ $ datefix Data.file
In otherwords, just by typing the script name( without Data.file).
2)
How do I specify a range for a print statement:
print $1-$19,date,$21-$37
Right now it seems I have to have a line like:
print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14...goes on and on till
I wanna scream!
I am sorry if these are obvious questions or FAQ's, but nothing about them
appears in the O'Rielly book, or anywhere else. I guess the creaters of
nawk expect you to write filters like:
mysys: ~ $ datefix -F\| -f20 < Data.file > Data.file.fixed
but I want to do it my way.
--
{...}!cs.utexas!cactus.org!cheselka (512)452-9412v ( 24hours answer. mach.)
3. consistent read() on file while rename() file.new -> file?
4. Plextor 3024 with FD TMC950
5. Substituting :: for __ in every htm file, and rename htlml file
7. rename files to include each file's timestamp
8. diff b/w red hat linux OS 7.1 and Sun SOlaris OS 5.7
9. renaming 500+ files based on contents of an existing text file
10. rename() doesn't return -1 when it does not really rename
11. Nawk: can I use more than one input file in one script?
12. : nawk how to redirect to the file
13. Q(nawk?): searching records in 2 files