Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
awk and substr
Name: yacob Date: March 14, 2008 at 13:42:22 Pacific OS: Linux CPU/Ram: 2Gb
Comment:
Dear all
I have an awk script as below but it just prints only to the last file when I wanted t o print all which satisfies the the "if" conditions. I do not know why but it it only print the last file in the out put file when there are many in my files that satisfy the if condition. Can u help ? See below
#!/bin/sh for i in GX* ; do echo $i | awk '{ if ( substr($1, 28, 3) > 100 && substr($1, 36, 3) < 300 ) print "jun_"substr($1, 24, 4)}' > out done
The output "out" only contain the last file that satisfy the condition but it should contain all the file that satisfy the if condition, and I know there are a lot of them. Why is that ? How ever if I directed to the screen all files that satisfy get printed in the screen.
Name: yacob Date: March 14, 2008 at 13:53:48 Pacific
Reply:
Dear All
I have just got the answer, sorry I should have put >>
0
Response Number 2
Name: ernie Date: March 15, 2008 at 06:35:26 Pacific
Reply:
That is the answer.
> redirects a command's output to the named file. If the named file already exists, it is overwritten. If the named file does not exist, it is created.
>> also redirects a command's output to the named file. If the named file exists, output is appended to the file. If the named file does not exist, it is created.
This explanation is intended for users who may be unfamiliar with command redirection.
Summary: MrJake, The part echo $var | awk '{ print substr($1,0,1) }' says to take just the first character of $var The next part | tr 'YN' 'yn' says to translate a 'Y' to 'y' and 'N' to 'n' although I like ...
Summary: you could use emacs, vi, possibly joe, or others (ed is "line-oriented"), but if this is to involve doing the same thing to many files, look into sed, awk, and even bash or perl scripting ...
Summary: ordinarily you would just do something like this: a = system("ls * 2&1 > /dev/null"); if (a != 0) { printf "%s", "Bogus run::program failed"; } else { EXIT_SUCCESS; } YMMV, I tried it with awk ...