LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Join lines in text file (http://www.linuxquestions.org/questions/linux-general-1/join-lines-in-text-file-457672/)

vidyashankara 06-23-2006 03:58 PM

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.

jschiwal 06-23-2006 04:13 PM

Using sed or awk would probably be your best bet.

vidyashankara 06-23-2006 04:36 PM

Quote:

Originally Posted by jschiwal
Using sed or awk would probably be your best bet.

Do you know the exact command to use it?

druuna 06-23-2006 05:15 PM

Hi,

Here's one way:

Code:

#!/bin/bash

awk ' BEGIN { cntr = 1 }
{
  printf("%s ",$0)
  if ( cntr == 3 ) {
    printf("\n")
    cntr = 0
  }
  cntr++
}
END {
  rest = int(cntr)
  if ( rest != 1 ) {
    printf("\n")
  }
}
' infile

Hope this helps.

vidyashankara 06-23-2006 05:23 PM

Quote:

Originally Posted by druuna
Hi,

Here's one way:

Code:

#!/bin/bash

awk ' BEGIN { cntr = 1 }
{
  printf("%s ",$0)
  if ( cntr == 3 ) {
    printf("\n")
    cntr = 0
  }
  cntr++
}
END {
  rest = int(cntr)
  if ( rest != 1 ) {
    printf("\n")
  }
}
' infile

Hope this helps.


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!!!

spirit receiver 06-23-2006 05:41 PM

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
Now that I read your post again, I realize that, instead of looking at the first field, you always want to group three consecutive lines. That should be a little easier:
Code:

sed '1~3 {N;N;s/\n/ /g}' filename

druuna 06-23-2006 05:47 PM

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.

vidyashankara 06-23-2006 08:33 PM

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 :)

Tinkster 06-23-2006 09:15 PM

And here's a slightly lazier version of druuna's awk :}
Code:

#!/bin/bash
awk '
{
  printf("%s ",$0)
  if ( NR%3 == 0 ) printf("\n")
}
END {
  if ( NR%3 != 0) printf("\n")
}
' $1


jschiwal 06-25-2006 03:26 AM

Quote:

Time to learn awk scripting
You may have a "gawk-doc" package available. It includes a 10 page AWK cheat sheet, and a book "GAWK: Effective Awk Programming".

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.

yingted 12-21-2009 03:17 PM

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 07:27 AM.