ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
cat $1 | sed -e 's/[ ][ ]*/,/g' | sed 's/,/,\n/g' | sed -e '$s/.....$//' - >> processing
cat processing | awk 'NR==1' | sed 's/^........//' >> processed
cat processing | awk 'NR==2,NR==100' | sed 's/^....//' >> processed
mv -i processed $1.csv
to process a particularly formatted PDF417 file.
It is formatted like this
$CENTAUR<code_length_8><batch_length_20><expiry_length_8><quantity_length_4>$
So a typical record looks like this
<code_length_8><batch_length_20><expiry_length_8><quantity_length_4>
(note, no spaces between each record or field if the field is present, otherwise white space for the length specified).
A file will never have more than 22 records.
The files I have been processing so far only have the quantity and code fields, so everything else is whitespace and my script doesn't take the other fields into account. I would like it to make a csvfile like this:
<LF> = linefeed/new line
<> separate fields only to show the format and are not actually present in the barcode output
It is actually a file scanned directly from a barcode output - so the PDF417 barcode is scanned and the file is saved in notepad++ - can it be scanned directly into the script?
example file - not sure how much formatting will be retained on the forum.
[code]
cat $1 | sed -e 's/[ ][ ]*/,/g' | sed 's/,/,\n/g' | sed -e '$s/.....$//' - >> processing
cat processing | awk 'NR==1' | sed 's/^........//' >> processed
cat processing | awk 'NR==2,NR==100' | sed 's/^....//' >> processed
mv -i processed $1.csv
[code]
to process a particularly formatted PDF417 file.
It is formatted like this
$CENTAUR<code_length_8><batch_length_20><expiry_length_8><quantity_length_4><code_length_8>....001$
(note, no spaces between each record or field) n
The files I have been processing so far only have the quantity and code fields, so everything else is whitespace and my script doesn't take the other fields into account. I would like it to make a csvfile like this:
It is actually a file scanned directly from a barcode output - so the PDF417 barcode is scanned and the file is saved in notepad++ - can it be scanned directly into the script?
example file - not sure how much formatting will be retained on the forum.
Thanks, but this is about PDF417 barcodes, rather than PDF files.
Your example shows whitespaces, but you wrote: "(note, no spaces between each record or field)".
Anyway, are fields of the matter of constant width ? If yes, in Perl constant width fields can be extracted by regular expression like this:
Code:
if($line =~ m/^(.{3})(.{5})(.{8})/)
{
print "\$1=$1 $2=$2 \$3=$3"; # $1 contains the first 3 characters, $2 contain the following 5 characters, $3 contains the following 8 charactes
}
There is no whitespace if all fields are present.
If fields are missing, like in my example, there are whitespaces to the length of the missing fields.
So would something like this work (after stripping the header and footer)
There is no whitespace if all fields are present.
If fields are missing, like in my example, there are whitespaces to the length of the missing fields.
So would something like this work (after stripping the header and footer)
Will this syntax definitely work? I need the commas between each field and the new line at the end.
I assume it will just discard the $ at the end of the file.
Perl script runs, but doesn't output anything -how do I get it to output?
Edit: There is no carriage returns, everything is output on one line - is this script expecting a LF or CR? Also it only needs to skip the header once for each output - will this work?
Something like this?
Code:
#!/usr/bin/perl
open (MYFILE, '>>data.txt');
while(defined(my $line = <STDIN>))
{
if($line =~ m/^.{8}(.{8})(.{20})(.{8})(.{4})/)
{print MYFILE "\$1=$1\,\$2=$2\,\$3=$3\,\$4=$4\n";}
}
close (MYFILE);
Will this syntax definitely work? I need the commas between each field and the new line at the end.
I assume it will just discard the $ at the end of the file.
Perl script runs, but doesn't output anything -how do I get it to output?
Edit: There is no carriage returns, everything is output on one line - is this script expecting a LF or CR? Also it only needs to skip the header once for each output - will this work?
Something like this?
Code:
#!/usr/bin/perl
open (MYFILE, '>>data.txt');
while(defined(my $line = <STDIN>))
{
if($line =~ m/^.{8}(.{8})(.{20})(.{8})(.{4})/)
{print MYFILE "\$1=$1\,\$2=$2\,\$3=$3\,\$4=$4\n";}
}
close (MYFILE);
Still not outputting anything though.
First and foremost - put
Code:
use strict;
use warnings;
just after
Code:
#!/usr/bin/perl
.
You do not need backslashes before commas.
You need to debug the script - first make sure the lines are indeed read from STDIN, for this just before the 'if' statement put
For that matter, I do not understand why/how the script managed to print even one $line.
I think because it's waiting for a new line, so when i press the enter key, it outputs the value of $line as specified in the warn statement.
I open and closed the file within the if statement and now it outputs to file properly. However, it seems only works on the first field and doesn't loop the regex search/parse until the end of the line.
I think because it's waiting for a new line, so when i press the enter key, it outputs the value of $line as specified in the warn statement.
I open and closed the file within the if statement and now it outputs to file properly. However, it seems only works on the first field and doesn't loop the regex search/parse until the end of the line.
So I get something like:
Code:
30293644, ,20042010,0001
Why won't you yo do two things:
chmod +x your_script.pl
./your_script.pl < input_data_file
?
Adjust path to your script and to your input data file as needed in the above.
Last edited by Sergei Steshenko; 04-21-2010 at 10:15 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.