Help with convert awk script into perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with convert awk script into perl
# 1  
Old 04-12-2012
Help with convert awk script into perl

Input file (a list of input file name with *.txt extension):
Code:
campus.com_icmp_ping_alive.txt
data_local_cd_httpd.txt
data_local_cd.txt
new_local_cd_mysql.txt
new_local_cd_nagios_content.txt

Desired output file:
Code:
data   local_cd_httpd
data   local_cd
new   local_cd_mysql
new   local_cd_nagios_content

Awk command I try:
Code:
[home@user]ls *.txt | awk -F"_" '{print $1}' > tmp1
[home@user]cat tmp1
data   
data   
new   
new

I able to generate the command 1 of my desired output file with awk.
Column 2 of the desired output file is exclude those content that appear before the first "_" and remove the "*.txt" extension too.

It would be better that the script is written in perl.
Thanks for any advice!
# 2  
Old 04-12-2012
Not perl but...
Code:
# ls -1 *txt| awk '!/^c/ {sub(/_/,"\t"); print}'
data    local_cd_httpd.txt
data    local_cd.txt
new     local_cd_mysql.txt
new     local_cd_nagios_content.txt

Or with sed:
Code:
# ls -1 *txt| sed -n '/^[^c]/ {s/_/\t/p}'
data    local_cd_httpd.txt
data    local_cd.txt
new     local_cd_mysql.txt
new     local_cd_nagios_content.txt


Last edited by zaxxon; 04-12-2012 at 10:02 AM.. Reason: shortened filter
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 04-12-2012
Thanks, zaxxon Smilie
Looking forward for perl language to solve this question too ^^
# 4  
Old 04-12-2012
You can convert awk to perl using a2p...
Code:
$ echo '!/^c/ {sub(/_/,"\t"); print}'|a2p
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
                        # this emulates #! processing on NIH machines.
                        # (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
                        # process any FOO=bar switches

$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

while (<>) {
    chomp;      # strip record separator
    if (!/^c/) {
        s/_/\t/;
        print $_;
    }
}

This User Gave Thanks to Ygor For This Post:
# 5  
Old 04-12-2012
So...
Code:
$ ls *.txt|perl -n -e 'if (!/^c/) { s/_/\t/; s/.txt//; print $_ }'
data    local_cd
data    local_cd_httpd
new     local_cd_mysql
new     local_cd_nagios_content

This User Gave Thanks to Ygor For This Post:
# 6  
Old 04-12-2012
Hi zaxxon, do you mind to explain what is "^c" in your command?
Many thanks Smilie

---------- Post updated at 08:50 PM ---------- Previous update was at 08:49 PM ----------

Hi ygor,

Do you mind to explain what is the meaning of "^c" in your code?
Thanks Smilie
# 7  
Old 04-13-2012
^[^c]
The 1st ^ stands for start of line.
The other ^ inside the square brackets (group of characters) is a negation of the characters inside the bracket ie. the group of characters, which is here just the c. It just means "All characters but a c that start the line".
This User Gave Thanks to zaxxon For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can someone convert this python script to perl

There is a python script that I would like converted to a perl script. If someone has the time to convert the script I would appreciate it. You can find the script below: reboot-mb8600/reboot-mb8600.py at master . j4m3z0r/reboot-mb8600 . GitHub #!/usr/bin/python ''' A hacky script to... (1 Reply)
Discussion started by: azdps
1 Replies

2. Shell Programming and Scripting

Convert awk line into perl

I have an awk statement in a ksh script that looks for a certain string then looks at each line after to find another match. The match could be the next line or second down and it works well. nawk 'BEGIN {FS=RS;RS="!"} /interface loopback0/ {for(i=1;i<=NF; i++) if ($i ~ /ip... (5 Replies)
Discussion started by: numele
5 Replies

3. Shell Programming and Scripting

Convert rows into columns using awk or perl

hi friends, i am able to parse cvs diff file using bit of cut and grep commands to produce following output in text file '''cvs-diff.txt''' Package-Name = dev-freetype. Old-Version = 2.4.8 New-Version = 2.4.10 Patches-removed = freetype-2.4.8-cross-compile.patch... (2 Replies)
Discussion started by: alexzander18
2 Replies

4. Shell Programming and Scripting

Convert awk command to Perl

Hi all; I have a Perl script that uses this awk command wrapped in a "system" call; it works exactly as I want it; however I want to keep this a Perl script and have very few if any "system" command...so the question; can someone help me code this in Perl please ... i have tried an now I am... (1 Reply)
Discussion started by: gvolpini
1 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

Help: how to convert perl script to awk in windows2003 server environment

For the following perl script, can anyone help to convert it to awk statement in windows2003 server environment ? Code: foreach $k (sort {$a <=> $b} keys %psnum) (1 Reply)
Discussion started by: tojzz
1 Replies

7. Shell Programming and Scripting

Convert .sh script into perl

Good afternoon to you all I really need your help I have the following script developed in .sh and I need to convert it into perl. Can someone help me do it please? Here´s the script: ############################################## ############################################## ... (3 Replies)
Discussion started by: zarahel
3 Replies

8. Shell Programming and Scripting

Need to convert ksh script to Perl

Guys I am new to this forum, this may seem like a cheeky request. I have been asked by my manager to convert this ksh script to Perl. I do not have the foggiest about Perl and would appreciate any help on this. Basically this scipt automates a recovery process for EMC Legato Networker. It will... (1 Reply)
Discussion started by: rahimm1
1 Replies

9. Shell Programming and Scripting

convert unix script to perl

Hi, I have these lines in a unix script: FILEONE = /<filepath1>/<filename1.txt> FILENEW = /<filepath2>/<filename2.txt> head -5 $FILEONE | sed '1d' > $FILENEW PARAM1 = `cat $FILENEW | awk '{print $2;}' ` echo "Param1 Value: $PARAM1" What's the correct syntax of the above lines if same... (2 Replies)
Discussion started by: gholdbhurg
2 Replies

10. Shell Programming and Scripting

awk to perl convert

Dear Collegue Do anybody help me to convert this AWK script to Perl script { for (i = 2; i <= length ($0); i++) { x = substr($0, i , 1) if (c > 0) { b = b x if (x == "(") c++ ... (2 Replies)
Discussion started by: jaganadh
2 Replies
Login or Register to Ask a Question