Operating System - HP-UX
1833346 Members
2958 Online
110051 Solutions
New Discussion

Replace first 10 spaces in text file

 
SOLVED
Go to solution
Robert A. Pierce
Frequent Advisor

Replace first 10 spaces in text file


Good morning!

I have a file like this:

a bb ccc dd ee ff gg hh iii j kk lmn op qr st

and I'd like to make it look like this:

a,bb,ccc,dd,ee,ff,gg,hh,iii,j,kk lmn op qr st

I figure I can use awk with

print $1","$2","$3...$12,$13...$NF

and so forth, but I'm not sure how to get from $11 to $NF. Perhaps something like $NF - $NF % 10, $NF + 1 - $NF % 10, and loop until $NF is reached?

Now I'm using "," and printing out to $20, which _should_ be greater than the number of fields, but since I don't have control over the data, I can't be sure of it staying that way.

This part doesn't have to be in awk; sed or unobfuscated perl would work as well.

Thanks,

Rob
8 REPLIES 8
Ken Penland_1
Trusted Contributor
Solution

Re: Replace first 10 spaces in text file

well, I am sure someone could come up with a shorter way to do it, but here is my solution to the problem, via perl where "textfile" is the file you are looking through:

#!/usr/local/bin/perl
foreach $line(`cat textfile`)
{
@bits = split(/\s+/,$line);
for ($i=0;$i < $#bits;$i++)
{
if ($i < 10) { print "$bits[$i],"; }
else { print "$bits[$i] "; }
}
print "\n";
}
'
A. Clay Stephenson
Acclaimed Contributor

Re: Replace first 10 spaces in text file

I would split $0 into an array and then your task becomes rather simple:

Here's x.awk:

{
i = 1
n = split($0,arry1," ")
while (i <= 10 && i < n)
{
printf("%s,",arry1[i])
++i
}
while (i < n)
{
printf("%s ",arry1[i])
++i
}
printf("%s\n",arry1[i])
}

awk -f x.awk < infile > outfile
If it ain't broke, I can fix that.
Robert A. Pierce
Frequent Advisor

Re: Replace first 10 spaces in text file

Ken,

> I am sure someone could come up with a
> shorter way to do it

Probably, but yours works and is easy to read! Thanks a lot.

Clay,

> I would split $0 into an array and then
> your task becomes rather simple:

Ah, yes! The array makes it very simple. Thanks very much!

Rob
Chris Vail
Honored Contributor

Re: Replace first 10 spaces in text file

You could convert all the spaces in your document to commas with
cat file|tr -s " " ","

then handle it with awk thereafter.



Chris
TwoProc
Honored Contributor

Re: Replace first 10 spaces in text file

A quick n goofy way...
Capture the whole list in a variable...
Set the list as your environment variables
This will put the first one in $1
The second one in $2
And the number of them in $#
That's All you need to know...

#!/bin/ksh
A="aa bb cc dd ee "
set $A
integer i; i=$#
while [ $i -gt 0 ]
do
echo $i
i=${i}-1
done

output...
aa
bb
cc
dd
ee


We are the people our parents warned us about --Jimmy Buffett
H.Merijn Brand (procura
Honored Contributor

Re: Replace first 10 spaces in text file

Just for the purists amongs you, the shorter way is to use -a

# echo "a bb ccc dd ee ff gg hh iii j kk lmn op qr st" | perl -nlae'splice@F,0,10,join",",@F[0..9];print join" ",@F'
a,bb,ccc,dd,ee,ff,gg,hh,iii,j kk lmn op qr st
#

from 'man pelrun':
--8<---
-a turns on autosplit mode when used with a -n or -p.
An implicit split command to the @F array is done as
the first thing inside the implicit while loop pro�
duced by the -n or -p.

perl -ane 'print pop(@F), "\n";'

is equivalent to

while (<>) {
@F = split(' ');
print pop(@F), "\n";
}

An alternate delimiter may be specified using -F.
-->8---

>> perl -nlae

-n: the -e section is a non-print loop around each line
-l: print a newline after every print (not printf) statement
-a: autosplit on whitespace to @F
-e: code section

>> splice@F,0,10,join",",@F[0..9];

replace, starting with the first element (index 0), 10 elements of @F, with the join with space of the first 10 elements (0..9) of @F

>> print join" ",@F

print all fields joined with space

Now play some golf (achief the same in less key-strokes):

perl -plae'splice@F,0,10,join",",@F[0..9];$_=join" ",@F'

or even

perl -plae'$_=qq{@{[(join",",@F[0..9]),@F[10..$#F]]}}'

and my final offer

perl -plae'$_=join" ",join(",",@F[0..9]),@F[10..$#F]'

Enjoy, Have FUN! H.Merijn [ who still thinks perl is so much fu
Enjoy, Have FUN! H.Merijn
Robert A. Pierce
Frequent Advisor

Re: Replace first 10 spaces in text file

Chris,

The `tr` program is great, except this particular data file has some variable-length text fields at the end; some of these fields may contain spaces.

What I needed to do was modify the _first_ part from "space-delimited" to "comma or pipe-delimited" data, and then I could work with the rest.

So, the problem was 1) space-delimited data with spacey text-fields on the end, and 2) a variable number of fields (so the original awk print $2","$3..$12,$13 wouldn't work).

(Okay, the primary problem was inflexible output to start with, but I couldn't control that. :)

Breaking the lines into arrays (in Perl and in Awk) fixed it.

Robert A. Pierce
Frequent Advisor

Re: Replace first 10 spaces in text file

John,

> A quick n goofy way...

Thanks! That's pretty neat.

Merijn,

> the shorter way

With wonderful examples and explanations!

Vele dank!