Solved

awk string parsing

Posted on 2001-08-09
4
279 Views
Last Modified: 2010-04-21
I have an output that looks like this,

         1  2  3  4
5  6  7  8  9 10 11

Now I need to print every string ("1"..."11") plus some extra information. I tried a script like

BEGIN {FS = " "}
{
for (i = 1; i <= NF; i++)
{
   if ($i == myvar)
       printf("%s %s", $i, mystr)
   else
       printf("%s ", $i)
   if (i == NF)
       printf("\n")
}
}

This parses the first string("1") as appearing in column one, but it should appear in column 4, i.e the spaces in columns must be printed.

0
Comment
Question by:eing9607
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6371825
do you mean the leading space left of 1 ?
And how about all the other leading spaces?
Then awk is probably not the rigth choice for that, but can be done too.
Could you please give an example (not phrases) how the output shoul look like.

Also, what does you program do ? the awk-variables myvar and mystr never get set, so they are always the empty string or 0 (depending on the context).

0
 

Author Comment

by:eing9607
ID: 6371928
I haven't included the setting of myvar and mystr here, but I do set them in my awk script.

Well I kind of solved the problem myself. I use the split function, n = ($0, arr, " "), function to retrieve the number of elements on each line. Then I can print each entry stored in arr[1]..arr[n]. By doing so I can (pre)format the output the way I want.

Sorry, when I asked the question I wasn't aware of the split function.    
0
 
LVL 21

Expert Comment

by:tfewster
ID: 7774718
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation for this question in the Cleanup topic area as follows:
- PAQ & refund points

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster (I don't work here, I'm just an Expert :-)
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7864314
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question