Yes, you are right, awk is the right tool for the job, and I have been fooling
around with it today. I'm about 95% there, but I have one quirky result. I was
wondering if anyone might be able to help clear up this AWKward problem.
I essentially did what you suggest Greg only I created a seperate awk script
file, so at the command line I have
awk -f shift.awk infile > outfile
the contents of shift.awk is essentially identical to your command line.
The script works beautifully except for one thing. If I let the output go to
the screen (stdout) or to the outfile I find each line of the input file is
echoed to the output file, and then the printed line form the awk script
follows. So when the first field is not -1 or 0 the output is two identical
line (one right after the other). If the first field is -1 or 0 the output is
the line straight from the input file followed by the properly shifted line
(created by awk).
What gives? I've done this kind of redirection before
(e.g. sed -e 's/XX/YY/g' < infile > outfile)
and it works fine so I'm not sure what is happening. Is there a defualt
echoing in awk that I need to turn off some how?
Greg Olszewski <[EMAIL PROTECTED]> wrote:
> On Thu, Apr 17, 2036 at 03:32:47AM -0500, Robert Haehnel wrote:
> > I have a tab delimited text file that has 6 field per record.
Unfortunately
> > the data aquistion program that created this file hiccupped in a few
places
> > and every so often moved the first to the last position (left shifting
the
> > whole record). If the first field has been moved I know it because now
the
> > first field in the record is either 0 or -1.
> >
> > So what I'ld like to do is write a quick and dirty script that rips down
thru
> > the file (record by record), and if it finds a 0 or -1 in the first field
it
> > yanks the last field back to the beginning where it belongs and right
shifts
> > all of the other fields in the record. The resulting recodr is still tab
> > delimited of course.
> >
> > I have been using sed, but perl or other languages are fine with me. If I
have
> > to I can write a c++ prog, but I like the brievity of scripts using sed
and
> > perl for doing various search and replace functions on text files.
> I think awk is more targeted for this. I'll have to think
> about how to do it in sed...
>
> I don't know how to make an awk script yet, but here's the
> command line that'll do it (I think)(seems to work here).
>
> awk 'BEGIN{FS=OFS="\t";}{if ($1 == "0"||$1 == "-1"){print
$6,$1,$2,$3,$4,$5;}else{print;}}' < file
>
> have fun
>
> greg
> (I'll think about sed soon)
> --
> dronf!
--
R. Haehnel
____________________________________________________________________
Get your own FREE, personal Netscape WebMail account today at
http://webmail.netscape.com.
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [EMAIL PROTECTED]
Please read the FAQ at http://www.linux-learn.org/faqs