"It seemed like a good idea at the time."
--Brian Kernighan
Nothing beats good old fashion awk when it comes to problem solving :-). Here is one such example:
[ircuser] is there any command or quick way to do like,say i have two strings, "tes**ng" and "lkmtide" . need to replace the '*' in the first string with the exact characters in the second string. so, "tes**ng" changes to "testing"
[ircuser] cmihai, here the issue is * is not fixed to two chars... it can be from one to len-2
# Author: Criveti Mihai, 2007.
# This program is free software.
# It comes without any warranty, to the extent permitted by applicable law.
# You can redistribute it and/or modify it under the terms of the
# Do What The Fuck You Want To Public License, Version 2
# as published by Sam Hocevar.
# See http://sam.zoy.org/wtfpl/COPYING for more details.
# Usage: cat file | awk -f change_stars_and_stuff.awk > output
BEGIN {
FS = " ";
}
{
string1 = $1;
string2 = $2;
do {
position = index( string1,"*");
finalstring = substr(string2,position,1);
sub(/\*/,finalstring,string1);
} while (position != 0)
}
{
print string1;
}
The input file would look something like this (from what I gather anyway):
cmihai@alap ~/scripts
$ cat file
tes**ng lkmtide
tes***g lkmtinaaa
testing lkmtide
abcdef* abcdefghijklmn
And now, let's take our little script for a test drive:
$ cat file | awk -f change_stars_and_stuff.awk
testing
testing
testing
abcdefg
Well, that did it :-). Good old fashion AWK to the rescue!
1 comments:
Thanks a Lotssss buddy. This works great.
Post a Comment