0

I need to get data from grepped string, but I'm kind of newbie.

I have grepped data string like this:

email=test@mail.com&id=12451233&name=Susanna&surname=Scott&street=East

I need to parse email, id, name and surname from this.

Tried to grep like this:

sed -e 's/email\ \(.*\)&id/\1/'

Nothing happens (need to do it using sed) Displaying data one under another is second step

expected format:

test@mail.ru|12451233|Susanna|Scott
Jane
  • 17
  • 4

2 Answers2

1

You could use awk

awk -F "=|&" '{print $2, $4, $6, $8}'

-F is the feldseperator, in your case they are = or(|) & and then just print the columns.

John Goofy
  • 1,330
  • 1
  • 10
  • 20
  • Yes, it displays what I need, but problem is in awk: I'm not the only work who works with bash script and it is little bigger request that I displayed, that's why I asked for help with sed) – Jane Jun 25 '19 at 14:20
  • Ok, than try the answer from @Mark – John Goofy Jun 25 '19 at 14:32
1

Here's a sed answer - simple but gets the job done:

sed -e 's/email=//' -e 's/&id=/ /' -e 's/&name=/ /' -e 's/&surname=/ /' -e 's/&street=.*//'

Reviewing the question, i just discovered you are looking for pipe delimited output. Well, here is the slightly modified sed script for piped output:

sed -e 's/email=//' -e 's/&id=/|/' -e 's/&name=/|/' -e 's/&surname=/|/' -e 's/&street=.*//' 
Mark
  • 4,249
  • 1
  • 18
  • 27
  • Yes, that's what I need. For education purposes: f.e. if there will be an unnecessary field between email and id, how can I remove it? – Jane Jun 25 '19 at 15:02
  • Add a -e (edit) command to the command. &street is already an example for deleting an unnecessary field (well, it is a bit harsh, since it deletes EVERYTHING after the &street token. You can delete a specific token like this `-e 's/&trash=[^&]*//' ` which deletes everything from &trash to the next &. Or if you want to delete everything from email to &id, you can try this: `-e 's/email=.*&id/&id/'` - basically the s command I keep using is s/find/replace/. – Mark Jun 25 '19 at 15:35
  • Thanks! One more if it's not difficult enough: Is it possible to change order of displayed parts? Surname->name->phone->id f.e. – Jane Jun 27 '19 at 07:49
  • Awk can easily rearrange the results. Try piping it to ` awk `{print $4,$3,$2,$1}'` – Mark Jun 27 '19 at 12:26