Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-16-2010, 06:19 AM   #1
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
replacing awk with perl

I want the output of this file to be in a column, not next to each ohter. I tired putting a a newline escape character in a few places, but it breaks the script. It is easy in awk, just ls -ltr | awk '{print $8 }'

casper@casper-laptop:~$ pwd
casper@casper-laptop:~$ ls -ltr > /tmp/outfile
casper@casper-laptop:~$ cat -n /tmp/moreawkreplace
1 #!/usr/bin/perl
2 @eighth_column;
3 while ($line =<>) {
4 push (@eighth_column, (split(/\s+/, $line))[7]);
5 }
6 print join(' ', @eighth_column);
casper@casper-laptop:~$ /tmp/moreawkreplace /tmp/outfile
this is the output:::

colemak.typ colemak-1.0 colemak-1.0.tar.gz colemak-1.0.tar Examples Templates Public Music
Old 07-16-2010, 06:56 AM   #2
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
#!/usr/bin/env perl

open my $file, "ls -ltr |" or die "Could not run ls";
while (<$file>) {
    my $col = split;
    $col = $col[7];
    print "$col\n";
close $file;
But for this application, I think awk is better. Or even the simple cut command.

Last edited by MTK358; 07-16-2010 at 06:58 AM.


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
Replacing string using awk gregarion Programming 3 01-19-2010 11:02 PM
Error in replacing special chars using awk linux_vidhyarthi Linux - General 2 04-09-2008 07:39 PM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM
Replacing a place holder on a template with bash, sed, and or awk rignes Programming 7 02-16-2006 04:20 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:52 PM.

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