Iain Hallam
Iain Hallam

Reputation: 289

What Linux commands can parse a file with two delimiters per line?

I've got a file that contains two delimiters (":" and ","), and I basically want something like a nested/recursive AWK command that will let me iterate over the values I want:

Value-1.1:Value-1.2:Value-1.3:Value-1.A,Value-1.B,Value-1.C
Value-2.1:Value-2.2:Value-2.3:Value-2.A,Value-2.B

For every Value-1.{A|B|C} I want to do something with Value-1.2, and then move on to the next line, where for every Value-2.{A|B}, I want to do something with Value-2.2, and so on.

Is this something I can do in a single AWK script, or am I going to need to be creative with pipes?

Thanks.

Upvotes: 0

Views: 1333

Answers (3)

thiton
thiton

Reputation: 36049

You can get creative with awk by replacing the field separator and assigning to $0:

 v11 = $1;
 v12 = $2;
 FS = ",";
 $0 = $3;
 print v11 " " v12 " " $1;

Upvotes: 0

Michael J. Barber
Michael J. Barber

Reputation: 25042

Awk uses a regular expression to indicate the field separator. E.g.,

awk -F ':|,' '{ print NF }' 

will show

6
5

for your input lines.

Upvotes: 2

perreal
perreal

Reputation: 97948

an example with perl:

#!/usr/bin/perl

while (my $line = <STDIN>) {
  my @vals = split(/,|:/, $line);
  for (my $value) (@vals) {
    print $value, "\n";
  }
}

Upvotes: 0

Related Questions