LinuxQuestions.org
Visit Jeremy's Blog.
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, 11:08 AM   #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, 02:02 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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 02:04 PM. Reason: //Warn
 
1 members found this post helpful.
Old 05-07-2013, 02:39 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,520

Rep: Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944
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 12:18 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
writing perl script to search forums qcoder Programming 2 07-13-2004 06:10 AM
writing a bash/ perl script on debian phr00ta Programming 4 01-29-2004 01:38 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

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

All times are GMT -5. The time now is 06:05 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
Open Source Consulting | Domain Registration