Inserting new line in txt file using CygWin

echo new line
linux add new line to file
sed insert line at end of file
bash script insert new line into file
new line in linux terminal
bash append to file
bash add blank line to file
echo add lines

I've got an XML table with the rows structured as follows:

<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>foobar</C3>
    <C4>foobar</C4>
    <C5>foobar</C5>
    <C6>foobar</C6>
    <C7>foobar</C7>
    <C8>foobar</C8>
    <C9>foobar</C9>
    <C10>foobar</C10>
    <C11>foobar</C11>
    <C12>foobar</C12>
</row>

What I would like to do is add a 13th column following the 12th, which needs to have an incremental number as its value to act as a primary key (starting from 1 and increasing by 1 per row, like X++ or etc). Worth noting is that there's about 20 million rows.

I have been trying to go about this using CygWin (Mintty), but I have absolutely no experience with it or anything Unix for that matter, it was simply the first thing I was recommended which was actually capable of loading these monstrous XML files.

For starters, I've tried using Sed to insert the 13th column using the following command:

-i -e 's/\/C12>/\/C12><C13><\/C13>/g' t1s.txt

My thought being that I'd replace the closing C12 bracket with itself + the 13th column. Instead it just replaced C12 with C13, as if the slashes hadn't been properly escaped.

There's probably a command to "insert" instead of "replace" which makes what I'm trying to do, look like complete madness, but I have no idea what that command would be and haven't had much luck searching. The closest I have come would be to use AWK instead, which I am up for, if this isn't feasible with Sed, but I have no clue how and it seems a tad harder as well.

