Home > Back-end >  Regex/sed formatting help for upload to sheets
Regex/sed formatting help for upload to sheets

Time:11-17

Hello I am looking for some help on how to format a file using sed.

I have a file that is generated that looks something like this:

projectA
[email protected]
[email protected]
[email protected]
TestB
[email protected]
[email protected]
ProjectC
[email protected]

I was playing around with sed to format, but I know there is an easier way to go about this instead of running several sed commands.

I'd like for the format to Look something like this in sheets:

ProjectID1 | Email1 | Email2 | ...
ProjectID2 | Email1 | Email2 | Email3 | ...
ProjectID3 | Email1 | ...

CodePudding user response:

I would use GNU AWK for this task following way, let file.txt content be

projectA
[email protected]
[email protected]
[email protected]
TestB
[email protected]
[email protected]
ProjectC
[email protected]

then

awk 'NR>1{printf /@/?" | ":"\n"}{printf $0}' file.txt

output

projectA | [email protected] | [email protected] | [email protected]
TestB | [email protected] | [email protected]
ProjectC | [email protected]

Assumption: every line with @ anywhere is email, every other line is project id.

Explanation: I use printf as it does not add newline to end. Before all but first line I printf either | or newline (\n) depending if it is mail line or other line, I use ternary operator (condition?valueiftrue:valueiffalse) to select depending on if line contains @ or not. For every line I printf it. Note: my code produce output without trailing newline, if you does need one then add END{print ""} behind {printf $0}.

(tested in gawk 4.2.1)

CodePudding user response:

This might work for you (GNU sed):

sed -E ':a;N;s/\n(.*@)/ | \1/;ta;P;D' file

Gather up lines, replacing the newline between lines which are not email addresses and lines that are email addresses by |.

  • Related