I have a text file that has numerous instances of the character '^'. I need all those instances replaced with a '['.
I prefer to use cygwin but would use windows command prompt if there is a direct way to do this. My initial instinct was to use Access (no other DB installed) to use the 'replace' function, but as I'm connecting using Jet - apparently this is not possible as per Exception when trying to execute "REPLACE" against MS Access
What's the cleanest way to achieve this?
Try this:
sed -i 's/\^/\[/g' myfile
I've tested this: sed 's/\^/[/g' //by the command line
Related
I got some Postgres table dumps from somebody using pgAdmin3 on Windows. (Blech.) First of all, it has a whole bunch of extra crap at the top of the file that I've had to get rid of-- things like "toc.dat" without comments, etc.
I've resorted to editing them by hand to get them in workable format to be imported, because as it stands they are somewhat garbled; for the most part I've succeeded, but when I open them in emacs, for example, they tend to be littered with the following character:
^#
and sometimes just alot of:
###
I haven't figured out how to remove them using sed or awk, mainly because I have no idea what they are (I don't think they are null characters) or even how to search for them in emacs. They show up as red for 'unprintable' characters. (Screenshot above.) They also don't seem to be printed to the terminal when I cat the file or when I open it in my OS X Text editor, but they certainly cause errors when I try to import the file in to postgres using
psql mydatabase < table.backup
unless I edit them all out.
Anybody have any idea of a good way to get rid of these short of editing them by hand? I've tried in place sed and also tried using tr, but to no effect-- perhaps I'm looking for the wrong thing. (As I'm sure you are aware, trying to google for '^#' is futile!)
Just was wondering if anybody had come across this at all because it's going to eat at me unless I figure it out...
Thanks!
Those are null characters. You can remove them with:
tr -d '\000' < file1 > file2
where the -d parameter is telling tr to remove characters with the octal value 000.
I found the tr command on this forum post, so some credit goes to them.
I might suggest acquiring access to a Windows machine (never thought I'd say that), loading the original dumps they gave you, and exporting in some other formats to see if you can avoid the problem altogether. Which to me seems safer than running any for of sed or tr on a database dump before importing. Good luck!
I frequently find myself searching for paths such as /a/b/c/file.txt in files (log files, scripts, etc.), using vi editor. This is usually on some server machine, to which I connect using SSH, so I'm looking for solutions which don't require a GUI editor.
It's painful to have to escape all those /s every time I want to search for string such as a file path.
Is there any alternative to using / while searching in vi? Or, is there some setting that will allow me to set this search character to something else? (even if it sets it only for the active session)
I know I can use grep, but a solution in vi would be nice.
You can use the ? character instead of /, But the only difference is /will do a forward search from top to bottom where as ? will search from last to first line.
You can use another delimiter, such as #:
:s#search#replace#g
Test
Given a file with this content:
hello this is me/you
I type:
:s#me/you#otherthing#g
And now the text is:
hello this is otherthing
You could try :cmap <C-/> \/
I know that this is quite an easy thing for any advanced Vim programmer, but I have been trying to find a solution for a couple of hours now.
In my results file, there are certain lines like:
/Users/name/Project/Task1/folder1 : INFO : Random Info message
Here, /Users/name/Project/Task1/folder1 is my pwd i.e present working directory.
I want to replace all the occurrences of my pwd above in the file with 'USER'. How can I do that?
:%s#/Users/name/Project/Task1/folder1#USER#g
or
:%s#<C-r>=getcwd()<CR>#USER#g
If I understand you correctly you can simply use the search and replace functionality and escape the / character like this:
:%s/\/Users\/name\/Project\/Task1\/folder1/USER/
If you need to replace multiple current working directories (and thus want to have the pwd to be dynamic) it is probably easier to use something like sed:
sed "s~$(pwd)~USER~" < file
Note that the ~ is used as a delimiter for the command instead of the /, this way we do not need to escape the / in the path.
I often find myself using something like:
sed -ri 's/<\/(abc|def ghi|j klm)>//g' someFile.html
to perform substitutions on multiple possible patterns, in this case, a closing html tag to be deleted, saving me the time and effort to do this three separate times for three closing tags I want deleted.
Is there a way to do this using substitute on vim's cli? I haven't yet found a way to do it, but it would be more efficient than going to a terminal cli or running sed from within vim if it could be done natively instead.
Yes, you don't need to use an external program at all:
:%s#</\(foo\|bar\|baz\)>##g
You can use the silent ! command to silently execute shell commands from inside vim:
:silent !sed -ri 's/<\/(abc|def ghi|j klm)>//g' %
This will execute the command in the shell silently (it won't take you away from vim to see any shell output). The % means the current buffer name. Vim will then notify you that the file you are editing has been changed and will ask you if you want to load the changes, press l for load and the new changes from the sed shell command will appear.
I've got a large SQL dump 250MB+ and I need to replace www.mysite with dev.mysite. I have tried nano and vi for doing find/replace but both choke. Nano can't even open it, and vi has been doing the find/replace now for an hour.
Anyone know of a tool on *nix or windows systems that does fast Find/Replace on large files?
sed -i 's/www\.mysite/dev.mysite/g' dump.sql
(requires temporary storage space equal to the size of the input)
Search/replace on a SQL dump is not a good idea
They aren't text files
SQL syntax errors are easily introduced
They contain very long lines sometimes.
What you should do is load it into a non-production database server, run the appropriate UPDATE statements then dump it again. You can use the REPLACE function in MySQL for this.
you need sed
example
sed -e "s/www.mysite/dev.mysite/g" your_large_sql
alternatively, import the sql into database, then use replace to replace for matched strings