AWK with 2 field separators 
Author Message
 AWK with 2 field separators


Quote:

>Hi all,

>I have one file with data in follwing format

>-rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/1.txt
>-rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/2.txt
>-rwx--rww--w- nayan xx xx nov 2 /usr/xyz/abc/3.txt
>-rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/4.txt

>now i want result in following format

>oct 30 1.txt
>oct 30 2.txt
>oct 30 4.txt

>how is it possible in awk to cut it on basis of 2 field separator

While it is possible to use regular expressions for field separators
in gawk (and perhaps some versions of awk), I would think it
easier to merely edit the lines using the sub() function:

gawk '/oct/{sub(/.*oct/,"oct");sub(/\/.*\//,"");print}' infile

which produced this output:

oct 30 1.txt
oct 30 2.txt
oct 30 4.txt

To answer your original question,

gawk -F"[ /]" '/oct/{print $5, $6, $NF}' infile

produced this output:

oct 30 1.txt
oct 30 2.txt
oct 30 4.txt

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 20 Apr 2001 03:00:00 GMT  
 AWK with 2 field separators

Quote:

> I have one file with data in follwing format

> -rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/1.txt
> -rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/2.txt
> -rwx--rww--w- nayan xx xx nov 2 /usr/xyz/abc/3.txt
> -rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/4.txt

> now i want result in following format

> oct 30 1.txt
> oct 30 2.txt
> oct 30 4.txt

What happened to the record for the file named 3.txt?

Quote:
> how is it possible in awk to cut it on basis of 2 field separator

Set the value of the built-in variable FS to a regular expression; in
this case, FS = "[ /]".

Here's a brief command-line demonstration:

$ cat nayan.dat
-rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/1.txt
-rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/2.txt
-rwx--rww--w- nayan xx xx nov 2 /usr/xyz/abc/3.txt
-rwx--rww--w- nayan xx xx oct 30 /usr/xyz/abc/4.txt
$ awk '{ print $5, $6, $NF }' FS='[ /]' nayan.dat
oct 30 1.txt
oct 30 2.txt
nov 2 3.txt
oct 30 4.txt
$

This exemplary script is based on assumptions that may or may not be
safe ones to make about your data (your ls -l lists). (Do any file
names contain embedded spaces? Are the month and date values always
the fifth and sixth words on the line? Conversely, are the fifth and
sixth words on the line always month and date values? Caveat scriptor.)

--
Jim Monty

Tempe, Arizona USA                    AVOID CLICHES LIKE THE PLAGUE!



Fri, 20 Apr 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Strange AWK behaviour with Field-Separators

2. multiple field separators in AWK

3. Dealing with commas as a field separator AND possibly within a field

4. Hexadecimal Field Separator

5. Multiple field separators for paranthesis not working

6. Field Separator

7. Field Separator larger than one character

8. Field Separator not working

9. Quoted field separators

10. backslash as field separator

11. field separator

12. Problem with Field Separators

 

 
Powered by phpBB® Forum Software