LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 03-15-2016, 02:37 PM   #1
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Joining Three Lines of Text into One Line with Delimiters Between the Original Lines


I have a file that contains
Code:
First_name Last_name
House_Number Street_Name Street_Type
City State Zip
I want the output to be a CSV file with tab delimiters between the three fields.

The names can include Jr, II, Sr, maybe a middle name too.

The Addresses can be 12345 Nicholson Hill Road or 12345 Cedar (or mine, 12345 North US Hwy 23.

The City State and Zip are just that.

There is a blank line in file between each (I can simply remove those.

I want the three to be joined with a tab character as a delimiter.

I've been fiddling with paste, can't quite get that to work, I've been fiddling with sed, no joy there either. I'm just getting ready to write in C (and this is a one-off job).

I know I've done this before, just gotten too danged old to remember how.

Anybody have a clue?
 
Old 03-15-2016, 02:44 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
man tr ?
maybe awk would be better.
i think i would do it in c also.

Last edited by schneidz; 03-15-2016 at 02:46 PM.
 
Old 03-15-2016, 02:51 PM   #3
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,646

Rep: Reputation: 147Reputation: 147
+1 for tr. Not tested because I'm bringing my son to bed:
Code:
cat text | tr "\n" "\t"
 
Old 03-15-2016, 03:10 PM   #4
riwi
Member
 
Registered: Jul 2011
Location: Netherlands
Distribution: Slackware64 14.2
Posts: 64

Rep: Reputation: Disabled
Code:
#!/usr/bin/perl
use warnings;
my $line1;
my $line2;
my $line3;
my $outline;
open(in_file,"<./list.txt") or die "no file found";
open(out_file,">./list_perl.txt") or die "cannot create file";
while (my $line1=<in_file>) {
  $line2=<in_file>;
  $line3=<in_file>;
  $line1 =~ s/\R//g;
  $line2 =~ s/\R//g;
  $line3 =~ s/\R//g;
  $outline= $line1 . "\t" . $line2 . "\t" . $line3 . "\n";
  print out_file $outline;
  }
close(in_file);
close(out_file);
input
Code:
First_name Last_name
House_Number Street_Name Street_Type
City State Zip
First_name Last_name
House_Number Street_Name Street_Type
City State Zip
First_name Last_name
House_Number Street_Name Street_Type
City State Zip
output :
Code:
First_name Last_name    House_Number Street_Name Street_Type    City State Zip
First_name Last_name    House_Number Street_Name Street_Type    City State Zip
First_name Last_name    House_Number Street_Name Street_Type    City State Zip

I like perl better because it is usually easier to read and much fast on large text file handling.

Last edited by riwi; 03-15-2016 at 03:19 PM.
 
4 members found this post helpful.
Old 03-15-2016, 03:23 PM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,901

Rep: Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025
sed implementation:
Code:
#!/usr/bin/sed -f

/^$/d
N
N
s/\n/\t/g
edit: enhanced version here

Last edited by GazL; 03-16-2016 at 07:09 PM. Reason: Nicer version
 
3 members found this post helpful.
Old 03-15-2016, 04:35 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,237

Rep: Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321
Python implementation:

Code:
#!/usr/bin/env python

import fileinput
import itertools

field_width = 3

field_indexes = (i for i in itertools.cycle(range(field_width)))
fields = []
for index, line in itertools.izip(field_indexes, fileinput.input()):
    fields.append(line.strip())
    if index == field_width - 1:
        print '\t'.join(fields)
        fields = []
Save it as "tabit" and run it with:

Code:
./tabit list.txt

Last edited by dugan; 03-15-2016 at 04:54 PM.
 
4 members found this post helpful.
Old 03-15-2016, 06:01 PM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,375

Rep: Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754
An awk solution.
Code:
awk '/.+/{x=$0;getline;x=x"\t"$0;getline;x=x"\t"$0;print x}' input.txt

Last edited by allend; 03-15-2016 at 06:08 PM.
 
3 members found this post helpful.
Old 03-15-2016, 07:20 PM   #8
Richard Cranium
Senior Member
 
Registered: Apr 2009
Location: McKinney, Texas
Distribution: Slackware64 15.0
Posts: 3,858

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
Quote:
Originally Posted by allend View Post
An awk solution.
Code:
awk '/.+/{x=$0;getline;x=x"\t"$0;getline;x=x"\t"$0;print x}' input.txt
The few times that I used awk, I thought that it was pretty powerful.

Unfortunately, I could never remember how the clever stuff that I had written worked after a couple of days.

https://news.ycombinator.com/item?id=682302
 
Old 03-15-2016, 07:28 PM   #9
Richard Cranium
Senior Member
 
Registered: Apr 2009
Location: McKinney, Texas
Distribution: Slackware64 15.0
Posts: 3,858

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
Quote:
Originally Posted by riwi View Post
I like perl better because it is usually easier to read and much fast on large text file handling.
When I used Perl in my day job, it felt as if the Devil was breaking wind into my face with almost every line that I read.

A large subset of the coding world love Perl. There's nothing wrong with that (and I've seen some pretty amazing stuff written in Perl).

I'd just rather not, and probably because the "There's more than one way to do it" is a horrible thing to inflict upon the reader of your code for anything remotely complicated.

(Please don't take the above as a comment about the code that you posted. I just hate Perl.)
 
Old 03-16-2016, 06:00 AM   #10
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Original Poster
Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Originally Posted by allend View Post
An awk solution.
Code:
awk '/.+/{x=$0;getline;x=x"\t"$0;getline;x=x"\t"$0;print x}' input.txt
Works like a champ, thank you (and I understand AWK, just hadn't got quite there yet).

Thanks to everybody, cripes there are so many ways to skin a cat -- thought I knew a couple, but, wow.

All is well that ends.
 
Old 03-16-2016, 10:41 AM   #11
PrinceCruise
Member
 
Registered: Aug 2009
Location: /Universe/Earth/India/Pune
Distribution: Slackware64 -Current
Posts: 890

Rep: Reputation: 186Reputation: 186
This thread is gold.

Regards.
 
Old 03-16-2016, 12:03 PM   #12
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
tronayne --

I know you've solved this one but being an awk junkie, myself, I had to send you another one <G>

Don't delete the Blank Lines !!!

This 'style' is a common file format and it's actually covered in 'the awk book' ( "The AWK Programming Language / Edition 1" by A,K,W )

Here's another using a newline as a Field Separator ( FS = "\n" ) and a "NULL" as a Record Separator ( RS = "" )

It will work as long as the Blank Lines are truly Blank and not spaces.

Code:
$ gawk 'BEGIN{ FS = "\n" ; RS = "" }{ print $1 "\t" $2 "\t" $3 }' <<your file>>
Test Data (<G> thanks GazL <G>):

Code:
$ cat test.txt
Joe Bloggs
5 Somewhere street, Somewhere.
Some Zip

Mary Smith
27  Other Street, Otherplace.
Another Zip
Note that the last line in the input file may be a blank line or not ( does not matter )...

Here's the output:
Code:
$ gawk 'BEGIN{ FS = "\n" ; RS = "" }{ print $1 "\t" $2 "\t" $3 }' test.txt
Joe Bloggs      5 Somewhere street, Somewhere.  Some Zip
Mary Smith      27  Other Street, Otherplace.   Another Zip
-- kjh
 
1 members found this post helpful.
Old 03-16-2016, 12:13 PM   #13
bassmadrigal
LQ Guru
 
Registered: Nov 2003
Location: West Jordan, UT, USA
Distribution: Slackware
Posts: 8,792

Rep: Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656
Quote:
Originally Posted by kjhambrick View Post
This 'style' is a common file format and it's actually covered in 'the awk book' ( "The AWK Programming Language / Edition 1" by A,K,W )
I feel like they missed a golden opportunity to order their names so their initials could come out as AWK instead of alphabetically.

Alfred Aho
Peter Weinberger
Brian Kernighan

 
Old 03-16-2016, 12:16 PM   #14
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by bassmadrigal View Post
I feel like they missed a golden opportunity to order their names so their initials could come out as AWK instead of alphabetically.

Alfred Aho
Peter Weinberger
Brian Kernighan

those are the original designers of the awk parsing language. i guess book publishers automatically alphabetize multi-authors (or maybe they are just not knowledgeable about it) ?
 
Old 03-16-2016, 12:32 PM   #15
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Original Poster
Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Originally Posted by kjhambrick View Post
tronayne --

I know you've solved this one but being an awk junkie, myself, I had to send you another one <G>

Don't delete the Blank Lines !!!
Hey, that's even slicker -- was looking through The AWK Programming Language this morning, hadn't noticed that (got busy with doing other things and just getting back to it).

BTW, you can go to Brian Kernighan's web site, http://www.cs.princeton.edu/~bwk/, and download the source for a Linux version of AWK (New AWK, the one from the book, with updates and fixes). Sometimes that page is a little iffy, sometimes it's not (if you can't get to it, I'll e-mail to you if you want).

Get it, unzip it (be careful, create a directory, cd into it, then unpack it; you get the source and all the examples from the book. Type make, wait a while, copy a.out to /usr/local/bin as nawk.

I use it instead of gawk. It doesn't have embedded GNU "features," and it works just fine.

Thanks for your interest.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replace text lines identified by leading text in line within multiple files juergen852 Linux - Newbie 9 09-21-2014 04:54 PM
[SOLVED] ASCII text with very long lines with CRLF line terminators sdorich Linux - Newbie 5 11-21-2013 05:33 PM
[SOLVED] bash: sort lines in 2 files so that equal lines are at the same line number... masavini Programming 10 06-21-2012 01:58 PM
[SOLVED] edit multiple lines of a text file into 1 line: schneidz Programming 2 04-09-2009 11:22 AM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 02:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 09:07 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration