LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-16-2016, 01:22 PM   #16
Paulo2
Member
 
Registered: Aug 2012
Distribution: Slackware64 15.0 (started with 13.37). Testing -current in a spare partition.
Posts: 928

Rep: Reputation: 515Reputation: 515Reputation: 515Reputation: 515Reputation: 515Reputation: 515

A bash solution
Code:
while read line;do let ++i;[ $i -lt 3 ] && echo -en "$line\t" || { i=;echo "$line";continue;};done <<<'1First_name Last_name
1House_Number Street_Name Street_Type
1City State Zip
2First_name Last_name
2House_Number Street_Name Street_Type
2City State Zip
3First_name Last_name
3House_Number Street_Name Street_Type
3City State Zip'
1First_name Last_name	1House_Number Street_Name Street_Type	1City State Zip
2First_name Last_name	2House_Number Street_Name Street_Type	2City State Zip
3First_name Last_name	3House_Number Street_Name Street_Type	3City State Zip
tronayne could you please post time for all those solutions? Just curious which one is faster.
 
1 members found this post helpful.
Old 03-16-2016, 02:06 PM   #17
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 Paulo2 View Post
A bash solution
Code:
while read line;do let ++i;[ $i -lt 3 ] && echo -en "$line\t" || { i=;echo "$line";continue;};done <<<'1First_name Last_name
1House_Number Street_Name Street_Type
1City State Zip
2First_name Last_name
2House_Number Street_Name Street_Type
2City State Zip
3First_name Last_name
3House_Number Street_Name Street_Type
3City State Zip'
1First_name Last_name	1House_Number Street_Name Street_Type	1City State Zip
2First_name Last_name	2House_Number Street_Name Street_Type	2City State Zip
3First_name Last_name	3House_Number Street_Name Street_Type	3City State Zip
tronayne could you please post time for all those solutions? Just curious which one is faster.
Well, that looks pretty slick, thanks.

So far, AWK goes like a striped ape (and it's a one-liner). Have to fiddle with more than one and see what's what, though. Actually, sed and paste fly too -- not really enough data to actually measure, typically about 1 or two seconds. So far, I like the AWK solution(s).
 
Old 03-16-2016, 02:49 PM   #18
riwi
Member
 
Registered: Jul 2011
Location: Netherlands
Distribution: Slackware64 14.2
Posts: 64

Rep: Reputation: Disabled
I did not know awk was written by the same guy Kernighan that wrote _the_ book on C as well (Kernighan & Ritchie) which I ploughed through back in 1995 and loved.

Quote:
Originally Posted by Richard Cranium View Post
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.)
Perl is a lot faster than using various passes with sed / awk and other bash scripting. Especially when things get more complicated I would use perl instead of awk. I like awk only for manipulating a few strings.

And you are right, perl can be illegible too. But in practice the same goes for awk. It is upto the programmer to create something that is easy to maintain and understand. That is why I coded the perl option using several lines and not one
 
Old 03-16-2016, 03:18 PM   #19
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
Actually, there were three guys that wrote AWK: Alfred Aho, Brian Kernighan and Peter Weinberger at Bell Labs. There is an "old" AWK and a "new" AWK. oawk came with System 3, nawk with System V. System VR4 still comes with oawk and nawk (so does Solaris). The nawk book was published in 1988, oawk was, pretty much, a man page and maybe a paper or two. The joke used to be that the name was short for awkward given the grammar and syntax but you notice that the syntax bears a lot of resemblance to C with regular expressions thrown in for good measure. I've written a lot of AWK programs over the years, it does a lot for you and, if you know C, it's fairly easy to get the idea. And, from '88 to now, nobody's actually figured out a way to make it better (GNU extensions notwithstanding -- I refuse to use extensions 'cause they've burned my butt more than once in many languages over the years).

Anyway, as above, you can get "real" AWK from Brian Kernighan's web site at Princeton (where he teaches).

Last edited by tronayne; 03-16-2016 at 03:19 PM.
 
1 members found this post helpful.
Old 03-16-2016, 05:36 PM   #20
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
Quote:
Originally Posted by tronayne View Post
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).
Thanks tronayne,

I already have it for nostalgia's sake<G>

