Join lines in text file
Lets say i have the following text file
1 A B C 1 D F G 1 X Y Z 2 A C S 2 F A A 2 D S R is there a command for me to get the following output? 1 A B C 1 D F G 1 X Y Z 2 A C S 2 F A A 2 D S R Basically it joins every 3 lines into one line. |
Using sed or awk would probably be your best bet.
|
Quote:
|
Hi,
Here's one way: Code:
#!/bin/bash |
Quote:
you are supposed to write all that on command line??? sorry, i know nothing about Awk. if you are supposed to save it to a file, let me know the command for including the script too! Thanks a LOT!!! |
The following will work, but I think I'd prefer Perl to do this.
Code:
for INDEX in $(cut -f1 -d" " filename| uniq); do grep "^$INDEX" filename | tr '\n' ' '; echo; done Code:
sed '1~3 {N;N;s/\n/ /g}' filename |
Hi,
It's a script that has the input file included (appropriately called infile :) ). Save it to file (name could be tripplejoin for example) and make it executable (chmod 755 tripplejoin). ./tripplejoin will run the 'program'. But........ The solution spirit receiver came up with (the first example) is probably a better solution for you. Mind you: The sed command (spirit receiver's second example) will only work if there are sets of 3 lines, if the last set is 2 or 1 line, that part will fail. |
Wow, that sed command did the work! thanks Spirit!
Drunna : I was speaking to my brother about the program. He was telling me you can make such scripts to do a lot of things! Time to learn awk scripting :) |
And here's a slightly lazier version of druuna's awk :}
Code:
#!/bin/bash |
Quote:
If you obtain the source for the gawk package, you can use the documentation source to generate a PostScript or PDF or DVI version of the info source, which is also very good. Often there is a make target to do that automatically, with the documentation in the <packagedirectory>/doc/ directory. Note: You may have used: ./configure make sudo make install to install a package. There often is another make target to generate the documentation: make pdf or make ps will generate a pdf version of the documentation. There isn't always this target, and sometimes it is called PDF or pdfdoc. For some packages there is a "make html" target to generate browsable documentation. Since, gawk is already installed, you don't need to perform the "sudo make install" step. Also, if you want to print out the man pages for sed or awk, I would recommend doing it this way: man -t sed | lpr This will use the groff system to generate a print worthy version of the manpages. |
For anyone that sees this page on Google
NVM I thought you wanted to concatenate all the lines
Should have read first post |
All times are GMT -5. The time now is 12:48 PM. |