Below is an excerpt of the file that I want to change. This is x.txt, the original file that I want to run this on is about 1000+ lines. Basically, these files are from several log files merged into one and I am wanting to change the USER=<username> field so that <username> is in lower case. I am working on the assumption that USERNAME=<username> is always field2.
but for 'clarity' I prefer to only change USER=<username> to USER=<lowercase_username> and leave the rest of the line as it is. I can't work out the awk or sed command options to use to achieve what I wanted, hence I ended up with a shell script instead. Maybe there is an awk one-liner that can do what I am trying to achieve :o
Thanks the one liner works like a charm. I won't be able to figure out for ages that this will do what I want :o It doesn't even requires any intermediate files.
Yes, $2 ( USER=MICKEY ) is split at the equals sign into array T , and then rebuilt with the lower case username in T[2] .
The !a[$0]++ is a trick (independent of $2 ): a[$0] evaluates to FALSE if it equals zero or doesn't exist or is created on first reference, so its negation is TRUE and triggers the default action: print . Then it is "post incremented" and will never (OK, not until reaching / crossing MAX_INT) trigger again. So any further occurrences of $0 are suppressed.
A more detailed explanation follows.
The main awk code runs for each input line. !a[$0]++ is ultra-condensed, quick and dirty.
A bit more explicit is !($0 in A) { A[$0]; print } :
If not $0 in array A (A[$0] not defined) then define A[$0] (no A[$0]=value needed here) and print $0.
The array A is associative (string-addressed). So if the same $0 will occur in another input line it will see a defined A[$0] and won't print.
If there is a pre-condition and no { action code } following then the default for a true condition is { print } , and print without arguments defaults to print $0 .
Now to the quick and dirty !A[$0]++ :
Define A[$0] with value 0 if undefined, if the negated value is non-zero (true) then default-print. Also post-increment A[$0].
If the same $0 will occur then the A[$0] value will be 1, negated 0 (false), won't print, but post-incremented.
If the same $0 will occur then the A[$0] value will be 2, negated 0 (false), won't print, but post-incremented.
...