how to include a text pattern in system function in PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to include a text pattern in system function in PERL
# 1  
Old 08-23-2012
CPU & Memory how to include a text pattern in system function in PERL

Pleeeeease help..

I'm working in perl

i have a system function to check whether a file is readable for others or not.

i just want to print a text in that command

my command is :

Code:
system ("ls -la $filename | awk '\$1~ /^-......r../ {print \$9}'");


i know for displaying text in awk command one needs to put it in double quotes "" but here how to do that.. when we are executing awk in system command (which already has double quotes in the start and end)


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. You already got a PM for that for your last posts, do not ignore that. Else you will start collecting infraction points.

Last edited by zaxxon; 08-23-2012 at 04:17 AM.. Reason: code tags
# 2  
Old 08-23-2012
Quote:
Originally Posted by shubhamsachdeva
i have a system function to check whether a file is readable for others or not.
my command is :
Code:
system ("ls -la $filename | awk '\$1~ /^-......r../ {print \$9}'");

Perl has file tests too, you know. You can do this to check if a file is readable or not!

Code:
if ( -r "/path/to/file.txt" ) {
    print "Yes, readable.\n";
}

# 3  
Old 08-23-2012
if (-r ) would display the print output if the file is readable for the user.

i need to check it for others.

E.g.

-rw--rw-r--

it means its :-

readable and writable for user
readable and writable for group
and only readable other

so.. i need it for others
# 4  
Old 08-23-2012
As i said in the another thread, you need to check stat

https://www.unix.com/shell-programmin...on-others.html
# 5  
Old 08-23-2012
its not helping

when i write stat file.txt on the command terminal the file statistics appear.

but when i write this in a perl script .. output doesn't appear

i anyway have to use

system (stat file.txt)

then it appear


but again difficulties are there in system command to print a string
# 6  
Old 08-23-2012
Indeed it is helping a lot.

If you would have checked the link itkamaraj posted correctly, you would have seen this example, which has just been changed by the shebang and a variable $filename:
Code:
$ cat mach.pl
#!/usr/bin/perl
$filename = "./infile";
$mode = (stat($filename))[2];
printf "Permissions are %04o\n", $mode & 07777;

It produces:
Code:
$ ls -la infile
-rw-r--r-- 1 root root 0 23. Aug 11:12 infile
$ ./mach.pl
Permissions are 0644

# 7  
Old 08-23-2012
Code:
#!/usr/bin/perl -w
$file="input.txt";
$mode = sprintf '%04o', (stat $file)[2] & 07777;
print "Permissions are $mode\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Include strings between a pattern

Hi, I have two files: File1: sample statements1 <UID = " " PWD = " "> sample statements2 File2: UID ="admin" PWD ="password" (6 Replies)
Discussion started by: manid
6 Replies

2. Shell Programming and Scripting

How to include file pattern in find command?

Hi I've to remove the files which has the following file pattern in path /home/etc/logs fnm_HST_date1 fnm_hst_date1 fnm_HST_date2 I've used the following code to to remove the files having file names like "HST" . #!/usr/bin/ksh set -x file_path=/home/etc/logs file_nm=HST find... (2 Replies)
Discussion started by: smile689
2 Replies

3. UNIX for Dummies Questions & Answers

How to modify the script to include cat function

I have the following script that greps lines containing "AT" from data files data1.hsq through data1000.hsq, then cuts their second column and puts in data files called perm1 through perm1000. I want to modify the script so that instead of putting the data in separate data files perm1 through... (2 Replies)
Discussion started by: evelibertine
2 Replies

4. Shell Programming and Scripting

awk regex- include text

Hi I am trying to filter some data using awk. I have a statement- awk 'BEGIN { FS = "\n" ; RS = "" } { if ( $6 = "City: " ) { print "City: Unknown" } else { print $6 } }'` The $6 values are City: London City: Madrid City: City: Tokyo This expression seems to catch all the lines... (4 Replies)
Discussion started by: jamie_123
4 Replies

5. UNIX for Dummies Questions & Answers

Needing a windowed system monitor that include temps

Hello, I'm using Ubuntu Netbook Edition on my family's 4 netbooks. Sadly (and a big mistake in my opinion?), I can't add a temp monitoring applet to my panel, but that's not really the point. I am needing a way to monitor my computer temps that includes a graph (kinda like a CPU usage ... (0 Replies)
Discussion started by: Narnie
0 Replies

6. UNIX for Advanced & Expert Users

Remove rows which include the word pattern

Hi, I'm trying to remove rows that including specific word, but the below is replacing only the first occurrence, how to do this for the all occurrences within the file. grep -v "mdsMVLink\|mdsMVMembership\|mdsMVLinkType" sfile.txt > dfile.txt Could someone help me how to accomplish this? ... (2 Replies)
Discussion started by: john_prince
2 Replies

7. Shell Programming and Scripting

Problem with sending email(to include contents of text file)

Hello, I was using a shell script for sending contents of a text file(email.report) to different users. I was using the below command in my script to send email... cat email.report | /usr/bin/mailx -s $REQ_SUBJECT -h 5 abc@xyz.com It was working fine all these days but now all of a sudden it... (18 Replies)
Discussion started by: smarty86
18 Replies

8. Shell Programming and Scripting

Include special system characters in file count

Hi, I have a script that checks the length of each record/line in file - This seems to be working when there are no special systems character that are invisible or hidden. awk -v file=$file '{ if (filename==file) { k+=$5 if (length() <= 10 ){print size length(), "bytes " k} }... (2 Replies)
Discussion started by: asemota
2 Replies

9. Shell Programming and Scripting

Include Line Before Pattern Using Sed / Awk

Hi, I have a sql file that runs something like this vi Test.sql REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM PUBLIC; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM DEMO_READ; REVOKE SELECT ON DEMO_USER.DEMO_NOMINEE_TEST FROM DEMO_READ; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM... (3 Replies)
Discussion started by: rajan_san
3 Replies

10. Shell Programming and Scripting

How to find pid of PS which executed by perl system function

hello All, I need to invoke by perl script some program/command and monitor it for 5 minutes . In case it still running for more then 5 min I need to send a signal which will stop it. I implemeted this as shown below by using eval & alarm and I'd like to know if there is a better way to... (1 Reply)
Discussion started by: Alalush
1 Replies
Login or Register to Ask a Question