Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


How to prevent sed command removing whole line?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
How to prevent sed command removing whole line?

hello,

I'm using sed command to extract the text between 2 special characters which are /* and */
I used following command to do this.

Code:
sed -n '/\/\*/,/\*\//p' file.txt

But if the file.txt contains a line something like this,

Code:
a=5;    /* this is a comment */

the above command extracts this whole line whereas I want only /* this is a comment */ part.

How to overcome this?


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-12-2016 at 05:12 AM.. Reason: Added CODE tags.
# 3  
Slight modification of itkamaraj's fine proposal to yield the full required output:
Code:
echo "a=5; /* this is a comment */" | sed "s;.*\(/\*.*\*/\);\1;"
/* this is a comment */

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Hi beginner_99,
Note that the suggestions you have received work for your sample input (and any other input that has no comments or exactly one complete comment on each line). The suggestions will not work with the following "code":
Code:
/*
** Multi-line comment.
*/
/* Multiple comments... */ code; /* ... on one line. */

Are either of these limitations a problem for the real data you will be processing?
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
sed or awk - removing part of line?
alis
hi all, I am having trouble finding the right string for this - I dont know whether to use awk or sed.. If I have a file with alot of names and phone numbers like this McGowan,Sean 978-934-4000 Kilcoyne,Kathleen 603-555-1212 Club603,The 617-505-1332 Boyle,William 301-444-1221 And...... UNIX for Dummies Questions & Answers
11
UNIX for Dummies Questions & Answers
sed removing until end of line
BeefStu
All: Can somebody help me out with a sed command, which removes the the first occurance of ')' until the end of the line If I have the following input ...... Shell Programming and Scripting
5
Shell Programming and Scripting
Removing end of line using SED
humkhn
Hello Friends, How can I remove the last two values of this line using sed John Carey:507-699-5368:29 Albert way, Edmonton, AL 25638:9/3/90:45900 The result should look like this: John Carey:507-699-5368:29 Albert way, Edmonton, AL 25638... UNIX for Dummies Questions & Answers
3
UNIX for Dummies Questions & Answers
removing a line containing a pattern in sed
Fire_Storm
i need to use sed to remove an entire line containing a pattern stored in a variable say $var1 this var1 will be a URL and will therefore contain slashes any help would be greatly appreciated... Shell Programming and Scripting
1
Shell Programming and Scripting
removing parts of a line with SED
gammaman
hi, i'm trying to erase all the characters after, and including, the first test test Output: test1 test2 test3 this is what I tried, but didn't work sed "s/*//" file > testfilename any suggestions? thanks, gammmaman... UNIX for Dummies Questions & Answers
2
UNIX for Dummies Questions & Answers

Featured Tech Videos