What I hope to achieve is to change the document as follows (This is long, but apparently it didn't make sense without including everything. This question only concerns how to add the 13th column in each row, the other 12 are just context):

<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>foobar</C3>
    <C4>foobar</C4>
    <C5>foobar</C5>
    <C6>foobar</C6>
    <C7>foobar</C7>
    <C8>foobar</C8>
    <C9>foobar</C9>
    <C10>foobar</C10>
    <C11>foobar</C11>
    <C12>foobar</C12>
    <C13>1</C13>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>foobar</C3>
    <C4>foobar</C4>
    <C5>foobar</C5>
    <C6>foobar</C6>
    <C7>foobar</C7>
    <C8>foobar</C8>
    <C9>foobar</C9>
    <C10>foobar</C10>
    <C11>foobar</C11>
    <C12>foobar</C12>
    <C13>2</C13>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>foobar</C3>
    <C4>foobar</C4>
    <C5>foobar</C5>
    <C6>foobar</C6>
    <C7>foobar</C7>
    <C8>foobar</C8>
    <C9>foobar</C9>
    <C10>foobar</C10>
    <C11>foobar</C11>
    <C12>foobar</C12>
    <C13>3</C13>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>foobar</C3>
    <C4>foobar</C4>
    <C5>foobar</C5>
    <C6>foobar</C6>
    <C7>foobar</C7>
    <C8>foobar</C8>
    <C9>foobar</C9>
    <C10>foobar</C10>
    <C11>foobar</C11>
    <C12>foobar</C12>
    <C13>4</C13>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>foobar</C3>
    <C4>foobar</C4>
    <C5>foobar</C5>
    <C6>foobar</C6>
    <C7>foobar</C7>
    <C8>foobar</C8>
    <C9>foobar</C9>
    <C10>foobar</C10>
    <C11>foobar</C11>
    <C12>foobar</C12>
    <C13>5</C13>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>foobar</C3>
    <C4>foobar</C4>
    <C5>foobar</C5>
    <C6>foobar</C6>
    <C7>foobar</C7>
    <C8>foobar</C8>
    <C9>foobar</C9>
    <C10>foobar</C10>
    <C11>foobar</C11>
    <C12>foobar</C12>
    <C13>6</C13>
</row>

This goes on for several million rows...

Please note that the ONLY change is the addition of the 13th column and its value increasing for every row. That's ALL this question is about, the other 12 columns are just the surrounding structure.

if your input is really that regular and simple then chances are all you need is:

awk '{print} sub(/<C12>.*/,""){print $0"<C13>"++cnt"</C13>"}' file

but without something concrete to test against its a guess. Here's that doing what I think you are trying to do using a MCVE with 2 foobars per block instead of 12:

$ cat file
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
</row>

$ awk '{print} sub(/<C2>.*/,""){print $0"<C3>"++cnt"</C3>"}' file
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>1</C3>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>2</C3>
</row>
<row>
    <C1>foobar</C1>
    <C2>foobar</C2>
    <C3>3</C3>
</row>

Note that the numbers 12 and 13 can with a tweak be generated from your input, they don't need top be hard-coded in the script, but idk if that's necessary or not so I didn't bother doing it.

Appending a line break to an output file in a shell script, I'm betting the problem is that Cygwin is writing Unix line endings (LF) to the file, and you're opening it with a program that expects Windows  To edit an XML file, you'd better use an XML parser. Here is the command to insert the node C13 containing text foobar into your XML file using xmlstarlet (available in Cygwin): xmlstarlet ed --subnode "/row" -t elem -n C13 -v foobar ts1.txt ed: Edit/Update XML document--subnode "/row": add a node inside the row node-t elem: type is element

Using xmlstarlet and XSLT transform:

Given this XSLT file

$ cat f.xslt 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!-- Identity transform -->
    <xsl:template match="/ | @* | node()">
            <xsl:copy>
                    <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
    </xsl:template>
    <xsl:template match="/">
            <xsl:for-each select="/doc/row">
                    <xsl:copy>
                            <xsl:apply-templates select="@* | node()"/>
                            <C13><xsl:value-of select="position()"/></C13>
                    </xsl:copy>
            </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

And assuming the <row>...</row> are enclosed within the tag <doc>...</doc>, you can add the C13 node with its counting number by using this command:

xmlstarlet tr f.xslt file

tr means "transpose" and uses an XSLT file to modify the given file.

The XSLT mainly contains 2 blocks. One called the "identity" that acually leave the input as is. The second block looks for each <doc><row> and copies its content and add the <C13> node with the counting number.

Credits: here and here

How to put a newline special character into a file using the echo , You asked for using some syntax with the echo command: echo $'first line\​nsecond line\nthirdline' > foo. (But consider also the other answer you got.) The $'​. To start a new line of text or add spacing between lines or paragraphs of text in a worksheet cell, press Alt+Enter to insert a line break. Double-click the cell in which you want to insert a line break Click the location where you want to break the line. Press Alt+Enter to insert the line break.

how do I echo $something >> file.txt without carriage return?, txt , a new line will be append to the file. What if I want to append without a new line? share. In a similar way to the StreamReader class, you can pass the path of a text file to the StreamWriter constructor to open the file automatically. The WriteLine method writes a complete line of text to the text file. Start Visual Studio. On the File menu, point to New, and then click Project.

apt, It's actually quite easy with sed : sed -i -e '1iHere is my new top line\' filename. 1i tells sed to insert the text that follows at line 1 of the file; don't  I want to write a text file with a line feed or carriage return at the end of every line. I don't know how to code that in the put statement or the file statement; I can write the file OK it just dosn't have the line feeds in it. Thanks for the help. Jim

Cygwin 1.7.9 and DOS Line Endings, Add or edit the Cygwin shell's .profile file to export the SHELLOPTS environment variable to use files containing carriage return (\r) characters. text editor create a file, .bash_profile, in that home directory with the contents: Hi:) I have a large file ( couple thousand lines ) and I'm trying to add a character after the first word/space on each line. eg: First line of text Second line of text Third line of text Fourth line of text Fifth line of text I'd like to accomplish: First - line of text Second (8 Replies)

add newline in file after finding specific text, Hi All, I am tring to insert a newline with "/" in a text file whenever there is the text in school, in our Principles Of Operating System Class We are using Cygwin. $ sed '/5/ a #Next line is the 6th line, not this' sedtest.txt This is line #1 This is line #2 This is line #3 This is line #4 This is line #5 #Next line is the 6th line, not this This is line #6 This is line #7 This is line #8 This is line #9 This is line #10 sed - Inserting Lines in a File 1. Insert line using the Line number This will insert

Comments
  • Actually it's to show that there are 12 collumns that are all completely irrelevant to this question. I only want to add the 13th collumn, and this question only concerns that collumn. I will try to edit the question to reflect this.
  • @Ed Morton There, I edited the example to show all of the collumns. It's pretty massive now, but I hope it makes more sense. Please note once again that the first 12 collumns in every row aren't part of the question, and that the data that they contain is NOT IMPORTANT TO THE QUESTION, which is why I wrote "foobar" in their value.
  • Does it make sense now? I'm not exactly certain which part of this was unclear to begin with. I'm just trying to insert a collumn in an XML table and assign its value. I'd consider that to be extremely basic, conceptually, at least.
  • The post literally says "with the rows structured as follows" on the first line, which implies that this is an example of one of the 20 million rows, each of which I need to change by adding another collumn. I included the 12 foobar rows in case someone writes a script or etc that takes line numbers, indentation, and etc into account. Also, It sounds to me like you're contradicting yourself here. You're saying that I should cut off some of the collumns, yet accuse me of not defining the input/output exactly as expected. I literally have nothing else to add to either input or output.
  • As for the second one... the output. The first line below it literally says "This goes on for several million rows". So 6 is just a random number because I obviously can't post 20 million of them, yet I was told that the concept of the "counter" in the 13th row was hard to visualize (which I also don't understand, as I made clear that it was a primary key, and that's pretty much how they all look), so I showed it counting to 6. Could have been 4. Could have been 11.673.201. It doesn't matter, I showed where I wanted the 13th row to appear and how I wanted it's value to be assigned.
  • This is exactly what I am trying to do, and it is in fact that simple. Your solution is a lot simpler than the current one, so I will give it a spin. Also, I am fully aware that the amount of collumns isn't important in this solution, but I wasn't sure that this would be the case when I asked the question, that's why all 12 were there.
  • There's what you state in the question text "My real input has 12 columns..." and the minimal reproducible example you provide for us to test against "Here's a MCVE with 2 columns...". They do not have to be identical to get a good solution - just differentiate 1 instance from more-than-1 instances in the example by providing 2 or 3 instances in the more-than-1 case and state in the text what your real "more-than-1" value is.
  • Alright. Will approach it like that from now on. Having spent several years on the internet though, I will not be surprised if I one day encounter someone criticizing me with the exact opposite standpoint. Alas, you came through with the best possible solution I could have hoped for, so I will regard your viewpoint on question structure accordingly.
  • By the way, is there a way that I can alter this solution to make it write this change into the file instead of printing it in the console?
  • Okay, will go with the output file then. That makes it easier to test out on the actual files. Thanks again for the effort and patience. I will consider this a learning experience in multiple ways.
  • Thanks for the reply. The rows are not enclosed in doc tags, they are however enclosed in a table tag. To clarify, the entire document structure is a declaration line on top and then a table tag containing all of the rows. That's about it though. Is it possible to apply this solution in a way that accomodates this?
  • @Noobie Just replace the doc in the line <xsl:for-each select="/doc/row"> with the tag name that encloses the <row> in your XML file.
  • Alright, will this work if they are all enclosed in the same tag/parent, or do they need to have a different parent each? Might be a dumb question but I just wanna be sure. :) Thanks so much for your efforts by the way.