Simple 'sed', 'awk', 'cut' problem

Simple 'sed', 'awk', 'cut' problem

Post by Tim Weil,Reston,732 » Thu, 24 Jun 1993 20:22:27



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

 
 
 

Simple 'sed', 'awk', 'cut' problem

Post by Rob Ry » Fri, 25 Jun 1993 21:28:57



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

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) }'

-- Rob
--
Rob Ryan, System Constructs Inc.


 
 
 

Simple 'sed', 'awk', 'cut' problem

Post by HARLOW A » Fri, 25 Jun 1993 23:44:00




>>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
>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) }'

Or sed could do it like:

sed 's/\(......\)\(...\)\(...\)\(....\)\(.....\)/\1|\2|\3|\4|\5/'

 
 
 

Simple 'sed', 'awk', 'cut' problem

Post by David W. Tamk » Sat, 26 Jun 1993 05:20:05





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

 
 
 

Simple 'sed', 'awk', 'cut' problem

Post by Robert Hartm » Sat, 26 Jun 1993 04:28:59



>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

I don't know if this is the simplest, but it's simple and it works:

        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

3. alpha sort: 'sed'/'awk'?

4. QT: Please help!!

5. sed -e 's/\'a\'/\';\'/' ?

6. Statically-linked BASH binary

7. PROBLEM: 'sed' script 's/^ /\n/' not working properly

8. RedHat 6&7 POP3 delay

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???