I'm trying to replace the text using the sed, but it's showing some error. Not getting where I'm getting wrong.
sed -i 's/process.env.REDIRECT_URI/http:\/\/test-domain.apps.io/\callback/g' input.txt
Have this :
process.env.REDIRECT_URI
Replace this with :
http://test-domain.apps.io
Try:
sed -i 's/process.env.REDIRECT_URI/http:\/\/test-domain.apps.io/g' input.txt
Notes:
The original command has a spurious string /\callback. All that was needed to make the code work was to remove it.
. is a wildcard. If you want to be sure that you are matching periods, they should be escaped:
sed -i 's/process\.env\.REDIRECT_URI/http:\/\/test-domain.apps.io/g' input.txt
Sometimes, its clearer if one doesn't have to escape /. One can use a separator of one's choice. For example, use #:
sed -i 's#process\.env\.REDIRECT_URI#http://test-domain.apps.io#g' input.txt
If you did want /callback in the output, use:
sed -i 's/process\.env\.REDIRECT_URI/http:\/\/test-domain.apps.io\/callback/g' input.txt
or:
sed -i 's#process\.env\.REDIRECT_URI#http://test-domain.apps.io/callback#g' input.txt
Suppose I have the bellow content in a file
file1.txt:
some text ...
...
begin
if somthing is true
some text here
...
fi
end
some text
I want to replace text between begin end including begin end with content of
another file
file2.txt:
while read line:
do
some code
done
After substitution the file1.txt should be like this
file1.txt:
some text ...
...
while read line:
do
some code
done
some text
You can try this sed
sed -e '/begin/,/end/!b' -e '/end/!d;r file2.txt' -e 'd' file1.txt
Sed print each line of the file if option -n is not specified.
Before printing, sed execute the script given by all the -e option given.
The command b in the script tell to sed to end the script at this point.
So the first -e command tell sed to end the script and print the line for all lines not in begin and end.
The second -e command tell sed to print the file file2.txt when it find the line with end.
The third -e command tell sed to delete (not print) the lines from begin to end.
Following awk may help you on same.
awk '/begin/{print;system("cat FIlE2.txt");next} 1' FIlE1.txt
Output will be as follows.
some text ...
...
begin
while read line:
do
some code
done
if somthing is true
some text here
...
fi
end
I have over 1000 text files where I need to remove the first 6 lines and also remove some boilerplate text from the bottom of each file.
I am trying this in one command in sed, if possible.
Here are the individual commands
sed -i.bak '/Some text as starting delimiter/,/Some text as an ending delimiter/d' My-File.txt
sed -e '1,6d' < MyFile.txt
Can these be combined ?
How so?
Thanks
Try this:
sed -i.bak -e '/Some text as starting delimiter/,/Some text as an ending delimiter/d' -e '1,6d' My-File.txt
or
sed -i.bak '/Some text as starting delimiter/,/Some text as an ending delimiter/d;1,6d' My-File.txt
Due to the know prob of mocha-lcov-mocha breaking file paths, I need to fix the current output paths that looks like this:
SF:Vis/test-Guid.coffee
SF:Vis/Guid.coffee
SF:Vis/test-Vis-Edge.coffee
SF:Vis/Vis-Edge.coffee
into
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
I'm not very good with sed, but I got it to work using:
mocha -R mocha-lcov-reporter _coverage/test --recursive | sed 's,SF:,SF:src/,' | sed s',SF.*test.*,SF:test//&,' | sed s',/SF:,,' | sed s',test/src,test,' | ./node_modules/coveralls/bin/coveralls.js
which is basically doing 4 sed commands in sequence
sed 's,SF:,SF:src/,'
sed s',SF.*test.*,SF:test//&,'
sed s',/SF:,,'
sed s',test/src,test,'
my question is if there is a way to do with this one sed command, or use another osx/linux command line tool
Initially put "src/" after every ":" and then if "test" is found on the line replace "src" with "test":
$ sed 's,:,:src/,;/test/s,src,test,' file
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
You could put all the sed commands in a file, one line per command, and just use "sed -e script". But if you just want it on a single command-line, separate with semicolons. This works for me:
sed 's,SF:,SF:src/,;s,SF.*test.*,SF:test//&,;s,SF:,,;s,test/src/,test,'
sed command
sed '\#test#!{s#SF:Vis/#SF:src/Vis/#g};\#SF:Vis/test#{s#SF:Vis/test#SF:test/Vis/test#g};' my_file
Here is an awk version:
awk -F: '/SF/ {$0=$1FS (/test/?"test/":"src/")$2}1' file
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
How it works:
awk -F: ' # Set field separator to ":"
/SF/{ # Does line start with "SF"?
$0=$1FS (/test/?"test/":"src/")$2 # Recreat String by adding "test" if line contains "test", else "src"
}
1 # Print all lines
' file # read the file
I have ending lines like
"true"/>
testname="A01
"true">
Prop>
testname="A02
Wanted to insert some text at the end of lines containing testname="A0...
I tried below option
sed -i 's/!>$/_\" enabled=\"true\">/g' file
sed -i 's/!>\\n/_\" enabled=\"true\">/g' file
However it is not reflecting in the file.
any tweak is needed for this. Help me out on this
You can use awk
awk '/="A0[0-9]+$/ {$0=$0" blabla"}1' file
"true"/>
testname="A01 blabla
"true">
Prop>
testname="A02 blabla
To update original file:
awk '/="A0[0-9]+$/ {$0=$0" blabla"}1' file > tmp && mv tmp file
Be carefull with sourroung space (code below don't use any space in pattern)
sed -i -e '/^testname="A[0-9]\{1,\}$/ a\
put some line\
to add\
here' YourFile
option g is not needed because you add a line and sed works (by default) line by line
Try the below GNU sed command,
sed -ri '/A0[0-9]+$/ s/(^.*testname=")(.*)$/\1\2_" enabled="true">/g' file
Example:
$ sed -r '/A0[0-9]+$/ s/(^.*testname=")(.*)$/\1\2_" enabled="true">/g' c
"true"/>
testname="A01_" enabled="true">
"true">
Prop>
testname="A02_" enabled="true">