bars (|) at fixed column positions in each record as a field separator.
Example -> 123456catdogbirdZYWUX
Becomes -> 123456|cat|dog|bird|ZYWUX
What is the simplest shell script for such a problem. Thanks
Example -> 123456catdogbirdZYWUX
Becomes -> 123456|cat|dog|bird|ZYWUX
What is the simplest shell script for such a problem. Thanks
I don't know about the simplest, but awk could do it like:Quote:>bars (|) at fixed column positions in each record as a field separator.
>Example -> 123456catdogbirdZYWUX
>Becomes -> 123456|cat|dog|bird|ZYWUX
>What is the simplest shell script for such a problem. Thanks
awk '{print substr($0,1,6) "|" substr($0,7,3) "|" substr($0,10,3) "|" substr($0,13,4) "|" substr($0,17) }'
-- Rob
--
Rob Ryan, System Constructs Inc.
>>Example -> 123456catdogbirdZYWUX
>>Becomes -> 123456|cat|dog|bird|ZYWUX
>>What is the simplest shell script for such a problem. Thanks
>I don't know about the simplest, but awk could do it like:
> awk '{print substr($0,1,6) "|" substr($0,7,3) "|" substr($0,10,3) "|" substr($0,13,4) "|" substr($0,17) }'
sed 's/\(......\)\(...\)\(...\)\(....\)\(.....\)/\1|\2|\3|\4|\5/'
W> Example -> 123456catdogbirdZYWUX
W> Becomes -> 123456|cat|dog|bird|ZYWUX
W> What is the simplest shell script for such a problem. Thanks
[Rob Ryan's awk suggestion deleted]
H> Or sed could do it like:
H> sed 's/\(......\)\(...\)\(...\)\(....\)\(.....\)/\1|\2|\3|\4|\5/'
In fact, there's no need to include the fifth expression: just add the
pipes after the first four and let the rest of the line remain unchanged.
It would be nice if there were a way to do it with cut, but I can't think
of a way to insert separators without echoing to a pipe to cut five times.
sed (as A. J. Harlow illustrated plus the slight change I recommended) is
better than five calls to cut.
David W. Tamkin Box 59297 Northtown Station, Illinois 60659-0297
>Example -> 123456catdogbirdZYWUX
>Becomes -> 123456|cat|dog|bird|ZYWUX
>What is the simplest shell script for such a problem. Thanks
echo 123456catdogbirdZYWUX | \
sed -e 's/\(......\)\(...\)\(...\)\(....\)/\1|\2|\3|\4|/'
-r
1. what's awk(1)'s equivalent of sed(1)'s `!d' flag?
Experts,
I want to use a line like this,
% sed 'l1,l2\!d' file
with *awk(1)* instead; why? because I need to use an octal immediate in
an address, that sed(1) is known to be unable to accept.
Or, how do I suppress a region of a file with awk(1)? Is it possible?
Regards,
junichi
--
Junichi Kurokawa
Image and Printing System Products Development Center
Fuji Xerox Co., Ltd.
2. Socket programming, detect if client or server has gone away
6. Statically-linked BASH binary
7. PROBLEM: 'sed' script 's/^ /\n/' not working properly
9. Help: problems with 'w', 'who' and 'last'
10. How do I use Sed to replace pipe '|' with ','
11. sed and '/' to '\/' conversion.
12. can sed pattern contain '<' & '>' characters or not?
13. Does sed '/'$var'/d' textfile work if $var has / in it???