We help IT Professionals succeed at work.

remove string of text from file via bash script

Medium Priority
646 Views
Last Modified: 2012-08-14
I have the following string to remove or replace from a file.  "X-LIC-LOCATION:America/New_York"  Sadly I cannot get commands sed, grep, awk to work.  I have tried assigning a variable to the string and then putting it into the commands.  

Attached is a little piece of the code I have been working with.

I am running RHEL5.4

Thanks!
xny="X-LIC-LOCATION:America/New_York"
ny="DTEND;TZID=America/New_York:"
blank="NULL"
#then replace strings
#sed -i 's/{cat $xny}/{cat $blank}/g' /root/Desktop/event.csv
sed -i 's/SUMMARY:/event_title /g' /root/Desktop/event.csv 
#sed -i 's/$ny/event_time_end/g' /root/Desktop/event.csv

Open in new window

Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
Please tell me what you want to replace

As I understand you want

sed -i 's/$xny/$blank/g' /root/Desktop/event.csv

Be careful with -i as the file would change.  You may be better off without -i in the test phase.

Please clarify your question

Author

Commented:
Thanks for the fast response guys!

The ""X-LIC-LOCATION:America/New_York" string needs to be gone. Alternatively it could be replaced with NULL or "event_location".

farzanj you have been working with me on https://www.experts-exchange.com/OS/Linux/Distributions/Q_26975723.html   any luck/ideas?

point pleasant what does grep -v do?  I looked up the manual page of the command and it said " -v, --invert-match Invert the sense of matching, to select non-matching lines."  I can't figure out what that means :(
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
hmm cool let me get it into my main code and try it out...  

Author

Commented:
grep -v took out the string!

then i tried "cat file | grep -v "SUMMARY:" and it didnt work...  Why would one string work and not another?
can you post copy of the file?
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
is the double quote in front of cat a typo

Author

Commented:
Attached is file example.

farzanj does the "\" tell the sed command that the search string contains a "/" ?  If so this may have been the reason it was not working for me.
event.csv

Author

Commented:
double quote is not typo... that was me trying to quote my actual command... noticed i forgot double quote at end... posting actual command in code section...
cat file | grep -v "SUMMARY:"

Open in new window

SRG041808

Not sure why grep -v SUMMARY is not working.  check command line you are using for a typo of feel free to post here

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:

Awesome info guys.   Let me play with my code, i am pretty sure both your answers have helped tremendously and resolved my issue!!  I'll leave the question open for now but dont worry I wont let it rot.  I just want to make sure I dont close it prematurely.

farzanj i had to make a small modification to your command, just added single quotes and it works.  below code line 1 was your command and code line 2 was my small modification.
sed s/X-LIC-LOCATION:America\/New_York//  filename
sed 's/X-LIC-LOCATION:America\/New_York//'  filename

Open in new window

Author

Commented:
All the solutions worked for me.

Thanks for the help!!!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.