In one script sh file these lines are present. I know we can do it using sed, but please let me know the way. I can use any suitable command.
BEFORE:
export HOME=${INSTALLROOT}/Subsystem
cd ${INSTALLROOT}
AFTER:
I want to add few lines after this string matches - export ASE_HOME
export HOME=${INSTALLROOT}/Subsystem
cd ${HOME}/tmp # added
rm -rf packed* # added
cd ${INSTALLROOT}
You can use this sed,
sed '/export HOME=/a cd ${HOME}/tmp # added \n rm -rf packed* # added' yourfile
man sed:
a \
text Append text, which has each embedded newline preceded by a backslash.
Related
I need to write a bash script that will first combine a few files into one and then replace all $ signs with string jQuery. I'm doing well till the replacing:
#!/usr/bin/env bash
# Run this script in this directory to compile the fixed query widget
# where we'll write the output to
OUT="compliled.js"
# path to dependencies
# combine all the dependencies into a single file in the right order (without jquery)
cat first_component.js <(echo) \
second_component.js <(echo) \
third_component.js <(echo) \
> $OUT
#replace all $ with jQuery
sed -i 's/$/jQuery/g' $OUT
But this way the result is jQuery string at the end of each line and all $'s untouched. Could someone please explain me what is going on here and maybe how to fix it?
$ is a special character denoting the end of each line in regular expressions, so you should escape it with a backslash:
sed -i 's/\$/jQuery/g' $OUT
^^
I'm using the Perl rename tool to write a bash script that looks for any files in a path that has spaces in it (It does other stuff but trying to keep this simple). I'm using the -c switch to transform to lower case and a regular expression to change all spaces to dashes. The command completes with no errors.
rename -f -X -c 's/[ ]+/-/g' /Volumes/data/Users-Links/adrz/test-site/with\ spaces/*
The above transforms the files to lower case but does not replace spaces with dashes. I then tried with the -e switch in front of the expression.
rename -f -X -c -e 's/[ ]/-/g' /Volumes/data/Users-Links/adrz/test-site/with\ spaces/*
and get ...
Can't rename '/Volumes/data/Users-Links/adrz/test-site/with spaces/hello world.txt' to '/volumes/data/users-links/adrz/test-site/with-spaces/hello world.txt': No such file or directory
It seems to be acting on the directory name that has spaces as well as the files inside the directory. To note, If the path does not have spaces in it, it works fine.
I've tried:
Using double quotes, single quotes and / to escape spaces in path.
I've tried using the Subst and Subs-all commands with same errors
I've been searching for weeks and I cannot work it out. Is this a bug in the tool or my mind?
Thanks.
Unless you're also trying to change the directory, you could use any of the following:
Run rename from the directory in which the files reside.
sh -c 'cd /Volumes/.../with\ spaces; rename -f -X -c -e "s/[ ]+/-/g" *'
Only match spaces that aren't followed by a slash (/).
rename -f -X -c -e 's{[ ]+(?!.*/)}{-}sg' /Volumes/.../with\ spaces/*
Break the path into its components and only operate on the file name portion.
rename -f -X -c -e '
use Basename qw( dirname basename );
$_ = dirname($_) . "/" . basename($_) =~ s/[ ]+/-/gr;
' /Volumes/.../with\ spaces/*
This is overkill in this situation, but it could be useful in situations where a simple modification (as in #2) isn't possible.
how is it possible to create a file with a name similar to "\?$*’KzUmi’*$?\" from terminal? I have tried to escape it using double quotes and \ but it didn't work.
Just add single quotes, for example:
$ mkdir '?$*’KzUmi’*$?'
To cd into it could then use:
$ cd '?$*’KzUmi’*$?'
or by pressing tab probably your shell would escape it to:
$ cd \?\$\*’KzUmi’\*\$\?
There is a file #A.py# that appears to be a copy of the original A.py in the same directory - when I try rm, I get the following:
rm: missing operand
What does the ## notation mean? How did this file appear?
Add quotes around:
rm "#A.py#"
Without quotes it's interpreted as a beginning of the comment
You could also escape the #:
$ touch \#rmme
$ ls|grep \#
#rmme
$ rm \#rmme
Like mention in other answers by using quotes should work:
rm "#A.py#"
Also this:
rm \#A.py\#
To remove all:
rm \#*
And just in case check the option --
The rm command supports the -- (two consecutive dashes) parameter as a delimiter that indicates the end of the options. This is useful when the name of a file or directory begins with a dash or hyphen. For example, the following removes a directory named -dir1
rm -- -filename
I tried
rm -r #*
and
rm #*
But it just outputs this message:
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
What's the problem?
# is a shell comment. You'll need to quote it, like so:
rm '#'*
Note that the hash is in quotes and the glob is outside the quotes.
rm \#*
should do the trick for you. Remember # has got special meaning in the shell, it starts a comment.
To quote
Lines beginning with a # (with the exception of #!) are comments and
will not be executed.
Comments may also occur following the end of a command.
&
escape [backslash]. A quoting mechanism for single characters.
\X escapes the character X. This has the effect of "quoting" X,
equivalent to 'X'. The \ may be used to quote " and ', so they are
expressed literally.
Had you have files 'file1,'file2 & 'file3, to delete them you would have used :
rm \'file* #Comment : This deletes all the files starting with 'file
Reference:TLDP note on special characters
This command will list all the file starting with # and feed them to rm:
ls . |grep "^#.*" |xargs rm -rf