473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

how to write it in one line..

fetaelin
Hi I have a litle script and want to write it in one line
I dont know how to do that .. here is the script.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open(FILE1, "<marenden");
  3. open(FILE3, ">>result");
  4.  
  5. while($line = <FILE1>) {
  6.   chop($line);
  7.   $temp=`grep $line file2.txt`;
  8.   print FILE3 $temp;
  9. }
  10.  
Jun 14 '07 #1
6 1557
miller
1,089 Expert 1GB
One Line :)

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open(FILE1, "<marenden"); open(FILE3, ">>result"); while($line = <FILE1>) {chop($line); $temp=`grep $line file2.txt`; print FILE3 $temp;}
  3.  
- Miller
Jun 14 '07 #2
miller
1,089 Expert 1GB
Hi Fetaelin,

As you already know, it often is possible to create one-liners for a lot of the most typical parsing needs. However, I generally avoid this as anything more than a curiousity.

In fact your currect script, if it's hoping to do what I think it is, is actually broken. I believe that the most important goal should be to make clear readable code, not ultra compact.

To that end, here is how I personally would do the project that you have listed:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use File::Slurp qw(read_file);
  4.  
  5. use strict;
  6.  
  7. my $file1 = 'marenden';
  8. my $file2 = 'file2.txt';
  9. my $outfile = 'result';
  10.  
  11. my %match = map {$_ => 1} grep {! /^\s*$/} read_file($file1);
  12.  
  13. open(IN, $file2) or die "Can't open $file2: $!";
  14. open(OUT, ">>$outfile") or die "Can't open $outfile: $!";
  15.  
  16. while (<IN>) {
  17. print OUT if $match{$_};
  18. }
  19.  
  20. close(IN);
  21. close(OUT);
  22.  
As you can see, this is actually more lines of code because of the separation of the file name declarations. It also would be longer still if I hadn't used the File::Slurp module as a shortcut for reading in $file1. However, this code is more readable, and definitely easier to debug.

I suggest that you first aim to write a script like this before you obsess about one-liners.

- Miller
Jun 14 '07 #3
What I ment was how to write it in one line in a command prompt and not have it as an script.
ex.:
bash$: perl the scrit as one line here...
Jun 15 '07 #4
miller
1,089 Expert 1GB
I know. All my previous statements still apply.

- Miller
Jun 15 '07 #5
hello
if you want write in one line at the command prompt so write like this

CODE
$>perl -e 'open(FILE1, "<marenden"); open(FILE3, ">>result"); while($line = <FILE1>) {chop($line); $temp=`grep $line file2.txt`; print FILE3 $temp;}'

regard'd
Lokee.......
Jun 15 '07 #6
miller
1,089 Expert 1GB
Here's the closest that I believe is possible:

>perl -e"use File::Slurp ':all';%m=map{$_=>1}read_file('a.txt');append_file ('c.txt',grep{$m{$_}}read_file('b.txt'))"

Note that it does require File::Slurp, which is not a core module.

- Miller
Jun 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole*...
11
by: Ken | last post by:
How do I force the text generated by a document.write command to wrap after a specified number of characters? Thanks for the help.
3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
6
by: john.lum | last post by:
I am using document.write in an external javascript file to display some content, but I've found that I can't rely on that content being displayed in the location I expect. Here's a very simple...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
3
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a file - I tried different versions of write() but...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.