Author Topic: command line parsing long text line  (Read 957 times)

Offline wedgetail

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 2469
  • Any Bugs in site?
command line parsing long text line
« on: March 30, 2012, 10:43:01 PM »
TBercaht

Scripts  ;D can you roll something that will turn this 574 character long line into some 20 odd lines. The 'fields' are 'space' separated.

I am wearing the tips off my fingers to turn that line into 'instantly readable' format, like this:

http://www.pclinuxos.com/forum/index.php/topic,103852.msg886113.html#msg886113

[root@localhost ~]# lpoptions -p Brother-HL-2040

Ind code tags:

Code: [Select]
auth-info-required=none copies=1 device-uri=parallel:/dev/lp0 finishings=3 job-hold-until=no-hold job-priority=50 job-sheets=none,none marker-change-time=0 number-up=1 printer-commands=AutoConfigure,Clean,PrintSelfTestPage printer-info='Brother HL-2040' printer-is-accepting-jobs=true printer-is-shared=false printer-location=Lower_Right_Shelf printer-make-and-model='Brother HL-2060 Foomatic/hpijs-pcl5e' printer-state=3 printer-state-change-time=1332711831 printer-state-reasons=none printer-type=10661908 printer-uri-supported=ipp://localhost:631/printers/Brother-HL-2040
In quote tags:

Quote
auth-info-required=none copies=1 device-uri=parallel:/dev/lp0 finishings=3 job-hold-until=no-hold job-priority=50 job-sheets=none,none marker-change-time=0 number-up=1 printer-commands=AutoConfigure,Clean,PrintSelfTestPage printer-info='Brother HL-2040' printer-is-accepting-jobs=true printer-is-shared=false printer-location=Lower_Right_Shelf printer-make-and-model='Brother HL-2060 Foomatic/hpijs-pcl5e' printer-state=3 printer-state-change-time=1332711831 printer-state-reasons=none printer-type=10661908 printer-uri-supported=ipp://localhost:631/printers/Brother-HL-2040


I will eventually get around to pose the problem in a separate topic.   ;D ;D

Any suggestions?
« Last Edit: March 30, 2012, 11:45:04 PM by wedgetail »
32 bit: KDE (older) & various KDE-mini, ASUSTek P5P41D Rev X.0x, BIOS AMI0207 07/21/2009, "Pentium(R) Dual-Core CPU E5300 @ 2.60GHz", nVidia GeForce 9600 GT, 2x1GB Seagate Technology 1000528AS HDD
TV CompuPro VideoMate Vista E700 (not working in Linux), Acer X243HD LCD Screen

Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: command line parsing long text line
« Reply #1 on: March 30, 2012, 11:03:12 PM »

Quote
lpoptions -p Brother-HL-2040  | sed -e 's/ /\n/g'

Quote
auth-info-required=none
copies=1
device-uri=parallel:/dev/lp0
finishings=3
job-hold-until=no-hold
job-priority=50
job-sheets=none,none
marker-change-time=0
number-up=1
printer-commands=AutoConfigure,Clean,PrintSelfTestPage
printer-info=Brother
HL-2040
printer-is-accepting-jobs=true
printer-is-shared=false
printer-location=Lower_Right_Shelf
printer-make-and-model=Brother
HL-2060
Foomatic/hpijs-pcl5e
printer-state=3
printer-state-change-time=1332711831
printer-state-reasons=none
printer-type=10661908
printer-uri-supported=ipp://localhost:631/printers/Brother-HL-2040

AS

Offline Rudge

  • Hero Member
  • *****
  • Posts: 9773
  • I'm Just A Dog.
Re: command line parsing long text line
« Reply #2 on: March 30, 2012, 11:30:04 PM »
I think a "break" might be what is needed. To break the line and continue the code on a new line, add a "\" to the end of the line and then press the "enter" key to start a new line.

The new line will be treated as a continuation of the previous line.

This is at least, how it works on the command line.

"No need for a disclaimer, you guys already know I'm an idiot." ;)
« Last Edit: March 30, 2012, 11:36:30 PM by Rudge »


-If you wish to make an apple pie from scratch, you must first invent the universe-  Carl Sagan

Offline wedgetail

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 2469
  • Any Bugs in site?
Re: command line parsing long text line
« Reply #3 on: March 31, 2012, 02:32:23 AM »
AS
Thanks, it would have taken me a long time to dare trying sed, I have better look at it a bit closer.

Quote
lpoptions -p xxxx  | sed -e 's/ /\n/g'


Quote
lpoptions -p xxxx  | tr " " "\n"


They seem to deliver the same result on my system.

Inside the string there is one instance of

Quote
printer-make-and-model='Brother HL-2060 Foomatic/hpijs-pcl5e'


The 3 spaces gets evaluated, I think because of the single quotes ('). Just in case I checked if (") double quotes would make a difference. Didn't seem to.

Since the long string is a response from the CUPS command I think I am going to leave it and just manually correct if I need to.

Saving the lpoptions -p xxxx to my file 120331-lpop.glk allowed me to do a few quick tests on combinations of ' and " in the string.

Quote
tr  " " "\n" <120331-lpop.glk >120331-lpop-done.glk


Rudge
The break would work fine if I was in control of the line but I am not.  CUPS for some reason in the instructions for lpoptions -p xxxx or lpoptions -d xxxx (appear to give the same result) do not output the result with new lines, making it hard to read in a hurry. Often it is only 3 lines of interest. If code tags are used the very long line show up. I quote tags are used it is slightly easier to read as no mouse movement needed.  In both cases it is a delicate process to do a copy/paste without having to correct content in the buffer.

Having the information based on lines, the copy paste is easier, nor problem with over shooting the mark at the end of a line etc..   ;D   The two suggestions I now find however seem to be magical when I run it on my own pc, and I am likely to use them in posts in the future.  This leaves me only with the situation where I am copying a 'long line' from Forum, at the moment that is slightly longer way to save it in a file and run the 3rd line.

I am rather happy with what I have got o far as the novelty has worn off parsing those lines.

Quote
lpoptions -p xxxx -l


Ah yes, xxxx of course is your printer or printer queue name in

http://localhost:631/printers/

Can you resist clicking  :D

TBercaht
Thanks, you got me motivated and this works, will save me a lot of work.  Regarding not being in this corner, notice we are under supervision here by able practitioners, where better when we are unraveling, to us,  command line mysteries.   :)  
« Last Edit: March 31, 2012, 04:05:05 AM by wedgetail »
32 bit: KDE (older) & various KDE-mini, ASUSTek P5P41D Rev X.0x, BIOS AMI0207 07/21/2009, "Pentium(R) Dual-Core CPU E5300 @ 2.60GHz", nVidia GeForce 9600 GT, 2x1GB Seagate Technology 1000528AS HDD
TV CompuPro VideoMate Vista E700 (not working in Linux), Acer X243HD LCD Screen