LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-23-2006, 03:58 PM   #1
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Rep: Reputation: 15
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.
 
Old 06-23-2006, 04:13 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Using sed or awk would probably be your best bet.
 
Old 06-23-2006, 04:36 PM   #3
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal
Using sed or awk would probably be your best bet.
Do you know the exact command to use it?
 
Old 06-23-2006, 05:15 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 06-23-2006, 05:23 PM   #5
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
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!!!
 
Old 06-23-2006, 05:41 PM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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

Last edited by spirit receiver; 06-23-2006 at 05:42 PM.
 
Old 06-23-2006, 05:47 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 06-23-2006, 08:33 PM   #8
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
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
 
Old 06-23-2006, 09:15 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
 
Old 06-25-2006, 03:26 AM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 12-21-2009, 03:17 PM   #11
yingted
LQ Newbie
 
Registered: Dec 2009
Distribution: Fedora
Posts: 1

Rep: Reputation: 0
For anyone that sees this page on Google

NVM I thought you wanted to concatenate all the lines
Should have read first post

Last edited by yingted; 12-21-2009 at 03:22 PM. Reason: NVM I thought you wanted to concatenate all the lines
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
adding text to lines in a file tpreitano Linux - General 2 10-04-2005 09:30 AM
How to filter lines from a text file Rascale Linux - Software 1 10-28-2003 12:46 PM
Loop over lines in text file? amaze Linux - General 2 08-12-2003 07:15 AM


All times are GMT -5. The time now is 01:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration