awk multiple fields separators


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk multiple fields separators
# 8  
Old 12-03-2014
Thanks RudiC ,

Works fine with GAWK ... but with NAWK I get this error
Code:
 
 nawk: you can only delete array[element] at source line 9
 context is
                                 delete O >>> 
 <<<                         }
nawk: syntax error at source line 9
nawk: illegal statement at source line 9

I did fix the <CR>

Thanks !

---------- Post updated at 05:28 AM ---------- Previous update was at 04:26 AM ----------

I tried this instead to clear out the array instead of delete
Code:
 
 split("", O)

Looks like its working ...

Last edited by vbe; 12-03-2014 at 09:37 AM.. Reason: code tags...
This User Gave Thanks to greycells For This Post:
# 9  
Old 12-04-2014
Have a question ... how do i get the complete 2nd field between commas if it has spaces in it

for ex

Code:
export "/0014apps test" name="/reet" root=10.200.12.29:10.200.12.32 access=10.200.12.29:10.200.12.32


with this code ... the space gets skipped
Code:
awk     'BEGIN          {nK=split ("name root access ro rw", K)}
                        {gsub (/"/,"")
                         for (i=3; i<=NF; i++) {split($i, T, "="); O[T[1]]=T[2]}

                         printf "[%s]", $2
                         for (i=1; i<=nK; i++) printf ",%s", O[K[i]]?"["O[K[i]]"]":""
                         printf "\n"
                         delete O
                        }
        ' file

output

Code:
[/0014apps],[/reet],[10.200.12.29:10.200.12.32],[10.200.12.29:10.200.12.32],,

thanks!!
# 10  
Old 12-04-2014
xargs understands quotes, so can be used to separate records which are annoying to handle other ways.

Code:
xargs -n 1 < inputfile | awk 'NR==2'

# 11  
Old 12-04-2014
thanks ! ... but I am not sure how would I use xargs in the above script .
The script works fine ... and gives me the right output ..except few records in the input file have space between the "" .. for example in the above example

the output should be

Code:
 
 [/0014apps test],[/reet],[10.200.12.29:10.200.12.32],[10.200.12.29:10.200.12.32],,

instead of
Code:
 
  [/0014apps],[/reet],[10.200.12.29:10.200.12.32],[10.200.12.29:10.200.12.32],,

Thanks !
# 12  
Old 12-11-2014
Still cannot figure out how to get the complete field for first column including spaces

Code:
export "/0014apps/test folder" name="/reet" root=10.200.12.29:10.200.12.32 access=10.200.12.29:10.200.12.32
export "/0016apps" ro=10.202.140.3 root=10.202.140.34:10.202.140.37 access=10.202.140.34:10.202.140.37 
export "/tech_st" root=10.202.98.59 rw=10.202.98.5 access=10.202.98.59



Output needed

Code:
[/0014apps/test folder],[/reet],[10.200.12.29:10.200.12.32],[10.200.12.29:10.200.12.32],,
[/0016apps],,[10.202.140.34:10.202.140.37],[10.202.140.34:10.202.140.37],[10.202.140.3],
[/tech_st],,[10.202.98.59],[10.202.98.59],,[10.202.98.5]

Code:
 
 awk     'BEGIN          {nK=split ("name root access ro rw", K)}
                        {gsub (/"/,"")
                         for (i=3; i<=NF; i++) {split($i, T, "="); O[T[1]]=T[2]}

                         printf "[%s]", $2
                         for (i=1; i<=nK; i++) printf ",%s", O[K[i]]?"["O[K[i]]"]":""
                         printf "\n"
                         delete O
                        }
        ' file
 
[/0014apps],[/reet],[10.200.12.29:10.200.12.32],[10.200.12.29:10.200.12.32],,
[/0016apps],,[10.202.140.34:10.202.140.37],[10.202.140.34:10.202.140.37],[10.202.140.3],
[/tech_st],,[10.202.98.59],[10.202.98.59],,[10.202.98.5]

Thanks !



# 13  
Old 12-13-2014
The problem is the field separator occurs in the field. For example:
Code:
export "/0014apps/test folder" name="/reet" root=10.200.12.29:10.200.12.32 access=10.200.12.29:10.200.12.32

to be split into
Code:
$1: export
$2: "/0014apps/test
$3: folder"
$4: name="/reet"
$5: root=10.200.12.29:10.200.12.32
$6: access=10.200.12.29:10.200.12.32

What is needed is a field separator that takes into account the quoted string - to be able to split the line into what is needed. Don't think awk is up to the task, so use perl:
Code:
use strict;
use warnings;

$, = ',';
$\ = "\n";

my @K = qw{ name root access ro rw };

while (<>) {
  chomp;
  my @F = m{(?:[^ "]|"[^"]+")+}g;

  shift @F; # get rid of the export

  my $dir = shift @F;
     $dir =~ s{"}{}g;

  my %O = ();

  for (@F) {
    my ($k, $v) = split /=/;
    next unless defined $v;
    $v =~ s{"}{}g;
    $O{$k} = $v;
  }

  print '[' . $dir . ']', map { defined $O{$_} ? '[' . $O{$_} . ']' : '' } @K;
}

Which results in
Code:
[/0014apps/test folder],[/reet],[10.200.12.29:10.200.12.32],[10.200.12.29:10.200.12.32],,
[/0014apps],[/reet],[10.200.12.29:10.200.12.32],[10.200.12.29:10.200.12.32],,
[/0016apps],,[10.202.140.34:10.202.140.37],[10.202.140.34:10.202.140.37],[10.202.140.3],
[/tech_st],,[10.202.98.59],[10.202.98.59],,[10.202.98.5]

I would be curious to know if there is an awkish solution.

Side note, I have to ask - why the square-braces?
This User Gave Thanks to derekludwig For This Post:
# 14  
Old 12-13-2014
the square brackets are just cosmetic .... optional I guess

Thanks again !!

---------- Post updated at 06:58 AM ---------- Previous update was at 06:43 AM ----------

quick question ... I missed another field in the input , not good at perl ... how can I add it

Code:
export "/0014apps/test folder" name="/reet" root=10.200.12.29:10.200.12.32 access=10.200.12.29:10.200.12.32 comment="test folder,finance/HR"
export "/0016apps" ro=10.202.140.3 root=10.202.140.34:10.202.140.37 access=10.202.140.34:10.202.140.37 comment="A&E group-NJ"

output
Code:
 
 [/0014apps/test folder],[/reet],[10.200.12.29:10.200.12.32],[10.200.12.29:10.200.12.32],,[test folder,finance/HR]
[/0016apps],,[10.202.140.34:10.202.140.37],[10.202.140.34:10.202.140.37],[10.202.140.3],[A&E group-NJ]

Thanks !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print multiple fields with awk

so its common knowledge one can print multiple fields with simple commands like this: echo 12 44 45 552 24 | awk '{print $1,$4,$3}' but suppose i want to avoid specifying the "$" symbol. is that possible? can something like this be done: echo 12 44 45 552 24 | awk '{print $(1,4,3)}' ... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

awk multiple filed separators

There is an usual ifconfig output vlan30 Link encap:Ethernet HWaddr inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: 2407:4c00:0:1:aaff::1/64 Scope:Global inet6 addr: fe80::224:e8ff:fe6b:cc4f/64 Scope:Link UP BROADCAST... (1 Reply)
Discussion started by: urello
1 Replies

3. Shell Programming and Scripting

Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file: 1 3 3 2 3 3 4 0 4 0 5 4 5 2 2 0 5 3 4 0 6 0 3 2 I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's" I tried the following, but it's not working awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input ... (8 Replies)
Discussion started by: Rabu
8 Replies

4. Shell Programming and Scripting

Multiple long field separators

How do I use multiple field separators in awk? I know that if I use awk -F"", both a and b will be field separators. But what if I need two field separators that both are longer than one letter? If I want the field separators to be "ab" and "cd", I will not be able to use awk -F"". The ... (2 Replies)
Discussion started by: locoroco
2 Replies

5. Shell Programming and Scripting

awk gsub multiple fields

Hi, I am trying to execute this line awk -F ";" -v OFS=";" '{gsub(/\./,",",$6); print}' FILE but for multiple fields $6 $7 $8 Do you have a suggstion? Tried: awk -F ";" -v OFS="";"" "function GSUB( F ) {gsub(/\./,\",\",$F); print} { GSUB( 6 ); GSUB( 7 ); GSUB( 8 ) } 1"... (2 Replies)
Discussion started by: nakaedu
2 Replies

6. Shell Programming and Scripting

AWK multiple fields separators

I need to print the second field of a file, taking spaces, tab and = as field separators. ; for 16-bit app support MAPI=1 CMC=1 CMCDLLNAME32=mapi32.dll CMCDLLNAME=mapi.dll MAPIX=1 MAPIXVER=1.0.0.1 OLEMessaging=1 asf=MPEGVideo asx=MPEGVideo ivf=MPEGVideo m3u=MPEGVideo (2 Replies)
Discussion started by: PamPam
2 Replies

7. UNIX for Dummies Questions & Answers

Multiple field separators in awk? (First a space, then a colon)

How do I deal with extracting a portion of a record when multiple field separators are involved. Let's say I have: Mike Harrington;(555) 555-5555:250:100:175 Christian Dobbins;(555) 555-2358:155:90:201 Susan Dalsass;(555) 555-6279:250:60:50 Archie McNichol;(555) 555-1348:250:100:175 Jody... (3 Replies)
Discussion started by: doubleminus
3 Replies

8. Shell Programming and Scripting

Multiple input field Separators in awk.

I saw a couple of posts here referencing how to handle more than one input field separator in awk. I figured I would share how I (just!) figured out how to turn this line in a logfile: 90000000000000000000010001 name... (4 Replies)
Discussion started by: kinksville
4 Replies

9. Shell Programming and Scripting

I need help counting the fields and field separators using Nawk

I need help counting the fields and field separators using Nawk. I have a file that has multiple lines on it and I need to read the file 1 at a time and then count the fields and field separators and then store those numbers in variables. I then need to delete the first 5 fields and the blank... (3 Replies)
Discussion started by: scrappycc
3 Replies

10. Shell Programming and Scripting

Awk Multiple Field Separators

Hi Guys, I'm tying to split a line similar to this:YO6-2000-30.htm: (3 properties found).......into separate columns, so effectively I need to check for a -, ., :, a tab and a space in the statement. Any help would be appreciated Thanks! (7 Replies)
Discussion started by: Tonka52
7 Replies
Login or Register to Ask a Question