Visit Our UNIX and Linux User Community


Perl SCript to read file content (if else statemenet)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl SCript to read file content (if else statemenet)
# 1  
Old 04-14-2014
Perl SCript to read file content (if else statemenet)

Hi All,
I wanted to write a perl script to read the content in a file,the file content is either 0 (zero) OR 1. The idea is like this.


If (content =1), then it will proceed to perform some step. and then update the file content to 0(zero)
else
if (content =0), it will update the content to 1 and do nothing.

The script will be run at every Saturday.

Can anyone advise?
# 2  
Old 04-14-2014
What are the steps that needs to be performed in case if the content is 1
# 3  
Old 04-14-2014
Try:
Code:
open(FH,"</path/to/your/file/filename");
my @file = <FH>;
close FH;
my $line;
foreach $line (@file) {
  if ( $line ~= m/1/) {
    #Do your thing.
  } elsif { $line ~= m/0/) {
    #Again Do your thing
  }
}

This User Gave Thanks to chacko193 For This Post:
# 4  
Old 04-21-2014
Code:
#!/usr/bin/perl

open (FH, '>filereindex.txt') or die 'Could not open file';
my @line = <FH>;
  if ($line == 1) {
    print "NUMBER 1";
  } elsif ( $line == 0) {
   print FH 1;

  }

I m using the above,

if fileindex.txt is 0, then it will write 1 to the file: fileindex.txt [expected result that I want]
BUT
if fileindex.txt is 1, then it will do nothing, I want it to print NUMBER 1

---------- Post updated at 04:24 PM ---------- Previous update was at 04:21 PM ----------

Code:
#!/usr/bin/perl

open (FH, 'filereindex.txt') or die 'Could not open file';
my @line = <FH>;
  if ($line == 1) {
    print "NUMBER 1";
  } elsif ( $line == 0) {
   print FH 1;

  }

I remove the > from the code. IF filereindex.txt = 1, it will print NUMBER 1 [result that I want]
BUT
if filereindex.txt=0, it do nothing [i wanted it to write 1 to the filereindex.txt]
# 5  
Old 04-21-2014
Did you try the code that I gave? Remove the comments and replace them with your print statements. And the code that you gave will not work.

You have declared line as an array and when you are checking you use it as scalar. Provide the index.
# 6  
Old 04-21-2014
I have the below when run the one you gave.
Code:
#!/usr/bin/perl

open(FH,"<filereindex.txt");
my @file = <FH>;
close FH;
my $line;
foreach $line (@file){
  if($line ~= m/1/){
    print "NUMBER 1";

  }elsif($line ~= m/0/){
    print "ZERO";
  }
}

syntax error at ./biw.pl line 8, near "$line ~"
syntax error at ./biw.pl line 11, near "}elsif"
Execution of ./biw.pl aborted due to compilation errors

my file only have either 0 or 1.
if 1, then it will run reindexing on the application, and then change file content from 1 to 0 , then exit

else if file content=0, it will change file content from 0 to 1 and then exit.
# 7  
Old 04-22-2014
Sorry my mistake. Pls replace ~= with =~

Previous Thread | Next Thread
Test Your Knowledge in Computers #523
Difficulty: Medium
Some programming languages implement strings by defining arrays of characters.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

2. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

3. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

4. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

5. Shell Programming and Scripting

perl - cgi script to read file from network

Hi, I have written a cgi perl script to read a file and display its contents. But if i have to access file located on network (local network), then i am unable to do so. However if i run script through shell it works, but through cgi script it generates no result. I have mapped network drive... (0 Replies)
Discussion started by: sarbjit
0 Replies

6. Shell Programming and Scripting

How to read file and only output certain content

Hi - I have a file containing data like :- cn=tommy,cn=users,c=uk passwordexpirydate=20100530130623z cn=jane,cn=users,c=uk passwordexpirydate=20100423140734z cn=michael,cn=users,c=uk passwordexpirydate=20100331020044z I want to end up with a file that looks like:-... (6 Replies)
Discussion started by: sniper57
6 Replies

7. Shell Programming and Scripting

read file content

i have one file abhi.txt its contents are home8/mc09ats/UnixCw/backup/file1 home8/mc09ats/file2 i want to read this content of file using while loop.. in this i want to seperate the content as follows path=home8/mc09ats/UnixCw/backup file=file1 echo path echo file can you... (1 Reply)
Discussion started by: AbhijitIT
1 Replies

8. Shell Programming and Scripting

Read a file content with awk and sed

Hello , I have huge file with below content. I need to read the numeric values with in the paranthesis after = sign. Please help me with awk and sed script for it. 11.10.2009 04:02:47 Customer login not found: identifier=(0748502889) prefix=(TEL) serviceCode=(). 11.10.2009 04:03:12... (13 Replies)
Discussion started by: rmv
13 Replies

9. Shell Programming and Scripting

read a file and use the content for mapping

help me pls.. :( i want to read a mapping file. Below is the content of my mapping file. 6221,189,SMSC1,OMC1,WAP1 6223,188,SMSC2,OMC2,WAP2 so when my program running msisdn="622130302310" while not EOF if substring($msisdn,1,4) == "6221" -- > "6221" read from the file then echo... (0 Replies)
Discussion started by: voidmain
0 Replies

10. UNIX for Dummies Questions & Answers

How to read the content of the files in unix script

Hi I need help below is my textfile format look like PO Nbr Ln Item Number Description Qty Order Order Date Due Date Status Reply ID Reply Date Reply Qty P304802 1 K0220040 TSX-3225 C 16.367900 MHz 320379 07/01/2008 29/01/2008 REQ OP304802 02/02/2008 190000 P304802 2 K0220040 TSX-3225 C... (0 Replies)
Discussion started by: thila
0 Replies

Featured Tech Videos