Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split data using sed or awk

I have a lot of data I'm trying to split in CSV. My source data has this format:

* USER 'field1' '[email protected]' 'field3'
* USER 'field1' '[email protected]' 'field3'
* USER 'field1' '[email protected]' 'field3'

And here's what I'm trying to get as output:

field1;[email protected];field3
field1;[email protected];field3
field1;[email protected];field3

Rules:

  1. * USER in the begin of the line must be obviously stripped;
  2. field1 and field3 could be an email address, or can contain ';
  3. field1 could be empty ''
  4. the second field is always an email address;
  5. each field has ' on the beginning and ending of the field itself.

My idea was to strip * USER (sed -e 's/^* USER //' could be a starting point), then "find" the mail in "the center" field, and then catch the left side and right side into two vars. Last thing should be to strip beginning and ending ' on the vars. Unfortunately, I don't have sed or awk knowledge at this level. Any ideas on how to achieve this?


Here an example

* USER '' '[email protected]' 'CORDINI ALBERTO'
* USER 'moglie delmonte daniele' '[email protected]' 'Anna Borghi'
* USER '' '[email protected]' 'CRAVERO ANNA MARIA'
* USER '' '[email protected]' 'D'AGOSTINO PATRIZIA'
* USER '' '[email protected]' 'DE PRA' PIERO'
* USER '' '[email protected]' 'D'INGEO VIVIANA'
like image 784
Giuseppe Donato Avatar asked Dec 19 '25 19:12

Giuseppe Donato


1 Answers

Update: You can use this awk for the provided input:

awk -F " '" '{gsub(/^ +| +$/, "", $3);
              s=sprintf("%s;%s;%s;", $2,$3,$4); gsub(/'"'"';/, ";", s); print s}' file
;[email protected];CORDINI ALBERTO;
moglie delmonte daniele;[email protected];Anna Borghi;
;[email protected];CRAVERO ANNA MARIA;
;[email protected];D'AGOSTINO PATRIZIA;
;[email protected];DE PRA' PIERO;
;[email protected];D'INGEO VIVIANA;
like image 164
anubhava Avatar answered Dec 22 '25 10:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!