LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-07-2013, 12:08 PM   #1
amber1
LQ Newbie
 
Registered: Mar 2013
Posts: 8
Blog Entries: 1

Rep: Reputation: Disabled
need help writing a perl script?


Hi,

I wrote the following script to create a report of 25 users, using sed and awk, I need to re-write some of it using perl would someone please help me.

#! /bin/bash

printf "# User: UID: GID: Owner: \n" > hwpasswd.txt

tail -n 25 /etc/passwd | sed -e 's/:x:/ /' -e 'y/:/ /' -e 's/\/bin\/bash/ /' \
| awk '{print NR ". "$1,$2,$3,$4,$5,$6,$7}' | cut -f1 -d"/" | column -t >> hwpasswd.txt

I would like to thank everyone in advance.
 
Old 05-07-2013, 03:02 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by amber1 View Post
I need to re-write some of it using perl
Then do post what code you have so far. That will give your fellow LQ members something to help you with and not the impression that they are expected to write your code for you

[EDIT]like members already did in your previous three threads[/EDIT].

Last edited by unSpawn; 05-07-2013 at 03:04 PM. Reason: //Warn
 
1 members found this post helpful.
Old 05-07-2013, 03:39 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,277

Rep: Reputation: 4443Reputation: 4443Reputation: 4443Reputation: 4443Reputation: 4443Reputation: 4443Reputation: 4443Reputation: 4443Reputation: 4443Reputation: 4443Reputation: 4443
Quote:
Originally Posted by amber1 View Post
Hi,
I wrote the following script to create a report of 25 users, using sed and awk, I need to re-write some of it using perl would someone please help me.
Code:
#! /bin/bash
printf "# User: UID: GID: Owner: \n" > hwpasswd.txt

tail -n 25 /etc/passwd | sed -e 's/:x:/ /' -e 'y/:/ /' -e 's/\/bin\/bash/ /' \
| awk '{print NR ". "$1,$2,$3,$4,$5,$6,$7}' | cut -f1 -d"/" | column -t >> hwpasswd.txt
I would like to thank everyone in advance.
Well, what's posted above is a combination of what Habitual and mddesai gave you in your other thread. As unSpawn said, show what you've done/tried ON YOUR OWN, and where you're stuck, and we will be happy to help.

You say you need to rewrite 'some of it'...but the whole script is only really two lines long. Or do you mean that you want to rewrite the ENTIRE thing in Perl? There are many easily-found examples of how to read a file, and read just a certain number of lines IN that file. I also suggest you look up the split command and how to use it, but that would probably be overkill for what you're looking for. Perl can do sed operations on variables at read-time, and you can even just use a system call to take your existing command and 'wrap' it.

Here's one code snippet; you should have no problems finding the rest of the hints on Google either. Please post what you come up with and where you're stuck afterwards:
Code:
use strict;
use warnings;

my $file = 'input.filename';
open my $infile, $file or die "Could not open $file: $!";

my $count = 0;
while( my $line = <$infile>)  {   
     <put whatever operations you want here>
    last if ++$count == 25;
}
close $info;
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
call perl script through another perl script with arguments nanda22 Linux - Newbie 21 07-21-2009 01:18 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
writing perl script to search forums qcoder Programming 2 07-13-2004 07:10 AM
writing a bash/ perl script on debian phr00ta Programming 4 01-29-2004 02:38 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 06:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration