Reputation: 69
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
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
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
Reputation: 10039
sed 's/[|][^;]*//g' YourFile
|
( the [|]
pattern) until it is not a ;
( the [^;]
pattern) and for each occurence ( the g
option)|
is only a separator and not part of a filed value (like in string)Upvotes: 2