LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 09-28-2012, 05:42 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
awk: same test on same file by different scripts gives different result


Never mind, hex editor showed problem. The line is actually '<table> '. Sorry for the false alarm.

The same if test is applied to the same data in both these awk programs. In the first it works as expected, in the second it blows right past it.
Code:
~/Documents $ awk '{print $0; if ($1 == "<table>") {print "found it"; exit;}}'  sccs.txt
<H1><a name="Screen Keyboard Shortcuts">Screen Keyboard Shortcuts</a></H1> 
<a href="#Screen Keyboard Shortcuts">Screen Keyboard Shortcuts</a> &nbsp; 
<table> 
found it
~/Documents $ ht sccs.txt
swx=0<H1><a name="Screen Keyboard Shortcuts">Screen Keyboard Shortcuts</a></H1> 
swx=0<a href="#Screen Keyboard Shortcuts">Screen Keyboard Shortcuts</a> &nbsp; 
swx=0<table> 
swx=0Shortcut   Command Description
swx=0^a '       select  Prompt for a window name or number to switch to.
swx=0^a "       windowlist -b   Present a list of all windows for selection.
swx=0^a 0...9   select 0...9    Switch to window number 0...9.
The code for the second program that I run with the alias 'ht' is
Code:
#!/bin/bash
# Creates an html table from each <table>\n ... \n</table> in the document
# In your text, each cell is separated from the next by a single <Tab> character
# There must be the same number of cells in each line of that table
# The end of a table Row must be indicated by an empty line in the text
# meaning that multiple lines of text may be integrated into the same table Row
# Run with ht /file/path, ofilename is ifilename.html
#
awk 'BEGIN {FS = "\t"; OFS = ""; swx = 0}
function prrow(dol, j, b, nb) 
{
  lin = "<tr>"; for (i = 1; i < j; i++) {lin = lin b dol[i] nb}
  delete dol; print lin "</tr>"
}
{
if ($0 == "") {prrow(dol, j, b, nb); b = "<td>"; swy = 1; nb = "</td>"; next}
if ($1 == "<table>") {print "found it"; exit}
if ($1 == "<table>") {b = "<td><b>"; nb = "</b></td>"; swx = 1; print $0; next}
if ($1 == "</table>") {swx = 0; if (swy != 1) {prrow(dol, j, b, nb)}; print $0; next}
if (swx == 0) {print "swx=", swx, $0; next}
for (j = 1; j<= NF; j++) {dol[j] = dol[j] $j "<br>"}
swy = 0
}
' < $1 #> $1'.html'
Any suggestions would be appreciated. Input file is attached.
Attached Files
File Type: txt sccs.txt (3.0 KB, 12 views)

Last edited by porphyry5; 09-28-2012 at 06:47 PM. Reason: Found problem
 
Old 09-30-2012, 05:26 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Glad you got it solved, and well done for adding the solution to your post just bumped this off the zero-reply list.

Regards,
 
  


Reply



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
awk scripts for trace file mikes1313 Linux - Newbie 7 04-18-2013 05:11 PM
[SOLVED] Pass Command Result to Awk BarataPT Programming 7 04-15-2011 09:23 AM
awk - gensub first result webhope Programming 3 05-27-2010 05:52 AM
Specific result of awk script jeesun Programming 3 01-24-2010 05:42 AM
Getting awk to extract scripts from a file jspaceman Programming 5 11-24-2002 06:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration