Remove part of url and keep the rest using sed - regex

Need to remove some part of the url and keep the remaining part same .
Below is the part of my text file which contains like this
{
host = "http://nxt-newepisode.xcfm.crata.dive.com/err1.2.2/table/kenny.xml.gz"
}
{
host = "http://nxt-secondepisode.xcfm.crata.dive.com/err1.2.2/table/kenny.xml.gz"
}
from above two urls i want get rid off nxt- and keep the rest . I know using
sed -i 's/nxt-//g' FILE
can solve the issue but i want to be specific and only remove nxt- for first url and nxt- from second url without making changes to rest of file.
I am trying this
sed -i '/host/s#"http://nxt-newepisode.*"#" "#' FILE

Could you please try following.
awk '/host/ && ++count==1{sub(/nxt-/,"")} 1' Input_file
Once you are happy with results then to save output into Input_file use following:
awk '/host/ && ++count==1{sub(/nxt-/,"")} 1' Input_file > temp && mv temp Input_file

Related

Sed - replace value in file with regex match in another file

I am trying to code a bash script in a build process where we only have a few tools (like grep, sed, awk) and I am trying to replace a value in an ini file with a value from a regular expression match in another.
So, I am matching something like "^export ADDRESS=VALUE" in file export_vars.h and putting VALUE into an ini file called config.ini in a line with "ADDRESS=[REPLACE]". So, I am trying to replace [REPLACE] with VALUE with one command in bash.
I have come across that sed can take an entire file and insert it into another with a command like
sed -i -e "/[REPLACE]/r export_vars.h" config.ini
I need to somehow refine this command to only read the pattern match from export_vars.h. Does anyone know how to do this?
sed is for simple substitutions on individual lines, that is all. You need to be looking at awk for what you're trying to do. Something like:
awk '
BEGIN { FS=OFS="=" }
NR==FNR {
if ( $1 == "export ADDRESS" ) {
value = $2
}
next
}
{ sub(/\[REPLACE\]/,value); print }
' export_vars.h config.ini
Untested, of course, since you didn't provide testable sample input/output.
Another in awk:
$ awk '/ADDRESS/{if(a!="")$0=a;else a=$NF}NR>FNR' export_vars.h config.ini
ADDRESS=VALUE
Explained:
$ awk '
/ADDRESS/ { # when ADDRESS is found in record
if(a!="") $0=a # if a is set (from first file), use it
else a=$NF } # otherwise set a with the last field
NR>FNR # print all record of the last file
' export_vars.h config.ini # mind the order
This solution does not tolerate space around = since $0 is replaced with $NF from the other file.

Format the "stats" parameter of rsync

I am very new to scripts, but I am writing a little rsync script for my NAS. I am now trying to edit the output of the rsync "stats" parameter.
Because this parameter give a lot of details, and I only need the final results, I start by only keeping the part I want :
sed -e '/Number/,$!d' $log > tmp && mv tmp $log
So that output for now looks like this :
enter image description here
So then I would like to remove the kind of timestamp from each line :
sed -e 's,.*] ,,' $log > tmp && mv tmp $log
So now it looks like this (in Outlook, as I send this result by email) :
enter image description here
So then, I thought I could add a new line. I have tried multiples possibilities, but it doesn't work the way I would like. I can't show you a 3rd picture though.
Would you have any suggestion for me :) ? Thanks for your help !
Thanks Lars for your answer. Your code did what I wanted, but the problem persisted.
Lucky me, I found my problem with outlook : https://naveensnayak.wordpress.com/2012/08/30/ms-outlook-messing-up-line-breaks/
So instead of adding a new line, I made a very little change to my code :
sed -e 's,.*], ,' $log > tmp && mv tmp $log
Because there is a whitespace after my bracket (]), and that I add a 2nd whitespace, now my file looks nice in Outlook.
You can add a newline to a file with the echo command:
> cat test.txt
1
2
3 > echo -e "\n" >> test.txt
> cat test.txt
1
2
3
>
-e enables the backslash interpretation
"\n" is interpreted as newline
>> appends instead of overwrites

Match multiple lines of a file and replace if content does not match

I have a particular file(sample.txt) say with content :
Doc=True
Add=123.566.33.8
This #is a sample File
Use #for Certain specific functions
Domain=443.59.43.23
Upon performing a certain operation, the content of this file changes :
Doc=True
Add=123.566.33.8
eshfsjfn
sldfhsehfoewjwefn
lsjdnfnfd
Now I need to make sure that the content always remains as in the first case.
So I'm trying to grep the initial pattern(sample.txt) using a certain grep command with all the required lines and if in case the lines do not match, then it should append these lines in the file sample.txt .
The content in sample.txt should always be limited to what is shown initially. I don't want to create another file with the same content and then match them.
I want to use grep, sed or perhaps awk.
I post this answer assuming you want sample.txt to be checked against an standar awk file entrance
Hope it can help you
awk'BEGIN {
while (getline < "sample.txt")
{
a[$0]
}
}
{
if( $0 in a)
{
located=true;
}
if(!located)
{
print "Line changed ->"NR" ->"$0
print $0 >> "sample.txt" <-- Printing located line in sample.txt
located=false;
}
}' file2.dat

Replace fileds with AWK by using a different file as translation list

I am using awk in Windows. I have a script called test.awk.
This script should read a file and replace a certain filed (key) with a value.
The key->value list is in a file called translate.txt.
It's structure is like this:
e;Emil
f;Friedrich
g;Gustaf
h;Heinrich
i;Ida
In a simple example, my input file would be
e,111
f,222
g,333
h,444
i,555
..
so the output should be
Emil,111
Friedrich,222
Gustaf,333
Heinrich,444
Ida,555
..
The script I an having is using a user function key2value to do the replacement, but I don't succeed in giving this function another file translate.txt as a source. See my code:
{
FS=","
d=key2value($1)
print d "," $2
}
function key2value(b)
{
#this should use another file, not the currently processed one
FILENAME="translate.txt"
begin
{
FS=";"
if ($1=b)
{
return $2
}
end
}
Another thing, the FS is buggy to, it starts working from the second line only.
This simple one-liner will do the trick:
awk 'FNR==NR{a[$1]=$2;next}{print a[$1],$2}' FS=',|;' OFS=',' translate input
Emil,111
Friedrich,222
Gustaf,333
Heinrich,444
Ida,555
In script form:
BEGIN { # The BEGIN block is executed before the files are read
FS="[,;]" # Set the FS to be either a comma or semi-colon
OFS="," # Set the OFS (output field separator) to be a comma
}
FNR==NR { # FNR==NR only true when reading the first file
key2value[$1]=$2; # Create associative array of key,value pairs
next # Grab the next line in the first file
}
{ # Now in the second file, print looked up value and $2
print key2value[$1],$2
}
Run like:
awk -f translate.awk translate.txt input.txt
There are numerous error with your script, you should take a read of Effective AWK Programming.
Code for GNU sed (Windows quoting):
sed -r "s#(\S+);(\S+)#/^\1,/s/.*,(\\S+)/\2,\\1/#" file1|sed -rf - file2
Shell session:
>type file1 file2
file1
e;Emil
f;Friedrich
g;Gustaf
h;Heinrich
i;Ida
file2
e,111
f,222
g,333
h,444
i,555
>sed -r "s#(\S+);(\S+)#/^\1,/s/.*,(\\S+)/\2,\\1/#" file1|sed -rf - file2
Emil,111
Friedrich,222
Gustaf,333
Heinrich,444
Ida,555

sed: remove strings between two patterns leaving the 2nd pattern intact (half inclusive)

I am trying to filter out text between two patterns, I've seen a dozen examples but didn't manage to get exactly what I want:
Sample input:
START LEAVEMEBE text
data
START DELETEME text
data
more data
even more
START LEAVEMEBE text
data
more data
START DELETEME text
data
more
SOMETHING that doesn't start with START
# sometimes it starts with characters that needs to be escaped...
I want to stay with:
START LEAVEMEBE text
data
START LEAVEMEBE text
data
more data
SOMETHING that doesn't start with START
# sometimes it starts with characters that needs to be escaped...
I tried running sed with:
sed 's/^START DELETEME/,/^[^ ]/d'
And got an inclusive removal, I tried adding "exclusions" (not sure if I really understand this syntax well):
sed 's/^START DELETEME/,/^[^ ]/{/^[^ ]/!d}'
But my "START DELETEME" line is still there (yes, I can grep it out, but that's ugly :) and besides - it DOES remove the empty line in this sample as well and I'd like to leave empty lines if they are my end pattern intact )
I am wondering if there is a way to do it with a single sed command.
I have an awk script that does this well:
BEGIN { flag = 0 }
{
if ($0 ~ "^START DELETEME")
flag=1
else if ($0 !~ "^ ")
flag=0
if (flag != 1)
print $0
}
But as you know "A is for awk which runs like a snail". It takes forever.
Thanks in advance.
Dave.
Using a loop in sed:
sed -n '/^START DELETEME/{:l n; /^[ ]/bl};p' input
GNU sed
sed '/LEAVEMEBE/,/DELETEME/!d;{/DELETEME/d}' file
I would stick with awk:
awk '
/LEAVE|SOMETHING/{flag=1}
/DELETE/{flag=0}
flag' file
But if you still prefer sed, here's another way:
sed -n '
/LEAVE/,/DELETE/{
/DELETE/b
p
}
' file