1847253 Members
3980 Online
110263 Solutions
New Discussion

Re: UNIX script help

 
Robert_73
Occasional Advisor

UNIX script help

Hi! list,
How do I put this in a script
%s/\[//
to remove all [ in a file.

Thanks in advance
UNIXDUDE
8 REPLIES 8
Rodney Hills
Honored Contributor

Re: UNIX script help

sed -e 's/\[//' outfile

-- Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: UNIX script help

Hi Robert:

You could use 'tr':

# tr -d "[" < filename > filename.new

Regards!

...JRF....
A. Clay Stephenson
Acclaimed Contributor

Re: UNIX script help

Easy way:

perl -i -p -e 's/\[//g' file1 file2 ...

or cd to desired directory

perl -i -p -e 's/\[//g' x*
to edit all files that begin with x.

No temp files or input output redirection needed.



If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: UNIX script help

Hi again Robert:

I suppose I should have added if you add a suffix to the -i a backup file is also created for you automatically.

perl -i.save -p -e 's/\[//g' x*
to edit all files that begin with x.

THat would put the changes in files x1, x2, ...
and the backups would be x1.save, x2.save, ...
If it ain't broke, I can fix that.
H.Merijn Brand (procura
Honored Contributor

Re: UNIX script help

in perl tr is also faster than s, and you don't need the escape, because tr does not know of character classes (in a golf game you would use tr's alias y)

l1:/tmp 105 > cat xx.txt
blah[1]
l1:/tmp 106 > perl -pi -e 'tr/[//d' xx.txt
l1:/tmp 107 > cat xx.txt
blah1]
l1:/tmp 108 >
Enjoy, Have FUN! H.Merijn
Ian Dennison_1
Honored Contributor

Re: UNIX script help

Aha! You want to put the command in a script or build the command string in a script then run it?

To simply run the command, place it in a script as per the first reply.

If you want to build the search string in the script, then you will need to put a backslash before the backslash to get the shell to convert it to a non-special character.

eg sed 's/\\\[//g'

Share and Enjoy! Ian
Building a dumber user
MANOJ SRIVASTAVA
Honored Contributor

Re: UNIX script help

Hi Robert


cat filename | sed -e "1.$ s/\[//g" > new file



Manoj Srivastava
V. V. Ravi Kumar_1
Respected Contributor

Re: UNIX script help

hi try this,

sed 's/\[//g' (file name) > (new file name)

hope this helps
regds
ravi
Never Say No