[SOLVED] How would I use awk or sed to match this?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Content-Disposition: attachment; filename= "=?big5?B?W8Opw6m2faTWl13i50?==?big5?Q?orrent?="
Green is the constant portion. Red is variable portion and the space inbetween the 'filename=' and the quotation mark '"' can be either a single space, a enter to a new line or several tabs?
Thanks
Ted
Last edited by ted_chou12; 04-08-2011 at 03:03 AM.
Sorry .. my bad I just thought the line endings had been messed up on the paste
btw. this is the correct sed should it all be on one line:
Code:
sed -r -n '/^Content/s/^[^"]*"|"$//gp' file
So now that I know it is over multiple lines ... how do you want the data returned? ie. do you still want it over multiple lines or joined into a single entry?
sed -r '/^Content-Disposition: attachment; filename=(*.?)/\1/p')
The first part: '/^Content-Disposition: attachment; filename=(*.?)/
matches a line. You're `s' command is missing a left hand side.
The "*.?" pattern doesn't make sense.
To match characters inside quotes, you could use:
"\([^"]*\)" or ".*"
However, your sample had the contents inside the quotes spread across 2 or 3 lines. Was that a mistake in copying to this post, or does it represent a real sample? Sed is a line editor. The input the LHS matches is in a line of input. If the input is in 2 or 3 lines, you need to build up more lines using the command "N" or "H".
Code:
sed -n '/Content-Disposition/{ /".*"/!N}
/Content-Disposition/{ /".*"/!N}
/Content-Disposition/{ /".*"/s/\n//g;p}' test
Content-Disposition: attachment; filename= "=?big5?B?W8Opw6m2faTfpHDC7V9ieV9qYWNreWtvMjAwMl13aGl0ZSBhbGJ1bSAxNC0yNi50?= =?big5?Q?orrent?="
Content-Disposition: attachment; filename="=?big5?B?W8Opw6m2faTfpHDC7V9ieV9qYWNreWtvMjAwMl13aGl0ZSBhbGJ1bSAxNC0yNi50?= =?big5?Q?orrent?="
Here I am building up the input pattern by up to 3 lines if both quotes are not present in the the line. Then I removed the line feeds, joining the line.
If you just want the contents between the quotes you could use:
s/\n//g;s/.*\(".*"\).*/\1/p
in it's place.
This prints the contents without the quotes:
Code:
sed -n '/Content-Disposition/{ /".*"/!N}
/Content-Disposition/{ /".*"/!N}
/Content-Disposition/{ /".*"/s/\n//g;s/.*"\(.*\)"/\1/p}' test
=?big5?B?W8Opw6m2faTfpHDC7V9ieV9qYWNreWtvMjAwMl13aGl0ZSBhbGJ1bSAxNC0yNi50?= =?big5?Q?orrent?=
=?big5?B?W8Opw6m2faTfpHDC7V9ieV9qYWNreWtvMjAwMl13aGl0ZSBhbGJ1bSAxNC0yNi50?= =?big5?Q?orrent?=
Your sample contains the same contents in both samples. Is that what you wanted?
----
Knowing more information about the input pattern can help. For example, if you have records of lines separated by a blank line, things could be a lot easier.
eg:
Code:
/sbin/lspci -v | sed -n '/Network/,/^$/p'
14:00.0 Network controller: Atheros Communications Inc. AR928X Wireless Network Adapter (PCI-Express) (rev 01)
Subsystem: Foxconn International, Inc. Device e009
Flags: bus master, fast devsel, latency 0, IRQ 19
Memory at f2100000 (64-bit, non-prefetchable) [size=64K]
Capabilities: <access denied>
Kernel driver in use: ath9k
This allows matching a pattern for a range of lines, which could be operated on inside of brackets.
The first part: '/^Content-Disposition: attachment; filename=(*.?)/
matches a line. You're `s' command is missing a left hand side.
The "*.?" pattern doesn't make sense.
To match characters inside quotes, you could use:
"\([^"]*\)" or ".*"
However, your sample had the contents inside the quotes spread across 2 or 3 lines. Was that a mistake in copying to this post, or does it represent a real sample? Sed is a line editor. The input the LHS matches is in a line of input. If the input is in 2 or 3 lines, you need to build up more lines using the command "N" or "H".
Code:
sed -n '/Content-Disposition/{ /".*"/!N}
/Content-Disposition/{ /".*"/!N}
/Content-Disposition/{ /".*"/s/\n//g;p}' test
Content-Disposition: attachment; filename= "=?big5?B?W8Opw6m2faTfpHDC7V9ieV9qYWNreWtvMjAwMl13aGl0ZSBhbGJ1bSAxNC0yNi50?= =?big5?Q?orrent?="
Content-Disposition: attachment; filename="=?big5?B?W8Opw6m2faTfpHDC7V9ieV9qYWNreWtvMjAwMl13aGl0ZSBhbGJ1bSAxNC0yNi50?= =?big5?Q?orrent?="
Here I am building up the input pattern by up to 3 lines if both quotes are not present in the the line. Then I removed the line feeds, joining the line.
If you just want the contents between the quotes you could use:
s/\n//g;s/.*\(".*"\).*/\1/p
in it's place.
This prints the contents without the quotes:
Code:
sed -n '/Content-Disposition/{ /".*"/!N}
/Content-Disposition/{ /".*"/!N}
/Content-Disposition/{ /".*"/s/\n//g;s/.*"\(.*\)"/\1/p}' test
=?big5?B?W8Opw6m2faTfpHDC7V9ieV9qYWNreWtvMjAwMl13aGl0ZSBhbGJ1bSAxNC0yNi50?= =?big5?Q?orrent?=
=?big5?B?W8Opw6m2faTfpHDC7V9ieV9qYWNreWtvMjAwMl13aGl0ZSBhbGJ1bSAxNC0yNi50?= =?big5?Q?orrent?=
Your sample contains the same contents in both samples. Is that what you wanted?
----
Knowing more information about the input pattern can help. For example, if you have records of lines separated by a blank line, things could be a lot easier.
eg:
Code:
/sbin/lspci -v | sed -n '/Network/,/^$/p'
14:00.0 Network controller: Atheros Communications Inc. AR928X Wireless Network Adapter (PCI-Express) (rev 01)
Subsystem: Foxconn International, Inc. Device e009
Flags: bus master, fast devsel, latency 0, IRQ 19
Memory at f2100000 (64-bit, non-prefetchable) [size=64K]
Capabilities: <access denied>
Kernel driver in use: ath9k
This allows matching a pattern for a range of lines, which could be operated on inside of brackets.
Thank you! jschiwal This was a very detailed explanation! Thanks to grail too.
Ted
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.