How to remove string in the text file ? Bash script
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
Of course it is more common to do this kind of operation (replacement/deletion) with sed:
Code:
sed -e s/https:"\/""\/"myserver"\/"xa"\/"if?took1=// file >newfile
For this particular line sed is a bit of a bear because / has is used for subtitution:
sed -e s/pattern/newpattern/
Since your pattern includes several / characters you have to do the bizarre quoting and escaping seen in the live above so it knows which ones are literal and which are part of the sed script.
This is why I mentioned sed - it is more common to do something like this:
Code:
sed -e s/myserver/yoursever/ file >newfile
Since you're only replacing one string you don't have to worry about all the rest of the line (special characters etc that I mentioned in earlier post).
Last edited by jlightner; 06-05-2009 at 09:37 AM..
Of course it is more common to do this kind of operation (replacement/deletion) with sed:
Code:
sed -e s/https:"\/""\/"myserver"\/"xa"\/"if?took1=// file >newfile
For this particular line sed is a bit of a bear because / has is used for subtitution:
sed -e s/pattern/newpattern/
Since your pattern includes several / characters you have to do the bizarre quoting and escaping seen in the live above so it knows which ones are literal and which are part of the sed script.
Actually there's no need to resort to backslash escapes here. You can use any character as the separator in sed, not just the slash. Try changing it to something like '|' instead, for example.
Code:
sed -e 's|https://myserver/xa/if?took1=||' file >newfile
You also don't need to quote each character separately. Quote the entire phrase in single quotes instead, and the whole string will be passed to sed literally.
Change $1 to $2 to print everything after the "=".
Code:
awk -F = '{print $1}' filename > new filename
To write output of command to a new file
To expand on this, awk works on the concept of fields. $1 is field one of the input string, $2 is field 2, etc. Usually the field separator is a space, but the '-F =' changes the separator to the equals sign. So $1 is everything before the equals sign, and $2 is everything after it.
Follow it up with the command to print only the fields you want to see and Bob's your uncle, as the Brits say.
You can learn a lot more about how to use sed and awk on this page. It's well worth taking the time to read (and work through) them.
Of course it is more common to do this kind of operation (replacement/deletion) with sed:
replacement and substitution operation is common string manipulation task but not necessary the tool used. since the input data has a structured format, it is obvious using fields and field delimiter is the better and "neater" approach.
Code:
awk -F"=" '{print $NF}'
you do not have to meddle with escaping slashes and stuffs like that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.