Delete a string in a file using bash script - bash

We have a file which has unformatted xml content in a single line
<sample:text>Report</sample:text><sample:user name="11111111" guid="163g673"/><sample:user name="22222222" guid="aknen1763y82bjkj18"/><sample:user name="33333333" guid="q3k4nn5k2nk53n6"/><sample:user name="44444444" guid="34bkj3b5kjbkq"/><sample:user name="55555555" guid="k4n5k34nlk6n711kjnk5253"/><sample:user name="66666666" guid="1n4k14nknl1n4lb1"/>
If we find a particular string suppose "22222222", i want to remove the entire string that surrounds the matched string. In our case the entire portion around 22222222 i.e., <sample:user name="22222222" guid="aknen1763y82bjkj18"/> should be removed and the file has to be saved.
How can we do it? Please help

You can do it using sed utility by invoking it like this:
sed -i file -e 's/<[^<]*"22222222"[^>]*>//'

Related

Adding quotes to variating characters in bash

I am trying to use the sed function in order to add double quotes for anything in between a matched pattern and a comma to break of the pattern. At the moment I am extracting the following data from cloudflare and I am trying to modify it to line protocol;
count=24043,clientIP=x.x.x.x,clientRequestPath=/abc/abs/abc.php
count=3935,clientIP=y.y.y.y,clientRequestPath=/abc/abc/abc/abc.html
count=3698,clientIP=z.z.z.z,clientRequestPath=/abc/abc/abc/abc.html
I have already converted to this format from JSON output with a bunch of sed functions to modify it, however, I am unable to get to the bottom of it to put the data for clientIP and clientRequestPath in inverted commas.
My expected output has to be;
count=24043,clientIP="x.x.x.x",clientRequestPath="/abc/abs/abc.php"
count=3935,clientIP="y.y.y.y",clientRequestPath="/abc/abc/abc/abc.html"
count=3698,clientIP="z.z.z.z",clientRequestPath="/abc/abc/abc/abc.html"
This data will be imported into InfluxDB, count will be a float whilst clientIP and clientRequestPath will be strings, hence why I need them to be in inverted commas as at the moment I am getting errors since they arent as they should be.
Is anyone available to provided to adequate 'sed' function to do is?
This might work for you (GNU sed):
sed -E 's/=([^0-9][^,]*)/="\1"/g' file
Enclose any string following a = does not begin with a integer upto a , in double quotes, globally.
here is a solution using a SED script to allow for multiple operations on a source file.
assuming your source data is in a file "from.dat"
create a sed script to run multiple commands
cat script.sed
s/clientIP=/clientIP=\"/
s/,clientRequestPath/\",clientRequestPath/
execute multiple-command sed script on data file redirecting the output file "to.dat"
sed -f script.sed from.dat > to.dat
cat to.dat (only showing one line)
count=24043,clientIP="x.x.x.x",clientRequestPath=/abc/abs/abc.php

How to replace a string after a particular string in some line in txt file using sed in shell scripting?

I have a text file say file.txt containing following data:
APP_ARTIFACT=xxxx
APP_DOMAIN=xxx
APP_NAME=xxx
APP_OWNER_EMAIL=xxx
APP_PATH=xxx
IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:2.4
NAMESPACE=xxx
PAAS=xxxnce1
Now, I want to write a script in .sh which can replace the version after 'IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:' to a different version.
It means i want to replace 2.4 (or it may contain some other string as well) with some other string., while keeping the rest of the file as it is.
How can I do this using some inbuilt linux tool like 'sed'?
after replacing the string containg version with 'mystring;, the expected output after substitution should be :
APP_ARTIFACT=xxxx
APP_DOMAIN=xxx
APP_NAME=xxx
APP_OWNER_EMAIL=xxx
APP_PATH=xxx
IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:mystring
NAMESPACE=xxx
PAAS=xxxnce1
Assuming you want the new version to be 555, you could do:
sed -re 's/(^IMAGE.*):(.*)/\1:555/'
The above matches the line starting with IMAGE and looks at everything after it. It makes sure you have a colon (:) and uses the first match (\1) and appends a colon 555 (:555) for your new version.
You could use a variable as well:
myver="555"
sed -re "s/(^IMAGE.*):(.*)/\1:$myver/"

How to remove a string from a text file in shell command?

I have a non.txt file and want to write a shell script to remove a string from the entire file. File has the following data :-
24321,247,654,"^A","91350","JEFFR2",21714,,1,243,654,"^A","91350","JEFFR2",21714,,1,654,0,"P","N","1140828","CA",,,,,"06037","C016","14","7",0,"21714 JEFFERS LN","","SANTA CLARITA","CA","913503917","","","","20140828"
And from the above set of data i want to remove "^A".
Please help me to find out the solution.
Try this:
sed -i 's/"^A"//g' non.txt

Replacing String Between two special charters in shell script

I am trying to replace a string in my shell script from localhost:7101/RPAS to sound.mojo.com:5555/rpas in my HTML file.
${myDeployment} =<\/td><\td width="0">http://localhost:7101/RPAS<\/td>
My output should be:
${myDeployment} =<\/td><\td width="0">http://sound.mojo.com:5555/rpas<\/td>
The strings that are fixed are ${myDeployment} and </td> and localhost:7101 is not a fixed string.
http://occurs multiple times in document and the above mentioned string occurs at the last , but not at the end of the document.
Is there a way to extend this command to only change the string of the last occurring pattern in the file.
sed -e 's#>http://[^/]*/#>http://sound.mojo.com:5555\/rpas/#' tst.html
Please let me know if there is a way to do it.
You can use this sed command:
sed 's#>http://[^/]*/#>http://sound.mojo.com:5555#' file
${myDeployment} =<\/td><\td width="0">http://sound.mojo.com:5555RPAS<\/td>

bash templating

i have a template, with a var LINK
and a data file, links.txt, with one url per line
how in bash i can substitute LINK with the content of links.txt?
if i do
#!/bin/bash
LINKS=$(cat links.txt)
sed "s/LINKS/$LINK/g" template.xml
two problem:
$LINKS has the content of links.txt without newline
sed: 1: "s/LINKS/http://test ...": bad flag in substitute command: '/'
sed is not escaping the // in the links.txt file
thanks
Use some better language instead. I'd write a solution for bash + awk... but that's simply too much effort to go into. (See http://www.gnu.org/manual/gawk/gawk.html#Getline_002fVariable_002fFile if you really want to do that)
Just use any language where you don't have to mix control and content text. For example in python:
#!/usr/bin/env python
links = open('links.txt').read()
template = open('template.xml').read()
print template.replace('LINKS', links)
Watch out if you're trying to force sed solution with some other separator - you'll get into the same problems unless you find something disallowed in urls (but are you verifying that?) If you don't, you already have another problem - links can contain < and > and break your xml.
You can do this using ed:
ed template.xml <<EOF
/LINKS/d
.r links.txt
w output.txt
EOF
The first command will go to the line
containing LINKS and delete it.
The second line will insert the
contents of links.txt on the current
line.
The third command will write the file
to output.txt (if you omit output.txt
the edits will be saved to
template.xml).
Try running sed twice. On the first run, replace / with \/. The second run will be the same as what you currently have.
The character following the 's' in the sed command ends up the separator, so you'll want to use a character that is not present in the value of $LINK. For example, you could try a comma:
sed "s,LINKS,${LINK}\n,g" template.xml
Note that I also added a \n to add an additional newline.
Another option is to escape the forward slashes in $LINK, possibly using sed. If you don't have guarantees about the characters in $LINK, this may be safer.

Resources