Convert perl-statement to /bin/sh shell

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert perl-statement to /bin/sh shell
# 1  
Old 04-21-2011
Convert perl-statement to /bin/sh shell


I'm doing a small shellscript which is going to take each line in a "queue file" and do stuff to them. I can do the script easily, but I'd like this one to be a bit prettier.

Consider the following perl statement:

foreach my $line (@filedata) {
    my ($a, $b, $c) = split(/\t/, $line);

The queue file will have three "columns" seperated by a single tab (\t). I am wondering if it's possible to make a shellscript (bash/sh, please) similar to the above. I am guessing this would involve the use of sed or awk, but I'm afraid I'm not too good at regular expressions yet to pull it off.


while read line; do
   # Magic should be happening here :-)
done < $queuefile

One could definitely argue to keep it in perl if one know how to fix it - but I'd like to expand my shellscripting knowledge / bag of tricks a bit :-)

So - What would the best (and prettiest way) to mimic the perl code in bash/sh be?
# 2  
Old 04-21-2011
Try this...


while read line; do
  set -A arr $line
  echo ${arr[0]} ${arr[1]} ${arr[2]}
done < $queuefile

This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 04-21-2011
while read a b c; do echo $a; done <file

This User Gave Thanks to bash-o-logist For This Post:
# 4  
Old 04-23-2011
Thanks to you both!

The solution from Ahamed works great in korn shell (but not SH), but still very nice to know.

I guess scripting never stops to surprise me, I didn't for a moment think it was as simple as supplying more variables to the loop.

Works like a charm!

while read uid mail; do
    echo "Uid=$uid, Mail=$mail" # Proof of concept
done < $myfile

Thanks to both of you!

Last edited by brightstorm; 04-23-2011 at 08:53 PM.. Reason: missing part in code
# 5  
Old 04-23-2011
None of the sh approaches provided are equivalent to the original perl, though they may be sufficiently close approximations for your needs. What follows is just a nitpicky analysis of the situation.

The perl is splitting on each occurence of a tab, nothing more. The sh approaches are splitting on contiguous whitespace (space, tab, and newline ... though a newline will never be seen since it's always consumed by the read).

In the perl version, consecutive tabs will yield empty fields. In the sh, they are taken as a single delimiter and will not produce an empty field.

In the perl version, if any of the tab-delimited fields contain a space character, the space will be part of a field. Under the sh approaches, the space will delimit a field and never be a member of it.

The sh read statements, due to how the sh handles IFS whitespace during field splitting, will discard leading and trailing whitespace (including tabs). The perl does not.

The sh read statement will treat a backslash before a newline as a line continuation sequence, stripping the backslash-newline pair from the data and will append the next line in the input. The perl will not treat that sequence specially. (This one can be easily fixed by using read's -r option.)

The perl list assignment will discard any extra fields generated by the split for which there are no variables allotted. The sh read statement will store all extra fields in the last variable.


Last edited by alister; 04-24-2011 at 11:11 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to shell convert

HI All, I am new in shell scripting ,can any one please help me to convert this scripts from Perl to Shell. Thank in advance. $customer=$ARGV; $rows=`hadoop fs -ls /ncip/$customer/|wc -l`; if($rows==3){ print "$customer directory exists , cleaning up ! \n"; `hadoop fs -rm... (1 Reply)
Discussion started by: Atul301
1 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

Convert perl to shell

Hi I am a novice in scripting .Please help me converting perl script into shell my ($ref) = shift; my $id; my $cnt=0; my $first =0; my $filename ; my $filename1 ; FOREACH:foreach my $r (qw(RET LSH PDM )) { $ref->{$cnt}->{num_errors} =0; $ref->{$cnt}->{num_warnings} =0;... (2 Replies)
Discussion started by: apassi
2 Replies

4. Shell Programming and Scripting

perl to shell convert

#!/usr/bin/perl #************************************************************** # # PERL SCRIPT # This script restarts the Application # # ##************************************************************** $ENV{'IFS'} = ' '; #open(STDERR,">&STDOUT"); #select... (2 Replies)
Discussion started by: srikoundinya
2 Replies

5. Shell Programming and Scripting

Convert shell script to Perl

Hello,,I have a very small script that contains these lines; and it works perfectly; however I need to use Perl now as I will need to feel variables from a MySQL table into this; to it would be nice to start by converting this first... find / -perm 777 \( -type f -o -type d \) -exec ls -lid {}... (1 Reply)
Discussion started by: gvolpini
1 Replies

6. Shell Programming and Scripting

Convert perl program to shell

Hi is there a way that i can convert this simple perl program into shell script #!usr/bin/perl -w use strict; use warnings; open INPUTFILE, "uniqprobecoordinates.out" || die "canot open the file $!"; open OUTPUTFILE, ">", "1_4reads.out"; while(<INPUTFILE>) { chomp; ... (3 Replies)
Discussion started by: bhargavpbk88
3 Replies

7. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

8. UNIX for Dummies Questions & Answers

fuser: difference with bin/sh and bin/ksh shell script

Hi, I have a problem I don't understand with fuser. I launch a simple shell script I launch the command fuser -fu but fuser doesn't return anything excepted: mysleep: Then I modify my script switching from #!/bin/sh to #!/bin/ksh I launch the command fuser -fu... (4 Replies)
Discussion started by: Peuj
4 Replies

9. Shell Programming and Scripting

Why does my /bin/csh take longer than /bin/perl?

Okay, so I have two "Hello, world!" scripts, "" and "". #!/bin/perl -w use strict; print "Hello, world!\n"; #!/bin/csh echo Hello,\ world! When I run, it runs instantly, always. When I run, it takes anywhere between 4 and 22 seconds! I'd like to know what... (3 Replies)
Discussion started by: acheong87
3 Replies

10. Shell Programming and Scripting

Help! Need to convert bash shell to perl

Hello All. I am very new to Linux and I am currently interning. I have been working on a project for 2 weeks now and I have had no success. I have to convert bash shell into perl to decrypt and store files. Here is the code in Linux and Bash. Any help would be greatly appreciated. $... (0 Replies)
Discussion started by: freak
0 Replies
Login or Register to Ask a Question

Featured Tech Videos