- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Replace first 10 spaces in text file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 04:35 AM
01-26-2005 04:35 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 04:48 AM
01-26-2005 04:48 AM
Solution#!/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";
}
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 04:49 AM
01-26-2005 04:49 AM
Re: Replace first 10 spaces in text file
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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 06:18 AM
01-26-2005 06:18 AM
Re: Replace first 10 spaces in text file
> 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 08:01 AM
01-26-2005 08:01 AM
Re: Replace first 10 spaces in text file
cat file|tr -s " " ","
then handle it with awk thereafter.
Chris
- Tags:
- tr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 11:38 AM
01-26-2005 11:38 AM
Re: Replace first 10 spaces in text file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 05:53 PM
01-26-2005 05:53 PM
Re: Replace first 10 spaces in text file
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2005 12:46 AM
01-27-2005 12:46 AM
Re: Replace first 10 spaces in text file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2005 01:25 AM
01-27-2005 01:25 AM
Re: Replace first 10 spaces in text file
> A quick n goofy way...
Thanks! That's pretty neat.
Merijn,
> the shorter way
With wonderful examples and explanations!
Vele dank!