Mahesh
Mahesh

Reputation: 69

How to process nested delimiters in a same file in unix

I have a semicolon-separated file.

And one of the fields in the file is pipe-separated.

Example.

field-1;field-2;value-1|value-2|value-3;field-4;field-5

Here field-3 is nothing but pipe-separated values.

Now my requirement is that if there are multiple values in field-3,

I only have to keep the first value. Discard anything after the first pipe in that field.

i.e., I want to keep only first value in field-3 as shown below

field-1;field-2;value-1;field-4;field-5

Can you please tell me how can I achieve this using sed/awk?

Upvotes: 0

Views: 62

Answers (3)

potong
potong

Reputation: 58401

This might work for you (GNU sed):

sed -r 's/^(([^;]*;){2}[^|;]*)[^;]*/\1/' file

This matches the first three fields and replaces it by the first two fields and the first value in the third field.

Upvotes: 0

Guru
Guru

Reputation: 16984

One way:

$ x='field-1;field-2;value-1|value-2|value-3;field-4;field-5'
$ echo $x | awk -F";" '{gsub(/\|.*/,"",$3);}1' OFS=";"
field-1;field-2;value-1;field-4;field-5
$

Upvotes: 0

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's/[|][^;]*//g' YourFile
  • it remove (replace by nothing) any value after (including) a | ( the [|] pattern) until it is not a ; ( the [^;] pattern) and for each occurence ( the g option)
  • Assuming | is only a separator and not part of a filed value (like in string)

Upvotes: 2

Related Questions