I thought about wrapping it in a SlackBuild script but I've never gotten around to it.

nawk sits in my /usr/local/bin/ directory collecting dust these days

-- kjh
 
Old 03-16-2016, 06:36 PM   #21
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
I've been playing with sed this evening: trying to modify my example above to detect an arbitrary number of lines per record rather than hardcoding for 3 lines of input. All I can say is, I now know why they wrote awk!

Anyway, here it is:
Code:
#!/usr/bin/sed -f

/^$/ b print

x
/^$/! {
  G
  h
  $ b print
} 
d

: print
  x
  /^$/d
  s/\n/\t/g

Last edited by GazL; 03-16-2016 at 06:39 PM.
 
Old 03-18-2016, 06:23 AM   #22
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
Maybe I am late...

Not strictly answering the initial question, but something a little more general: merging consecutive lines (replacing the newline by a tabulation) until the next empty line as in GazL's previous post.
File input.txt:
Code:
I thought
that it could be fun
to write a poem,

unfortunately
I am not able

to do
that.
Sorry about that and
long life Thomas.

I am the last line.
Command:
Code:
sed -n -f mergelines.sed input.txt
File mergelines.sed (press [TAB] only once where you see a white space as \t is not posixly correct):
Code:
:a;${p;q};N;/	\n/{s/	\n/\n/;P;D};s/\n/	/;ba
Result:
Code:
I thought    that it could be fun    to write a poem,
unfortunately    I am not able
to do    that.    Sorry about that and    long life Thomas.
I am the last line.
PS If a single paragraph includes more than 8192 bytes the outcome is unpredictable as POSIX only requests the pattern space to hold at least that amount. This being said GNU sed can hold more than that.

Last edited by Didier Spaier; 03-18-2016 at 09:58 AM. Reason: syntax error fixed in the command.
 
1 members found this post helpful.
Old 03-18-2016, 08:39 AM   #23
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Interesting to see an alternate implementation.

I learnt quite a lot about 'sed' writing the above, I don't think I've ever used sed for anything other than manipulating individual lines before. To be honest though, it's an unwieldy tool when it comes to anything other than that, so I doubt I'll use it for anything other than manipulating singles line in future either.

Thanks for raising the pattern space limit issue. I wasn't aware of that, or that '\t' wasn't POSIX.

Last edited by GazL; 03-18-2016 at 08:53 AM.
 
Old 03-18-2016, 09:27 AM   #24
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
Thanks Didier,

Only one little tiny thing, it's sed -f merelines.sed (at least my version of sed). Woks fine that way, not so hot without the -f.

I do use sed files for things I do over and over with data that will get loaded into data bases or for cleaning up text files. For example, Windows Weenies don't seem to know the difference between a slash and a back slant (put back slants in dates, plays havoc with a DBMS). People seem bent on making sure that what they said actually is really, truly IMPORTANT!!!!!!!!! and don't know what an ellipses is for............ either.

Just little files of sed directives, makes my life easier, and when you've got thousands of lines of stuff to deal with the streaming editor that does everything on one line at a time, no jumping back to the beginning of the file. It's fast.

And AWK is pretty easy to deal with too, got a lot of AWK programs sitting around solving problems.

Thanks again and be well.
 
Old 03-18-2016, 09:42 AM   #25
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
Interesting to see an alternate implementation.

I learnt quite a lot about 'sed' writing the above, I don't think I've ever used sed for anything other than manipulating individual lines before. To be honest though, it's an unwieldy tool when it comes to anything other than that, so I doubt I'll use it for anything other than manipulating singles line in future either.
I can understand that .

However if some day you have nothing useless or fun to do, check out convtags (link in my signature below) to see something both long and ugly. Just run "sh convtags" to know more.

I think it's possible to write some functions in sed: before calling one (with t[label] or a b[label]), just store the return address somewhere in the hold space, then at the end of the function use that address to go back.

But I was too lazy to rewrite convtags to implement that. Which doen't matter as I doubt anyone ever used it

Last edited by Didier Spaier; 03-18-2016 at 11:35 AM.
 
1 members found this post helpful.
Old 03-18-2016, 09:46 AM   #26
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
Thanks Thomas, mistake corrected. Have a good day.
 
  


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 06:03 PM.

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