I am trying to split txt file line by line and want to add tab. for first level is 1 tab, second level is 2 tabs indented and so on.I know how to do this if user add input line by line but I want to split txt file contents.  This is input file:  &lt;company&gt;&lt;name&gt;xyz&lt;/name&gt;&lt;name&gt;ABC PQR&lt;/name&gt;&lt;address&gt; &lt;line1&gt;G M Road&lt;/line1&gt;&lt;line2&gt;akurdi&lt;/line2&gt;&lt;state&gt;Maharashtra&lt;/state&gt; &lt;city&gt;Pune&lt;/city&gt;&lt;/address&gt;&lt;company&gt;   And I want output like this:  &lt;company&gt;     &lt;name&gt;xyz&lt;/name&gt;     &lt;name&gt;ABC PQR&lt;/name&gt;     &lt;address&gt;         &lt;line1&gt;G M Road&lt;/line1&gt;         &lt;line2&gt;akurdi&lt;/line2&gt;         &lt;state&gt;Maharashtra&lt;/state&gt;         &lt;city&gt;Pune&lt;/city&gt;     &lt;/address&gt; &lt;company&gt;   This is what i tried but its not working:  class lineSplit {  public static void main(String[] args) throws Exception {     BufferedReader br = new BufferedReader(new FileReader("input.txt"));     List&lt;String&gt; list = new ArrayList&lt;String&gt;();     String line = null;     String[] values;     while ((line = br.readLine()) != null) {         values = line.split("&gt;");          for (String str : values) {             list.add(str + "&gt;");         }      }     Iterator itr = list.iterator();     while (itr.hasNext()) {         System.out.println(itr.next());     }      br.close(); } }   How can i do this?
27515
I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this?  String unformattedXml = "&lt;tag&gt;&lt;nested&gt;hello&lt;/nested&gt;&lt;/tag&gt;"; String formattedXml = new [UnknownClass]().format(unformattedXml);   Note: My input is a String. My output is a String.   (Basic) mock result:    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;root&gt;   &lt;tag&gt;     &lt;nested&gt;hello&lt;/nested&gt;   &lt;/tag&gt; &lt;/root&gt;
73724
There is a standard command for file splitting - split.  For example, if I want to split a words file in several chunks of 10000 lines, I can use:  split -dl 10000 words wrd   and it would generate several files of the form wrd.01, wrd.02 and so on.  But I want to have a specific extension for those files - for example, I want to get wtd.01.txt, wrd.02.txt files.  Is there a way to do it?
41776
I'm sure there are many ways to do this: how can I count the number of lines in a text file?  $ &lt;cmd&gt; file.txt 1020 lines
24166
It took me a while to word this best.  Problem 1:  Is there a way to find lines in a text file that have the same string at the beginning and the same strings at the end?   Beginning and end strings not necessarily equal to each other.  Text between the two strings is not consistant in number of characters or number of hyphens.  A hyphen is in between notable strings (which I added to my large text file).  For example:  File:    AAAAA-tjhut-ghyut-FILE1.txt   BBB-fhuj-dnbhg-frt76-FILE2.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   CCCC-fjuk-dryy-fhuj-FILE3.txt   Lines to detect:    AAAAA-tjhut-ghyut-FILE1.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   Problem 2:  Is there a way to remove all but one of the lines containing the duplicate strings at the beginning and end from above?  Before:    AAAAA-tjhut-ghyut-FILE1.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   After:    AAAAA-tjhut-ghyut-FILE1.txt   Big thanks for any advice.  Note: I think using the occurances of the first hyphen and last hyphen can be used to locate the duplicate strings.
2306
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:  Host localhost     ForwardAgent yes   So "line then new line 'tab' then text" I think its a sensitive format.  I know you can do this:  cat temp.txt &gt;&gt; data.txt   But it seems weird since its two lines. Is there a way to append that in this format:  echo "hello" &gt;&gt; greetings.txt
19556
Yes,  but there must be a canonical or most efficient or most concise way. I'll add answers I know of and see what percolates to the top.  To be clear, the question is how best to read the contents of a file into a string. One solution per answer.
91763
I have have some code which adds new cells to a table and fills them with text boxes.   The way I've coded it so far works fine:          TableCell tCell1 = new TableCell();         TableCell tCell2 = new TableCell();         TableCell tCell3 = new TableCell();         TableCell tCell4 = new TableCell();         TableCell tCell5 = new TableCell();         TableCell tCell6 = new TableCell();         TableCell tCell7 = new TableCell();          TextBox txt1 = new TextBox();         TextBox txt2 = new TextBox();         TextBox txt3 = new TextBox();         TextBox txt4 = new TextBox();         TextBox txt5 = new TextBox();         TextBox txt6 = new TextBox();         TextBox txt7 = new TextBox();          tCell1.Controls.Add(txt1);         tCell2.Controls.Add(txt2);         tCell3.Controls.Add(txt3);         tCell4.Controls.Add(txt4);         tCell5.Controls.Add(txt5);         tCell6.Controls.Add(txt6);         tCell7.Controls.Add(txt7);          tRow.Cells.Add(tCell1);         tRow.Cells.Add(tCell2);         tRow.Cells.Add(tCell3);         tRow.Cells.Add(tCell4);         tRow.Cells.Add(tCell5);         tRow.Cells.Add(tCell6);         tRow.Cells.Add(tCell7);   As you can see there's basically 4 instructions getting repeated 7 times. I'm sure there has to be a way to accomplish this with just 4 lines of code within a FOR loop and having all the names dynamically assigned but I just can't seem to find anything that would point me in the direction of how to do it.  Something like the following is what I'm after:      for (int i = 0; i &lt; 6; i++)     {          TableCell tCell[i] = new TableCell();         TextBox txt[i] = new TextBox();         tCell[i].Controls.Add(txt[i]);         tRow.Cells.Add(tCell[i]);      }    Any help would be much appreciated.
26157
I've a file named addresses.txt :  address1@domain.com address2@domain.com address3@domain.com   I'd like to echo "address1@domain.com.html" using for loop. Here's what I did :  cat addresses.txt | while read address do echo "${address}.html" done   Here's the result :  .htmlss1@domain.com .htmlss2@domain.com .htmlss3@domain.com   Same result if I add a space between the variable and ".html"  What am I miss ?  Thanks
2343
I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited.  Is there a better/different way to read a file into a string in Java?  private String readFile(String file) throws IOException {     BufferedReader reader = new BufferedReader(new FileReader (file));     String         line = null;     StringBuilder  stringBuilder = new StringBuilder();     String         ls = System.getProperty("line.separator");      try {         while((line = reader.readLine()) != null) {             stringBuilder.append(line);             stringBuilder.append(ls);         }          return stringBuilder.toString();     } finally {         reader.close();     } }
62986
I have tried to get the 2 files output in a single file.  I have tried below command:  cat file1 file2   but here file2 data is appending to file1.  What I need is file1 1st line followed by file2 first line, and file1 second line followed by file2 second line etc.  Can anybody tell me how to do this in a simple way?
38533
Is there a simple way I can echo a file, skipping the first and last lines? I was looking at piping from head into tail, but for those it seems like I would have to know the total lines from the outset. I was also looking at split, but I don't see a way to do it with that either.
82863
How can I insert some text in specific lines of a file? What is the simplest method that I can use? (bash, sed, awk?) What I want to do might be simple, but I don't know where I should start. It takes too much time for me to try to do this manually (I have a lot of files that I have to change for Excel/Calc later use). Here is my input file example: 4.06 4.05 5.04 4.06 34.50 56.06 45.33 36.44  And I want something like this (insert text before line 1 and 5): Exp1     4.06 4.05 5.04 4.06 Exp2 34.50 56.06 45.33 36.44  How can I do this?
31096
I have a long line in equation. I want to split it properly in two or three lines. I've been using \begin{split} and \end{split}, but still it is not in good position. The equation's code is below.  \begin{equation} \begin{split} \frac{r(r-1)c_{0}}{z} + \\ &amp;  \sum_{0}^{\infty}[[(j+r)(j+r+1)c_{j+1} - [(j+r)(j+r-1) - l(l+1)]c_{j}] \\ &amp;= 0, \end{split} \end{equation}
106050
Hi I'm pretty new to Stack Overflow so I hope that I'm doing this correctly and that someone out there has the answer I need.  I'm currently coding a program in Java with Eclipse IDE an my question is this:  I need a snippet of code that does the following  It's supposed to get a .TXT file containing text and from that .TXT file  count the number of rows and print it,  count the number of words and print it,  count the number of characters and print it. And finally make a list of the top 10 words used and print that.  Allt the printing is done to system outprintln  I'm pretty new to Java and am having some difficulties.  Anyone out there who can provide me with these lines of code or that knows where i can find them? I want to study the code provided that's how I learn best=)  Thanks to all   Didnt find the edit button sorry...  I Added this to my question:  Hehe it´s an assignment but not a homework assignment ok i see well i could provide what i've done so far, i think im pretty close but it´s not working for me. Is there anything i have missed?  // Class    Tip   import java.io.*; import java.util.*;  class Tip {     public static void main(String [] args) throws Exception     {          String root = System.getProperty("user.dir");            InputStream is = new FileInputStream( root + "\\tip.txt" );         Scanner scan = new Scanner( is );          String tempString = "";         int lines = 0;         int words = 0;         Vector&lt;Integer&gt; wordLength = new Vector&lt;Integer&gt;();         int avarageWordLength = 0;          while(scan.hasNextLine() == true)         {                 tempString = scan.nextLine();                 lines++;         }          is.close();          is = new FileInputStream( root );         scan = new Scanner( is );          while(scan.hasNext() == true)         {                 tempString = scan.next();                 wordLength.add(tempString.length());                 words++;         }          for(Integer i : wordLength)         {                 avarageWordLength += i;         }         avarageWordLength /= wordLength.size();           System.out.println("Lines : " + lines);         System.out.println("Words : " + words);         System.out.println("Words Avarage Length : " + avarageWordLength);          is.close();          } }
18905
I've got a text file full of records where each field in each record is a fixed width.  My first approach would be to parse each record simply using string.Substring().  Is there a better way?   For example, the format could be described as:  &lt;Field1(8)&gt;&lt;Field2(16)&gt;&lt;Field3(12)&gt;   And an example file with two records could look like:  SomeData0000000000123456SomeMoreData Data2   0000000000555555MoreData       I just want to make sure I'm not overlooking a more elegant way than Substring().    Update:  I ultimately went with a regex like Killersponge suggested:  private readonly Regex reLot = new Regex(REGEX_LOT, RegexOptions.Compiled); const string REGEX_LOT = "^(?&lt;Field1&gt;.{6})" +                         "(?&lt;Field2&gt;.{16})" +                         "(?&lt;Field3&gt;.{12})";   I then use the following to access the fields:  Match match = reLot.Match(record); string field1 = match.Groups["Field1"].Value;
41225
Some people have asked about how to add multiple paragraphs (when \intertext and \shortintertext no longer work), a fix was proposed by Peter Grill :  He wrote:     My first thought was to hack apart the result of an align and dole   out the lines one-by-one wherever you want them. Alas, align   produces a list of considerable complexity and it is not worth the   effort to unravel it (if that's even possible).      So I made my own alignment environment that, as far as I can tell,   does what you'd want align* to do but produces a very simple list.   My macro \ALineAgain then picks that list apart and drops the lines   one at a time wherever you ask.      Because the lines are not printed where the alignment is actually   given, it makes no sense to have an AlignAgain*; it never prints   equation numbers. Instead, the \ALineAgain macro takes a star that   suppresses the addition of an equation number.   The problem with this code is that the equation numbers seem to be messed up once you try referencing them. Below, I've added only a few lines to Peter's code (notably: \numberwithin{equation}{section}, two section commands, and then a single line at the very end that attempts to reference the two equations).   Rather than printing (1.1) and (1.2), it prints (1.1) and (1.1). There seems to be something wrong with the equations portion of the code. Can anybody spot the problem?   \documentclass{article} % \usepackage{showframe} \usepackage{amsmath}  \numberwithin{equation}{section}  % It's like {align}, but simple! \newenvironment{SimpleAlign} {%  \let\\=\cr  \tabskip 0pt plus 1fil\relax  \halign\bgroup   &amp;\hfil$\displaystyle ##$\tabskip 0pt\relax   &amp;$\displaystyle {}##$\hfil\tabskip 0pt plus 1fil\relax\cr } {%  \crcr\egroup }  % Saves its contents typeset like SimpleAlign.  You can give it a name. \newenvironment{AlignAgain}[1][AlignBox] {%  \expandafter\let\expandafter\AlignAgainBox\csname #1\endcsname  \setbox\AlignAgainBox=\vbox\bgroup\SimpleAlign } {%  \endSimpleAlign\egroup  \ReBox\AlignAgainBox } \newbox\AlignBox  % Prints the lines from {AlignAgain} one-by-one, with or without equation numbers as starred.  If you named the {AlignAgain}, use it here. \makeatletter \newcommand\ALineAgain{%  \@ifstar   {\AlignAgainStartrue\@ALineAgain}   {\AlignAgainStarfalse\@ALineAgain}% } \newcommand\@ALineAgain[1][AlignBox]{%  \expandafter\let\expandafter\AlignAgainBox\csname #1\endcsname  \begin{equation*}   \PutFirstBox\ifAlignAgainStar\AlignAgainBox  \end{equation*} } \makeatother \newif\ifAlignAgainStar  % Reboxes a box in the format: {[1][2]...[n]} -&gt; {[1]{[2]{...}[n]}} % {...} = \hbox \newcommand{\ReBox}[1] {%  % To enter internal vertical mode, where \lastbox is valid  \setbox0=\vbox{%   \unvbox#1\relax   \global\setbox#1=\hbox{\hbox{}}   \loop    \unskip    %\box0 is the box currently at the end of #1    \setbox0=\lastbox   \ifvoid0\relax   \else    \global\setbox#1=\hbox{\box0\box#1}%   \repeat  }% }  % Puts the first box in a nested-box list into the display, saving the rest back in #1 % Optionally places an equation number \newcommand{\PutFirstBox}[2]{%  \hbox to \displaywidth{%   \unhbox#2\relax   \global\setbox#2=\lastbox   \setbox0=\lastbox   \unhbox0%   #1\else\refstepcounter{equation}\llap{(\theequation)}\fi  }% }  \begin{document}  \section{First section} \subsection{A subsection} Hello \begin{AlignAgain}  a &amp;= b &amp; c &amp;= d \label{firsteqn} \\  f(x) &amp;= g(x) &amp; h(x) &amp;= e^{-1/x^2} \label{myeqn} \\ \end{AlignAgain}  This is a paragraph with a lot of redundant words whose only purpose is to extend it onto a new line or two so it's dramatic when I display this equation: \ALineAgain and then follow it with a bunch more text.  And also a new paragraph, just for good measure.  I wonder if the two equations will line up?  I sure hope so! \ALineAgain This is sort of fun.   Can we reference equations by section? Try (\ref{firsteqn}) and (\ref{myeqn}).  \end{document}
49537
Here is an example of using cut to break input into fields using a space delimiter, and obtaining the second field:  cut -f2 -d' '   How can the delimiter be defined as a tab, instead of a space?