LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-21-2010, 05:18 PM   #31
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453

Quote:
Originally Posted by suse_nerd View Post
Care to give me a little more guidance? I am not that proficient at regex or perl.
I got that I need to do something like this
Code:
$header='~m/^.{8}(.{8})(.{20})(.{8})(.{4})';
$body='(.{8})(.{20})(.{8})(.{4})';
$foot='{2}';
But how to put this into a loop I am not sure. Also still don't really know what you mean by $N. Do you mean the nth ($N) character I split?

How do you detect the end footer? Look ahead by one character and see if it is a $?
By $N I mean $1, $2, $3, etc you've already used.

I do not understand why you need a loop - every line which comes you split into chunks using all those $N variables. You are already doing this, don't you ?

Sorry, but being able to match a character literally is a very basic knowledge, and the tutorial I suggested to read really has this info - somewhere in the beginning.

Your $header, $body, $foot code doesn't make sense to me. Do you understand how the $N variables work ? If not, it's described in the already suggested tutorial.
 
Old 04-21-2010, 09:17 PM   #32
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
Here is an alternative:
Code:
#!/usr/bin/awk -f

BEGIN{
		FS=""
		codel=8
		batchl=20
		expiryl=8
		quantityl=4
		start=1
}

{
		gsub(/\$/,"")
		header=gensub(/[^A-Z]*/,"","g")
		gsub(header,"")
		
		print header
		for(i=0;i<(NF/40);i++)
		{
			code = substr($0,start,codel)
			batch = substr($0,(start += codel),batchl)
			expiry = substr($0,(start += batchl),expiryl)
			quantity = substr($0,(start += expiryl),quantityl)
			start += quantityl

			printf "%d,%d,%d,%d\n",code,batch,expiry,quantity
		}
}
 
Old 04-22-2010, 02:52 AM   #33
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
By $N I mean $1, $2, $3, etc you've already used.

I do not understand why you need a loop - every line which comes you split into chunks using all those $N variables. You are already doing this, don't you ?

Sorry, but being able to match a character literally is a very basic knowledge, and the tutorial I suggested to read really has this info - somewhere in the beginning.

Your $header, $body, $foot code doesn't make sense to me. Do you understand how the $N variables work ? If not, it's described in the already suggested tutorial.
It's not every line though - it's only a single line.
I can do it by having 80+ $N variables, yes. That works!
 
Old 04-22-2010, 04:29 AM   #34
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Can I redirect the output to a file with that?
It works if I already have the barcode scanned into a file, I can just use the input and output parameters but what if i scan the barcode directly, it takes the input fine and outputs the fine also, but only to STDOUT

Last edited by suse_nerd; 04-22-2010 at 04:51 AM.
 
Old 04-22-2010, 06:29 AM   #35
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
I am not sure if you are talking to me or Sergei but in mine you can either redirect on the command line or at the print points in the code.

eg. the command line would look like:
Code:
./script input_file > output_file
 
Old 04-22-2010, 07:39 AM   #36
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
I am not sure if you are talking to me or Sergei but in mine you can either redirect on the command line or at the print points in the code.

eg. the command line would look like:
Code:
./script input_file > output_file
Yes thanks, I got tht to work. But if i just run the script, it waits for input. I wanted to just scan the barcode with the handheld scanner and have it either prompt for a file name or let me do something like

Code:
./script > output_file
 
Old 04-22-2010, 08:02 AM   #37
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
The script is waiting for a filename as it uses this to run its changes over, so yes without one it will wait.
When you run the barcode scanner normally, how does it pick the file to save into?
 
Old 04-22-2010, 08:25 AM   #38
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
The script is waiting for a filename as it uses this to run its changes over, so yes without one it will wait.
When you run the barcode scanner normally, how does it pick the file to save into?
It just outputs like a keyboard, it doesn't pick the file at all.
 
Old 04-22-2010, 09:40 PM   #39
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
Where does it output to?
 
Old 04-23-2010, 08:22 AM   #40
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
Where does it output to?

Hi Grail - the scanner just functions like a keyboard wedge, it just works like you were typing the data in manually.
 
Old 04-23-2010, 08:59 AM   #41
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
Ok, we are missing each other here. If you were at your desktop with no applications open and started typing, generally nothing
would happen unless you hit specific key combinations.
However, if you had a terminal or editor open, then what you typed would appear in those applications.

So, if I plug your scanner into the computer and scan a barcode, would my data go anywhere or does it also require the terminal/editor to be opened
first? Or does it simply send the data straight to a file?
 
Old 04-25-2010, 10:54 AM   #42
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
Ok, we are missing each other here. If you were at your desktop with no applications open and started typing, generally nothing
would happen unless you hit specific key combinations.
However, if you had a terminal or editor open, then what you typed would appear in those applications.

So, if I plug your scanner into the computer and scan a barcode, would my data go anywhere or does it also require the terminal/editor to be opened
first? Or does it simply send the data straight to a file?
Well I would open notepad/kwrite and it would appear directly in kwrite/notepad just as if you were typing. Likewise if you had cmd/shell open it would appear at the command prompt as if you were typing a command. Data won't go to a file unless you press save or pipe it manually into an echo command e.g.
type echo "
scan barcode
type " >> file
 
Old 05-16-2010, 12:13 AM   #43
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
Sorry I haven't gotten back to you on this one

I have been busy, but I do have an option although not necessarily very automated.
If you type the following:
Code:
./script > output_file
This will wait for input. So now you can scan using the reader and this will be entered.
If you hit enter after last scan and then the key combination - ctrl+d
This will now pass whatever has been scanned into the script and then into the file.

Sorry I cannot think of a more automated way. Hopefully someone else will see and suggest something, but for now this should work.
 
  


Reply

Tags
awk, barcode, barcodes, bash, decode, perl, process, processing, script, sed


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
[SOLVED] Looking for experience to make a script more efficient. aSingularity Programming 7 04-03-2010 01:41 AM
Next step to make this script more efficient...? redlinuxxx Programming 2 10-01-2009 09:42 AM
parsing script sthompson Linux - Server 14 06-10-2008 09:39 AM
LXer: Memory-efficient XML parsing in PHP with XMLReader LXer Syndicated Linux News 0 02-04-2007 05:33 PM
Help with Text Parsing Script NiallC Programming 6 06-24-2006 03:28 PM


All times are GMT -5. The time now is 04:30 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration