Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm Using windows Xp.

I want to replace a word of length 200,000 characters and CMD has problems (the input line is too long). With Linux, does the terminal have same length problems?

If I replace many words with many other words, I can't replace a character with / because the CMD give an output error.

Is there a solution to bypass this problems?

share|improve this question
you want to replace a single 200,000 character word with something else? Are you sure you're doing this right? Certainly an atypical usage. Can we get an example? (NOT the whole 200k thing... be nice!) – lornix Jul 6 '12 at 4:43
I agree with @lornix, we probably need an example--maybe a toy example that shows the basic problem. I've successfully used sed on *NIX for text replacement in 141MB text files--inside a script. If you want help with sed, you'll definitely need to show us more. – ephsmith Jul 6 '12 at 4:49
Also, note, if you're looking to search or replace / with sed, you ought to use something else for an expression delimiter. Ex. Use s%search-pattern%replacement% instead of s/search-pattern/replacement/. Use a delimiter that is not in any of your expressions. – ephsmith Jul 6 '12 at 4:52
There is no obligation whatsoever to use a replacement character for /. You only need to escape each / in your search-pattern to \/. The feature is only a convenience when you know what your search pattern contains. However, if you have a dynamic/unknown search pattern, then you need to be aware of all characters which can end up in that search-pattern, and choose a character which won't occur as the delimiter, or escape each / to \/... btw: The delimiter is only significant in relation to the search-pattern. The data being searched can in no way clash with the delimiter. – Peter.O Jul 6 '12 at 9:59
thanks guys. Change delimiter is working, i'm using £ delimiter. 200k is very big, I wanted to see if CMD was unable to bear the load of work .. but I could perhaps create a batch file to automate the replacement tasks for big word in CMD? – user143822 Jul 6 '12 at 10:11
up vote 0 down vote accepted

Regarding your question about the Linux terminal command line. The answer is: It can handle much larger strings, which are pretty much dependant on how much free RAM you have. Eg. From the command line, I just ran printf '%s' with a 20,000,000 byte string; it had no problems. I also ran it through sed.exe (in a Linux wine environment); this worked fine, too... Whatever the command line limit may be, it also depends on what the program you are feeding such large strings can handle as their command input maximum size.

But it is an unusual/strange situation in which you would want to pass such large strings to a program, because you will at some point reach a maximum.

With sed and such large strings, you can do either of two things:

  1. Write your data to a file and use:

    sed "s@ab/cd@AB_CD@" my_data_file

  2. pipe (|) the output of a command to sed's standard input:

    my_data_app | sed "s@ab/cd@AB_CD@"

Note that sed itself may have some line length limit, but from memory, GNU sed's limit is quite high.

Also, instead of the default /, I have used @, but you can use pretty much anything you like.

share|improve this answer
Thanks Peter, i try use Sed on *nix – user143822 Jul 6 '12 at 9:58
@user143822. Be aware that *nix uses UTF-8 encoding for its textfiles. Windows does not. – Peter.O Jul 6 '12 at 10:13
Yes, my text file are UTF-8 encoding, japanese, chinese characters sometimes..but I'm forced to use windows CMD to launch command in sed or can i create a .bat file or something similar? – user143822 Jul 6 '12 at 10:23
I ran sed.exe in wine quite happily. The "sed.exe" I used was the GNU-for Windows version 4.2... I used a bat file which contained: sed.exe -n "s@a/@-=@gp" > file.out ... also,piping to stdin works: type | sed.exe -n "s@a/@-=@gp" > file.out – Peter.O Jul 6 '12 at 10:38
Ok..but if i want replace many words with other many words but that there are several files? What command should I use? <xxx.txt>yyy.txt command is good only for words located in 1 file not words that are located on different files. – user143822 Jul 6 '12 at 10:38

I am using sed for windows on win7. I ran into a problem where the replacement string was just too large. I was using this to replace sections of configuration files and after around 10,000 chars it would stop working and give an error of Unable to find specified file.

I ended up having to break the replace into 2 operations. The fist would fill in 1/2 of the data and end with ###ENDOFSECTION### and then then other replace would replace the ###ENDOFSECTION### with the rest of the data. Not sure if this is just a windows limit or not.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .