How do I strip the characters from this string? - ruby

"/home/chief/project/public/system/uploads/000/000/001/original/1/1.flv
to this:
/system/uploads/000/000/001/original/1/1.flv

str = "/home/chief/project/public/system/uploads/000/000/001/original/1/1.flv"
chopped = str.sub(/.*\/public/, "") #=> "/system/uploads/000/000/001/original/1/1.flv"
This will remove everything to the left of public (including /public). This way your code isn't specific to one location, but rather it is more portable in that you can have anything in front of /public, and it will still strip the characters.

s = "/home/chief/project/public/system/uploads/000/000/001/original/1/1.flv"
s.sub("/home/chief/project/public", "")
This should do the trick.

You should specify in which language.
Using sed is trivial.
echo "\"/home/chief/project/public/system/uploads/000/000/001/original/1/1.flv" | sed -e 's-\"/home/chief/project/public--'

Related

Dynamically replace string value in configuration file

I'm building a shell script to install apache-airflow and update some configurations for local development.
I would like to replace the value of property dags_folder. How can I replace it using shell script?
Here is what I've tried:
if [[ "$OSTYPE" == "darwin"* ]]; then
SED_EXTRA=" "
else
SED_EXTRA=""
fi
sed -i${SED_EXTRA}'' "s,dags_folder = ,dags_folder = banana,g" ~/airflow/airflow.cfg
It's almost working but I want to replace entire value. Is possible?
Don't use sed at all. Use ed, which won't require OS-specific changes to the invocation.
You want to match the entire line, not just the name = prefix, so add .* to the regular expression.
ed ~/airflow/airflow.cfg <<EOF
s,dags_folder = .*,dags_folder = banana,g
wq
EOF
Match up until the end of the line with .* regex
sed -i${SED_EXTRA}'' 's,dags_folder = .*,dags_folder = banana,g' ~/airflow/airflow.cfg
try this one if I understand your issue correctly :)
var1="dags_folder ="
var2="dags_folder = banana"
sed -i 's/'"$var1"'/'"$var2"'/g' path to your cfg file

Wants to echo new line character

I wants to echo \n like in this way using ruby language:
`echo "array = text.split(/\n/)" > demo`
and demo file should contain output in this way:
array = text.split(/\n/)
But instead to above i am getting in this way, \n produce new line in real which i dont want:
array = text.split(/
/)
Please let me know any options, I already tried backslash and ^ but none of this useful.
If you want to stick with regexps, double-backslash your \n
`echo "array = text.split(/\\n/)" > demo`
You could use split("\n") instead of a regexp.
There are 6 backslash needed here, Like this:
`echo "array = text.split(/\\\\\\n/)" > demo`
This produce excellent results.

how to use bash to find text string like--> "PROVISIONING_PROFILE[sdk=iphoneos*]" <--this string has many special characters

