an awk script for...

an awk script for...

Post by br.. » Wed, 13 Oct 1993 00:48:29



I need 2 awk scripts.  The 1st must change the last "/" in 8th field of
a line to a space and print the line.

The 2nd must change the space between the 8th and 9th fields to "/"
and print the line.

Can someone advise?

(The goal here is to separate the filename from the pathname in ls -l
output so we can sort by the pathname and then rejoin them.)

Liam Breck
Info-Mac Archive Moderator

 
 
 

an awk script for...

Post by Barbara Vaugh » Wed, 13 Oct 1993 05:47:59


|> I need 2 awk scripts.  The 1st must change the last "/" in 8th field of
|> a line to a space and print the line.
|>
|> The 2nd must change the space between the 8th and 9th fields to "/"
|> and print the line.
|>
|> Can someone advise?
|>
|> (The goal here is to separate the filename from the pathname in ls -l
|> output so we can sort by the pathname and then rejoin them.)
|>

TO ALL NET KIBBITZERS:

Please don't ask me why I suggested an awk solution.  If a person
requests an awk solution, I try to assume they have a reason for
doing so.

I have an awk function called findlast that will help in this task:

function findlast(string,char){
# find the last occurrence of char in string
 iplast=0
 rest=string
 left = ""
 ip = index(rest,char)
 while (ip > 0){
   left = left substr(rest,1,ip)
   rest = substr(rest,ip+1)
   iplast = length(left)
   ip = index(rest,char)
   }
 return iplast

Quote:}

You would put this function before the BEGIN section of your awk
script.  

I think you should rephrase the task for the first script to 'find
the last "/" in the input line', rather than 'in the 8th field', because
if you break the line into fields, you lose the spacing in the line.

I suggest making script one:

[put function findlast here]
{ lastsl = findlast($0,"/")
  if (lastsl > 0){
    begin = substr($0,1,lastsl-1)
    end = substr($0,lastsl+1,length($0)-lastl)
    print begin,end
    }
  else print $0
  }

For the second script, do the following:

{
 findfld = 0
 if (NF == 9)findfld = index($0,$9)
 if (findfld >0){
   begin = substr($0,1,findfld-2)
   print begin"/"$9
   }
  else print $0
 }

Both of these allow for the possibility that some lines in the original
file have no slashes.

Barbara Vaughan

 
 
 

1. awk script

Each day I have a file generated with several hundred lines. I'd like to
create a single awk script that would print to screen only the line(s)
with the most fields. I've futzed around with this and come to a dead
end, brain-dead probably. Found lots of interesting examples that help
not. O'Reilly's sed & awk book hasn't provided the inspiration I'd hoped
for. A nudge in the right direction sure would be appreciated.

Bill Piety
Registered Linux user #193275

2. Thanks in Order (former newbie)

3. Need help with simple awk script

4. xrdb

5. awk script that works like "cut"

6. ndiswrapper without windows?

7. getopts / awk script

8. Can't find htpasswd file/compiled for IRIX 5.3

9. awk scripting

10. awk script fails on solaris 2.4

11. 'getline < "-" in awk script does not work!

12. Can you launch an awk script this way?

13. awk script