Adding data to text file with SED without changing file size

I have text files where I need to add 1 character to the beginning of each line of the file. On Windows, I found that a quick way to do this is to install Cygwin and use the following command, which adds the letter N to each line of the file:

$ sed 's/^/N/' inputFile.txt > outputFile.txt

      

What struck me as odd is that after adding a new character to the beginning of each line, the file size barely changed. I checked this further to see if I can recreate the problem with the following steps:

  • Created a text file called "Test.txt" which had 10,000 lines with the word "TEST" on each line.
  • Created a text file TestWithNPrefix.txt which had 10,000 lines with the word "NTEST" on each line.
  • Executed the following command to create another file with 10,000 lines "NTEST"

    $ sed 's/^/N/' Test.txt > "SEDTest.txt"    
    
          

results

"Test" and "SEDTest" were almost the same size, while "TestWithNPrefix" was 10KB larger.

Test = 59,998 Bytes;  SEDTest = 59,999 Bytes;  TestWithNPrefix = 69,998 Bytes

      

When I ran the "fc" command on the command line, it returned that there was no difference between "SEDTest" and "TestWithNPrefix". "FC" between "SEDTest" and "Test" returns "Resync Filed. Files too different".

Can someone please help me understand what is causing these file size inconsistencies?

EDIT: I created files "Test.txt" and "TestWithNPrefix.txt" in UltraEdit. I just typed the word "TEST" / "NTEST", then copied and pasted it 10,000 times.

+3


source to share


1 answer


Not an answer, but a comment with formatting:

It seems that you are running into some strange situation where the DOS and Unix lines end. I have to ask: how do you create files? I would expect 10,000 lines of "TEST \ r \ n" to be exactly 60,000 bytes rather than 59,999



On Linux (I don't have access to cygwin environment at the moment): '

$ yes $'TEST\r' | head -n 10000 > Test
$ ll Test
-rw-r--r-- 1 jackman jackman 60000 Jan  8 13:06 Test
$ sed 's/^/N/' Test > SEDTest
$ ll *Test
-rw-r--r-- 1 jackman jackman 70000 Jan  8 13:06 SEDTest
-rw-r--r-- 1 jackman jackman 60000 Jan  8 13:06 Test

      

+2


source







All Articles