[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

remove string of text from file via bash script

Posted on 2011-04-27
16
Medium Priority
?
628 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

0
Comment
Question by:SRG041808
  • 7
  • 5
  • 4
16 Comments
 
LVL 8

Assisted Solution

by:point_pleasant
point_pleasant earned 400 total points
ID: 35475489
how about

cat file | grep -v "X-LIC-LOCATION:America/New_York"
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35475498
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
0
 

Author Comment

by:SRG041808
ID: 35475585
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 http://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 :(
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Assisted Solution

by:point_pleasant
point_pleasant earned 400 total points
ID: 35475619
grep -v passes everything through except the string following the -v so in essense it will strip out X-LIC-LOCATION:America/New_York from the file
0
 

Author Comment

by:SRG041808
ID: 35475631
hmm cool let me get it into my main code and try it out...  
0
 

Author Comment

by:SRG041808
ID: 35475748
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?
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35475794
can you post copy of the file?
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 600 total points
ID: 35475797
If you want to delete this line from a file use this
sed /X-LIC-LOCATION:America\/New_York/d filename

Once it works, stick in -i option to remove from actual file
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 600 total points
ID: 35475813
If you want to just remove this string without deleting the entire line use this:

sed s/X-LIC-LOCATION:America\/New_York//  filename

Again you can use -i option when you want to modify the file as well
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35475824
is the double quote in front of cat a typo
0
 

Author Comment

by:SRG041808
ID: 35475856
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
0
 

Author Comment

by:SRG041808
ID: 35475882
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

0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35475918
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

0
 
LVL 31

Accepted Solution

by:
farzanj earned 600 total points
ID: 35475942
Yes, you have to escape / by \/

Or you can use

sed s| | |


Instead of
sed s/ / /

Did you try my solution?  Any other problems?
0
 

Author Comment

by:SRG041808
ID: 35476132

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

0
 

Author Closing Comment

by:SRG041808
ID: 35477382
All the solutions worked for me.

Thanks for the help!!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question