![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Append string to columns from 2 files | karthikn7974 | Shell Programming and Scripting | 3 | 04-28-2008 06:32 AM |
| awk printing all columns after (but including) $n | cassj | UNIX for Dummies Questions & Answers | 2 | 03-05-2008 12:22 PM |
| Need help in AWK;Search String and rearrange columns | spring_buck | Shell Programming and Scripting | 2 | 04-05-2007 08:40 AM |
| Printing Columns in Unix | Terrible | Shell Programming and Scripting | 3 | 11-27-2006 03:43 AM |
| printing columns | cdunavent | Shell Programming and Scripting | 3 | 06-07-2003 08:31 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Perl, printing a string into columns
How can I use Perl to a take a string of 10 characters and print the last five characters of the string in columns 1-5 and the first five in columns 6-10?
Result: 0123456789 5 0 6 1 7 2 8 3 9 4 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
If the string is a constant and the output is also constant, use 'substr' to put each character into variables then print out in any format you want.
Code:
my $first_char = substr $_, 0, 1; my $second_char = substr $_, 1, 1; my $third_char = substr $_, 2, 1; my $fourth_char = substr $_, 3, 1; . . . print "$sixth_char\t$first_char\n"; print "$seventh_char\t$second_char\n"; . . . and so on |
|
#3
|
|||
|
|||
|
Code:
$ echo 0123456789 | perl -nle 'for ($i=0; $i < 5; $i++) { print substr($_,$i+5,1)," ", substr($_,$i,1) }'
5 0
6 1
7 2
8 3
9 4
|
|
#4
|
|||
|
|||
|
The string is actually going to be coming from STDIN.
|
|
#5
|
|||
|
|||
|
So I dug around and found some code similar to what I want, but not quite (similar in that it can break a string down by characters).
#!/usr/bin/perl print "Enter 10 characters\n"; chomp ($foo=<STDIN>); { %seen = (); foreach $char (split //, $foo) { $seen{$char}++; } print "broken into columns: ", sort(keys %seen), "\n"; } print $foo; How can I get this to print characters into columns then? |
|
#6
|
|||
|
|||
|
Nevermind. Got it.
#!/usr/bin/perl print "Enter 10 characters\n"; chomp ($foo=<STDIN>); @arr=split //, $foo; for($i=0;$i<5;$i++) { print $arr[$i+5] . " " . $arr[$i] . "\n"; } |
|||
| Google The UNIX and Linux Forums |