As the title says, i'd like to find a string in a text, but the string is special.
The text may like this:
{
PREBINDING = NO;
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = 2;
}
( Additionally, if you are a ios developer, you may know that the text example is from XXX.xcodeproj/project.pbxproj )
I need to find and delete this line of text:
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";
In this line, it has quotes("), has brackets([ ]), has star(*), also has space( ). When i try to find this line using code below:
provisionStr="22B333C5-6D7A-4A76-81F2-12A045DAE44C"
fileDir=project.pbxproj
totalLineStr=`grep $provisionStr $fileDir`
sed -i "" "s#$provisionStr##" $fileDir
This bash code does not work. Please help me, i'm just beginning to learn to use bash.
sed substitute with nothing:
s(for substitute)/string_to_match/new_string_or_empty_to_remove/g(for global/all document)
sed 's/\"PROVISIONING_PROFILE\[sdk=iphoneos\*\]" = \"22B333C5-6D7A-4A76-81F2-12A045DAE44C\";//g'
escape double quotes, asterix and brackets using \
It sounds like all you want to do is:
grep -v '"22B333C5-6D7A-4A76-81F2-12A045DAE44C"' file
If you ever did want to search for a string in a file instead of search for matches to an RE in a file, then you'd just do this, for example:
$ awk -v str='"PROVISIONING_PROFILE[sdk=iphoneos*]"' 'index($0,str)' file
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";
$ awk -v str='"PROVISIONING_PROFILE[sdk=iphoneos*]"' '!index($0,str)' file
{
PREBINDING = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = 2;
}
Do not try escaping all the RE metacharacters and then do an RE comparison as that makes no sense. If you want to look for an RE then do an RE comparison but if you want to look for a string do a string comparison, it's that simple.

sed single quotes

I've been banging into escaping single quote's problem using SED (Bash shell).
I need to make
$cfg['Servers'][$i]['password'] = '';
into
$cfg['Servers'][$i]['password'] = 'mypassword';
What I've tried is:
sed -i "s/$cfg['Servers'][$i]['password'] = '';/$cfg['Servers'][$i]['password'] = '$rootpassword';/g" /usr/share/phpmyadmin/libraries/config.default.bak
Which ends up really jumbling the line.
$cfg['Servers'][$i]['password['Servers'][]['passsword'] = 'mypassword'
I've tried the '\'' to escape single quotes and I think everything else under the sun but just can't get it quite there.
can anyone point to my probable obvious mistake?
Thank you.
instead of escaping, you can use \x27 for single quote. this works not only for sed, for awk etc. as well.
see test:
kent$ cat tt
$cfg['Servers'][$i]['password'] = ''
kent$ sed -r 's/(\$cfg\[\x27Servers\x27\]\[\$i\]\[\x27password\x27\] \= \x27)\x27/\1mypassword\x27/g' tt
$cfg['Servers'][$i]['password'] = 'mypassword'
note that, the sed line may not be the best solution for your problem, e.g. put the whole string to "s/../" may not be necessary. However, I just want to show how the \x27 worked.
$ sed -i "s/\$cfg\['Servers'\]\[\$i\]\['password'\] = '';/\$cfg['Servers'][\$i]['password'] = '\$rootpassword';/g" file
Try this:
sed -i "/Servers.*password.*''$/s/''/'foo'/" /path/to/your/testfile
Match a line that contains "anything..Servers..anything..password..anything..''" (end with '') and on that line replace '' with 'foo'
This can match more than one lines, but only the first occurance will be changed.
Test it, it's most probable that .. Servers .. password .. '' is only on one line.
Or you can just escape everything.

How to replace multiple newlines in a row with one newline using Ruby

I have a script written in ruby. I need to remove any duplicate newlines (e.g.)
\n
\n
\n
to
\n
My current attempt worked (or rather not) using
str.gsub!(/\n\n/, "\n")
Which gave me no change to the output. What am I doing wrong?
This works for me:
#!/usr/bin/ruby
$s = "foo\n\n\nbar\nbaz\n\n\nquux";
puts $s
$s.gsub!(/[\n]+/, "\n");
puts $s
Use the more idiomatic String#squeeze instead of gsub.
str = "a\n\n\nb\n\n\n\n\n\nc"
str.squeeze("\n") # => "a\nb\nc"
You need to match more than one newline up to an infinite amount. Your code example will work with just a minor tweak:
str.gsub!(/\n+/, "\n")
For example:
str = "this\n\n\nis\n\n\n\n\na\ntest"
str.gsub!(/\n+/, "\n") # => "this\nis\na\ntest"
are you sure it shouldn't be /\n\n\n/, "\n" that what you seem to be wanting in your question above.
also, are you sure it's not doing a windows new-line "\r\n"?
EDIT: Additional info
Per Comment
"The amount of newlines can change. Different lines have between 2 and 5 newlines."
if you only want to hit the 2-5 lines try this
/\n{2,5}/, "\n"
Simply splitting and recombining the lines will give the desired result
>> "one\ntwo\n\nthree\n".split.join("\n")
=> "one\ntwo\nthree"
Edit: I just noticed this will replace ALL whitespace substrings with newlines, e.g.
>> "one two three\n".split.join("\n")
=> "one\ntwo\nthree"
First check that this is what you want!
Simply calling split will also trim out all of your whitespace.
You need to pass \n to split
>> "one ok \ntwo\n\nthree\n".split(/\n+/).join("\n")
=> "one ok \ntwo\nthree"
Additionally, also works with
spaces on blank lines
n number of back to back blank lines
str.gsub! /\n^\s*\n/, "\n\n"
where,
\n is of course newline
\s is space
denotes 1 or more spaces along when used after \s
Try This It Worked for me:
s = test\n\n\nbar\n\n\nfooo
s.gsub("\n\n", '')
Ruby needs the backslashes escaped differently than you have provided.
str.sub!("\\\\n+\\\\n","\\\\n")
http://www.ruby-forum.com/topic/176239

Resources