SED Help and Explanation

This is the place for queries that don't fit in any of the other categories.

SED Help and Explanation

Postby ylafont » Sun May 25, 2014 3:12 pm

I have the following sed line

Code: Select all
      sed -nr \
         -e '/^SCANNING:/{s///; s/\(us-irc:|\(us-hrc:|\(us-cable:/\t/; s/,/\t/; s/us-cable://; s/\)/\t/;h}' \
         -e '/^PROGRAM/{s///; /: 0$/d; s/:/\t/; s/\//-/; s/MC://; s/ \(encrypted\)//; G;s/(.*)\n(.*)/\2\1/p}' "$HDHomeID".log


Wich is supposed to parse out a log file like thins

SCANNING: 681000000 (us-irc:105, us-cable:105)
LOCK: qam256 (ss=100 snq=100 seq=100)
TSID: 0x021B
PROGRAM 340: 340 STARZ (encrypted)
PROGRAM 341: 341 STZCI (encrypted)
PROGRAM 342: 342 STZK (encrypted)
PROGRAM 343: 343 STZE (encrypted)
PROGRAM 344: 344 STZB (encrypted)
PROGRAM 345: 345 STZw (encrypted)
PROGRAM 346: 346 STZCO (encrypted)
PROGRAM 350: 350 ENC (encrypted)
PROGRAM 351: 351 ENCA (encrypted)
PROGRAM 352: 352 ENCS (encrypted)
PROGRAM 353: 353 ENCWS (encrypted)
PROGRAM 354: 354 ENCC (encrypted)
PROGRAM 355: 355 ENCB (encrypted)
PROGRAM 356: 356 ENCF (encrypted)
PROGRAM 357: 357 ENCw (encrypted)



and should create a line for each "program line" like this

Code: Select all
681000000    105    105    340    340 STARZ
 681000000    105    105    341    341 STZCI
 681000000    105    105    342    342 STZK
 681000000    105    105    343    343 STZE
 681000000    105    105    344    344 STZB
 681000000    105    105    345    345 STZw
 681000000    105    105    346    346 STZCO
 681000000    105    105    350    350 ENC
 681000000    105    105    351    351 ENCA
 681000000    105    105  352    352 ENCS
 681000000    105    105    353    353 ENCWS
 681000000    105    105    354    354 ENCC
 681000000    105    105    355    355 ENCB
 681000000    105    105    356    356 ENCF
 681000000    105    105    357    357 ENCw



but instead it gives me this

681000000 105 105
340 340 STARZ
681000000 105 105
341 341 STZCI
681000000 105 105
342 342 STZK
681000000 105 105
343 343 STZE
681000000 105 105
344 344 STZB
681000000 105 105
345 345 STZw
681000000 105 105
346 346 STZCO
681000000 105 105
350 350 ENC
681000000 105 105
351 351 ENCA
681000000 105 105
352 352 ENCS
681000000 105 105
353 353 ENCWS
681000000 105 105
354 354 ENCC
681000000 105 105
355 355 ENCB
681000000 105 105
356 356 ENCF
681000000 105 105
357 357 ENCw



Can anyone explain where the extra line feed is coming from? thank you.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: SED Help and Explanation

Postby stranac » Sun May 25, 2014 3:20 pm

Why post about your sed problem on a python forum?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: SED Help and Explanation

Postby ylafont » Sun May 25, 2014 3:59 pm

Forgive me stranac

I have though i had used this forum previously for this. My mistake.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: SED Help and Explanation

Postby 7stud » Sun May 25, 2014 5:36 pm

I suggest you learn python so that you don't have to write painful sed code like that:


Code: Select all
import re

f = open('mylog.txt')

for line in f:
    if line.startswith("SCANNING"):
        long_num, us_irc, us_cable = re.findall(r"\d+", line)

    elif line.startswith("PROGRAM"):
        pieces = re.split(r"\s+", line)
        num1 = pieces[1].rstrip(":")
        num2 = pieces[2]
        cable_provider = pieces[3]

        print "{} \t {} \t {} \t {} \t {} {}".format(
            long_num,
            us_irc,
            us_cable,
            num1,
            num2,
            cable_provider,
        )

f.close()

--output:--
681000000     105     105     340     340 STARZ
681000000     105     105     341     341 STZCI
681000000     105     105     342     342 STZK
681000000     105     105     343     343 STZE
681000000     105     105     344     344 STZB
681000000     105     105     345     345 STZw
681000000     105     105     346     346 STZCO
681000000     105     105     350     350 ENC
681000000     105     105     351     351 ENCA
681000000     105     105     352     352 ENCS
681000000     105     105     353     353 ENCWS
681000000     105     105     354     354 ENCC
681000000     105     105     355     355 ENCB
681000000     105     105     356     356 ENCF
681000000     105     105     357     357 ENCw
Last edited by 7stud on Sun May 25, 2014 6:44 pm, edited 4 times in total.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: SED Help and Explanation

Postby ylafont » Sun May 25, 2014 5:48 pm

unfortunately,

It is easier to do it sed or awk than python.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: SED Help and Explanation

Postby stranac » Sun May 25, 2014 6:23 pm

ylafont wrote:
Code: Select all
      sed -nr \
         -e '/^SCANNING:/{s///; s/\(us-irc:|\(us-hrc:|\(us-cable:/\t/; s/,/\t/; s/us-cable://; s/\)/\t/;h}' \
         -e '/^PROGRAM/{s///; /: 0$/d; s/:/\t/; s/\//-/; s/MC://; s/ \(encrypted\)//; G;s/(.*)\n(.*)/\2\1/p}' "$HDHomeID".log

How is that easy?
And if it's easy, why is it not working correctly?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: SED Help and Explanation

Postby ylafont » Sun May 25, 2014 7:38 pm

did not mean to offend anyone, 2 lines versus 20 seams simpler. Yes, it is a little bit crypted to understand,but it is not that bad.

thanks for the initiative 7stud,
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: SED Help and Explanation

Postby 7stud » Sun May 25, 2014 7:49 pm

No offense taken. We're just discussing different options.

2 lines versus 20 seams simpler.


That's an illusion. Brevity is never the goal; code clarity should be the goal. Your sed script looks like it might be a maintenance nightmare. The advantage sed has is speed, which can be an important consideration.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: SED Help and Explanation

Postby ylafont » Sun May 25, 2014 8:24 pm

Can't disagree with that!

Anyway, the problems as not with the SED Command, looks to be a file encoding problem when creating a file in on a windows machine and transferring to to Linux platform. Once i cut an pasted into a new file, it worked as expected.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: SED Help and Explanation

Postby 7stud » Sun May 25, 2014 11:29 pm

creating a file in on a windows machine and transferring to to Linux platform

One obvious consequence of creating a file on windows is: the character sequence at the end of lines will be "\r\n" instead of just "\n".
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], puck2020 and 3 guests