for i in `cat file` do


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users for i in `cat file` do
# 1  
Old 08-29-2011
for i in `cat file` do

in bash:

HTML Code:
for i in `cat file` ; do
echo $i    
done;
how will i do this in perl ?
# 2  
Old 08-29-2011
Code:
open(MYINPUTFILE, "<filename");
while(<MYINPUTFILE>)
 {
 # Good practice to store $_ value because
 # subsequent operations may change it.
 my($line) = $_;

 # Good practice to always strip the trailing
 # newline from the line.
 chomp($line);

 # Print the line to the screen and add a newline
 print "$line\n";
 }

---------- Post updated at 06:25 PM ---------- Previous update was at 06:25 PM ----------

Code:
#!/usr/local/bin/perl  
open (MYFILE, 'data.txt');  
while (<MYFILE>) {
   chomp;   
   print "$_\n";
  } 
 close (MYFILE);

just google it.. you can find lot of ways
This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 08-29-2011
If you want to preserve the input file content intact
during processing you should be using something like
this:

Code:
while IFS= read -r var; do
  printf '%s\n' "$var"
done < infile

Consider the following:

Code:
bash-2.03$ ls
infile
bash-2.03$ cat infile
one two
-n -e ok?

   three *
bash-2.03$ for i in `cat infile` ; do   echo $i    ; done
one
two

ok?
three
infile
bash-2.03$


And:

Code:
bash-2.03$ while IFS= read -r var; do   printf '%s\n' "$var"; done < infile
one two
-n -e ok?

   three *
bash-2.03$

So the later with Perl would be:

Code:
bash-2.03$ perl -pe1  infile
one two
-n -e ok?

   three *

Or:

Code:
#!/usr/bin/perl

use warnings;
use strict;

open my $infile, '<', 'infile'
  or die "open: $!\n";
  
print while <$infile>;

close $infile
  or warn "close: $!\n";

This User Gave Thanks to radoulov For This Post:
# 4  
Old 08-29-2011
Thanks itkamaraj... i will try this ..
# 5  
Old 08-29-2011
How to emulate that shell fragment depends on the value of IFS.

Assuming the default value, itkamaraj's suggestions are incorrect. That sh loop prints out one line per word, not a line per line. The sh loop will also not only trim leading and trailing whitespace, but squeeze contiguous whitespace embedded in the line.

If IFS is set to a non-default, non-whitespace value, there would be no trimming.

Regards,
Alister
# 6  
Old 08-29-2011
Quote:
Originally Posted by linuxgeek
in bash:

Code:
for i in `cat file` ; do
echo $i    
done;

how will i do this in perl ?
That's not even how you're supposed to do this in bash. It's wasteful and dangerous -- a file too long might throw an error, or just be silently truncated. Where did you learn this?

Code:
while read i
do
...
done < file

# 7  
Old 08-30-2011
Actually, below is my bash script that i want to do in perl and it has two arguements:

Arguement1 is list of servers and arguement2 is list of home directories.

HTML Code:
#!/bin/bash

user='rsolis@sysgen.com';

servers_list=`cat $1`
homedir_list=`cat $2`

for i in $servers_list

do
ssh -q root@$i true
   if [ $? = 0 ]

   then
      for j in $homedir_list
      do
        ssh root@$i "/bin/echo $user >> /opt/home/$j/.k5login"
        echo -e " ... appending $user in server $i at /opt/home/$j/.k5login ...OK"
      done

   else
   echo -e "\nServer: $i ssh is down"

fi
done
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh cat file output into a file on local computer

Hello, I'm on a remote computer by SSH. How can I get the output of "cat file" into a file on the local computer? I cannot use scp, because it's blocked. something like: ssh root@remote_maschine "cat /file" > /locale_machine/file :rolleyes: (2 Replies)
Discussion started by: borsti007
2 Replies

2. Shell Programming and Scripting

perl and file and cat

Hi All i need a little script that can open a file , read it and then spit out some information from it from the shell i would do cat /var/log/Xorg.0.log | grep pixel | sed 's/: 330.*//' | how can i do this nicley in perl thanks Adam (3 Replies)
Discussion started by: ab52
3 Replies

3. Shell Programming and Scripting

Cat file

how to cat a file by ignoring first line and last line (1 Reply)
Discussion started by: thelakbe
1 Replies

4. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

5. Shell Programming and Scripting

cat a file on webpage

Hi, Is there a way to cat a file on Webpage? . Thanks in advance (3 Replies)
Discussion started by: rider29
3 Replies

6. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

7. UNIX for Dummies Questions & Answers

How to cat file

I want to cat a file with only show the line contain '/bin/bash' but don't show the line contain 'load' (don't show if the line contain 'load' and '/bin/bash' together), how to type in the command? thk a lot! (2 Replies)
Discussion started by: zp523444
2 Replies

8. UNIX for Dummies Questions & Answers

Easiest way to cat out first 100 lines of a file into a different file?

Not sure how to do this exactly.. just want to take the first 100 lines of a file and cat it out into a second file. I know I can do a more on a file and > it into a different file, but how can I make it so only the first 100 lines get moved over? (1 Reply)
Discussion started by: LordJezo
1 Replies

9. Shell Programming and Scripting

cat file problem

Hi, I wnat to read a fiel line by line and store each line in a variabel, so I made a for loop: for i in `cat file` ; do #do sth. done; The problem is, that in the file, there are lines with only asterisks like this... (3 Replies)
Discussion started by: bensky
3 Replies
Login or Register to Ask a Question