LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-24-2012, 11:03 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
sed - loop construct for text processing


I'm learning the sed loop construct and having difficulty.

The input file consists of a variable number of blank-delimited fields.
Sample input file:
Code:
000001 now is the time
000002 for all good men
000003 to come to the aid
000004 of their country
The desired transformation replaces the 2nd, 3rd, ... nth field
with whatever appears in the first field.
In this example the first field is a line number but it could be
anything (color, city, car model, etc.)
Sample output file:
Code:
000001 000001 000001 000001 000001
000002 000002 000002 000002 000002
000003 000003 000003 000003 000003 000003
000004 000004 000004 000004
Is sed-with-loop a suitable technique?
If so, please show how it is done.

Daniel B. Martin
 
Old 01-24-2012, 11:47 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Not sure if I would use sed to do this, awk comes to mind:
Code:
awk '{ for ( z = 1; z <= NF; z++ ) printf("%s ",$1) }{ print "" } ' infile
NF is an awk internal variable that holds the amount of fields on the line that is being worked on, which is used in a loop.

Hope this helps.
 
Old 01-24-2012, 06:06 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi again.

Here is a commented sed solution, I'm relatively satisfied with (put it to file and make executable)
Code:
#!/bin/sed -rf

# squeeze spaces
s/ +/ /g
# place newline (used as a marker) after second word
s/ /\n/2

:a

# print intermediate steps for debugging
l

# w1 w2\n -> w1 w1\n
s/([^ ]+) ([^ ]+)\n/\1 \1\n/

# if newline is at the end of string -- remove marker and go to the end
/\n$/ {s/\n//; b}

# advance marker
s/\n([^ ]+) ?/ \1\n/

ta
Here is my previous attempt if you are interested:
Code:
sed -r 's/$/ =/; h; s/ .*$//;x; s/ +/\n/g; :a; s/[^\n]*\n//; G; /^=/be; ba; :e; s/\n/ /g; s/= //'
= is a marker symbol. It is not unique, so this may cause problems.
 
1 members found this post helpful.
Old 01-24-2012, 09:31 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

you could try this:
Code:
sed -r ':a;s/([0-9]+) [a-zA-Z]+(.*)/\1 \1\2/g;ta'
However, it has limits. It only works if there are no other numbers in the other fields. E.g., this will fail:
Code:
000001 now is the 3rd time
 
2 members found this post helpful.
Old 01-24-2012, 10:38 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Original Poster
Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
Quote:
Originally Posted by firstfire View Post
Here is a commented sed solution ...
Thank you for this impressive solution, and for commenting so well. It will take me a while to digest and understand it.

Daniel B. Martin
 
Old 01-24-2012, 10:42 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Original Poster
Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
[QUOTE=crts;4583633]
Code:
sed -r ':a;s/([0-9]+) [a-zA-Z]+(.*)/\1 \1\2/g;ta'
Thank you for providing this solution. It is relatively concise, and that's a "plus" for comprehension. The restriction is tolerable for my purposes. I'll test all proposed solutions with larger files than the original sample.

Daniel B. Martin
 
  


Reply

Tags
sed


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 10:18 AM
[SOLVED] sed processing wget progress bar RichardS Programming 9 11-05-2010 12:01 PM
again stucked with text processing (sed/awk/perl), copy the line and change rahmathullakm Programming 4 01-19-2009 02:53 PM
Text substitution and processing with sed and awk shanecraddock@gmail.com Linux - Newbie 1 12-18-2008 12:34 PM
saving files after sed processing raypen Linux - General 2 07-23-2002 07:38 AM


All times are GMT -5. The time now is 01:47 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration