Here is a very slightly more complex awk script that should be much more efficient than what you were doing.
I used ksh, but this script will work with any shell that recognizes basic Bourne shell syntax. If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of just awk. (This is also true if you use the awk scripts suggested by rveri.)
The first line in the awk script above sets the input and output field separators to " | ". The second line replaces your grep. And the third and fourth lines replace your if statements.
Note that your sample input only had 20 fields, but you're printing the contents of fields up to field #27. That is why there are empty fields at the end of the output lines this script produces.
Note also that your sample input had lines that were about 1150 bytes long. If your real input has lines that are longer than LINE_MAX on your system (you can get the limit on your system using the command getconf LINE_MAX), the behavior of awk and grep is unspecified. The standards specify LINE_MAX must be at least 2048; and 2048 is the limit on many systems. If you can't use awk and grep to get reliable results on your system, the solution to your problem will be much more complex.
You didn't show us what output you wanted, so rveri and I made similar guesses at what you wanted. The difference between rveri's scripts and mine is that I assume your field separator (for input and output) was <space><vertical bar><space> where rveri removed the <vertical bar> in the first suggestion and added more spaces in the output with the suggested edit.
With the sample input given in the 1st message in this thread, the output my suggestion above produces is:
The original code posted by rveri produces:
Note the leading spaces and the lack of any indication at the end of the output lines corresponding to the empty output fields.
With the suggested edit to rveri's code, it produces:
Note the leading spaces and the added spaces separating existing input fields that are copied to the output. This presents the empty fields at the end of the line the same way my suggestion does it.
Maybe one of these three will be close to what you want.
Thanks Don, honestly, I can not appreciate the improvement in code given my limitation in understanding it.
The code given by rveri which produces space separated values (or rather just the tokens with out pipe) does suffice. Thanks again for your help!
---------- Post updated at 05:04 PM ---------- Previous update was at 11:22 AM ----------
The servers are cloned from a single image, however on some of the servers, I am gettign this:
What could be the reason. Shell is ksh. Google search did not return any satisfactory responses.
The code is same across all servers. Also the servers are cloned from a single image. It is indeed surprising to see the in-consistent behavior. Any hint will be helpful. Thanks.
Hey all,
so I'm using AWK in a project at work, to generate xml from csv. So far things are going relatively smoothly, but I have one thing I can't figure out.
For every field in each row, I must generate <entry name=KWNamex>Field</entry>
Then I will need to pull data from a second file... (6 Replies)
Hi,
I have a file such that:
40454,31,48,4,1304.967741935484,1
31708,25,48,4,1268.32,1
20900,64501,671,788,0.3240259840932699,0
20137,51358,834,743,0.3920908135051988,0
I want to replace the 6th column by "ones" if it is 1, and with "zeros" if it is 0.
Thanks. (6 Replies)
Hi,
I'm new to AWK and I'm having problems comparing a field to a string variable.
/ARTIST/ {x = $2}
$1 ~ x {print $0}My code tries to find a record with the string "ARTIST". Once it finds it, it stores the second field of the record into a variable. I don't know what the problem is for the... (7 Replies)
Hello;
I need to print two previous lines after searching for a reg exp:
awk '/haywood/'
should produce the following
===================
p9J46THe020804 89922 Tue Oct 18 21:06 MAILER-DAEMON
(host map: lookup (haywood.com): deferred)
... (1 Reply)
Hello;
Trying to figure out how to keep just the contents between the two search lines:
awk '/regexp_1/ ,/regexp_2/'
I do not want lines containing regexp_1 and regexp_2 in the output.
Thank you for any ideas
Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
awk '!_++'
Most importantly, I want to know what the underscore does "!_"
But ideally, please breakdown the whole thing. It is supposed to remove duplicate lines when found in a file. (1 Reply)
Hi,
I just don't understand awk. I think I'm close here but can someone give me a hand to finish this little parsing routine ?
the input file is formatted like this:
District 2502110
Gsub 2384889
Gsub 1428180
District 2502220
Gsub 1466390
Gsub 1466389
Gsub 1466388
Gsub 1466386
Gsub... (4 Replies)
Whats the syntax to find all lines that matches a text and print out specific fields after the match?
ex:
1: some random text
2: Full name: John E. Smith
3: some random text
4: Full name: Mary J. Lue
5: some random text
So I'd like to print out First names or last names or everything... (2 Replies)
when I execute this awk stmt ..
awk "/log_directory/ { print $5}" /opt/dba/oraadmin/tools/tmp_purge_op.log
it's returning the whole line as ..
IRMD118_LISTENER1 parameter "log_directory" set to /opt/oracle/10.2/network/log/
my expected output is : /opt/oracle/10.2/network/log
what... (7 Replies)
I need a hand with this simple script,
in Unix i have a variable called portal:
$ echo $portal
chu0
when i use awk this variable is not recognized. How can i make awk recognize and print the value of this variable, since the output is not shown i.e.
awk '
BEGIN {printf("%4s\t%14s\n",... (3 Replies)