I have a text file that contains separate lines where this text will always appear, short_message=<some text>. The <some text> part could potentially be different on every line. What I'd like to do is extract the <some text> text and only that text from the file. I don't want the text after the comma following <some text>. I would simply like to extract and redirect this text to another file.

An example of the line is as follows:
..., short_message=<some text>, product_code=xxx_xxx_xxx_xxx_x, subscription_action=SYSTEM, MESSAGE_FORMAT_TYPE=TEXT}}}

I've tried sed, grep, even awk, but I'm no expert on these tools and simply cannot find a way to get only the text I'm looking for. Hoping someone can help.

You need 'cut' (awk would be somewhat more efficient, but I always have to fiddle around to get an awk script working):

cat originalfile | grep 'short_message=' | cut -f2 -d '=' | cut -f1 -d ',' > short_message_file

If your file has some '=' before the part you need, then the first cut will be -fn for an appropriate value of n.

sed '/short_message=/ s/.*short_message=\([^,]*\),.*/\1/'

Thanks so much for the help. These both seem to do the trick.

Awk would be great for this too, especially if the message is in the same 'field' every time.

For instance, in your example text, your 'short_message' is in the second field (after the first comma). Something like this would do the trick: awk -F, '/short_message/ {split ($2,A,"="); print A[2]}' In the example, we set the comma as the initial delimeter with the '-F' flag.
We grab the second field with '$2'.
Then we split that field at the '=' sign and print the second half, which gives us <some text>

I hope this helps! You've already got two good solutions here :) Enjoy!

-G

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.