Two columns in leafpad or geany ??

Using applications, configuring, problems
Message
Author
Burunduk
Posts: 80
Joined: Sun 21 Aug 2011, 21:44

#61 Post by Burunduk »

Maybe it's OK to mention this old utility here.

Code: Select all

pr -mt -S"|  " <(fold -s -w30 left.txt) <(fold -s -w30 right.txt) | expand >merged.txt

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#62 Post by greengeek »

Burunduk wrote:Maybe it's OK to mention this old utility here.

Code: Select all

pr -mt -S"|  " <(fold -s -w30 left.txt) <(fold -s -w30 right.txt) | expand >merged.txt
That is really clever. Thanks!

The only problem is that i cannot seem to get text editors to handle Landscape mode. The lines wrap too early. I really need to be able to use Landscape.

EDIT: Also this method has a similar issue to musher0's earlier suggestion - it means that when i am re-editing the document i must pay very careful attention to what is happening with "insert" and movement / overwriting of the pipe characters.

Nonetheless - it seems these methods could be useful if I am maintaining WorkToDO and PersonalToDo as separate text files ready for inclusion into the merged output document..

thanks!
Attachments
merged_text.jpg
(25.11 KiB) Downloaded 318 times

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#63 Post by greengeek »

Burunduk wrote:Maybe it's OK to mention this old utility here.

Code: Select all

pr -mt -S"|  " <(fold -s -w30 left.txt) <(fold -s -w30 right.txt) | expand >merged.txt
Hi Burunduk - do you have any idea why the pipe characters are "stuck" in column 34? I can see how the "w30" parameter determines the wrapping of the text contents, but i see no way to move the pipe characters to make a wider column in the merged output.
cheers!

(ps: maybe this is some sort of fixed kernel parameter related to console functions?)
- seems like page_width maxes out at 72 characters. I am struggling to understand why the pr output seems limited to 72 characters when leafpad can be forced to print landscape up to around 130 or 140 characters. Confusing :?
Last edited by greengeek on Thu 02 May 2019, 19:30, edited 1 time in total.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#64 Post by greengeek »

Just for future reference i will list here the syntax i found that lets me print the leafpad output as Landscape orientation (direct to whatever is set up as the default printer):

Code: Select all

lp -olandscape -ofp12 -ovsi5.45 -otl66 -ohsi9.5 myfile.txt
It prints with narrow margins which suits my needs perfectly. Apparently the number of characters accommodated across the page is determined by the choice of character set (which is one of those funny parameter strings in the code above...)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#65 Post by musher0 »

g\g?

What's your source for this?

Also, would you care to provide some details about what the parms do?

TIA.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#66 Post by greengeek »

musher0 wrote:g\g?

What's your source for this?

Also, would you care to provide some details about what the parms do?

TIA.
Source is here:
https://www.unix.com/unix-for-dummies-q ... scape.html

As to what the parameters do - i am still very much in the dark. All i know is that syntax allowed me to print a leafpad file in landscape mode and it came out on my default printer - a Brother MFC7820 (despite the fact that the originator of the syntax was using an HP printer).

In that thread the respondent does make some comments about what the parameters are - but seems printer specific and sounds very techo.

I reckon if i decide to use separate Work/Personal ToDo lists they can be combined into two columns on a landscape page then the output printed. I feel it is getting closer...

EDIT : I feel that the contents of a text file are more or less "width agnostic" whereas a text file displayed is usually constrained by some sort of kernel console limitation of 72 characters wide. And i feel that a text file printed is of variable width depending on what utility formats it for printing. (This may sound like nonsense but it represents 100% accurately my current state of confusion) :?

Burunduk
Posts: 80
Joined: Sun 21 Aug 2011, 21:44

#67 Post by Burunduk »

greengeek wrote:- seems like page_width maxes out at 72 characters. I am struggling to understand why the pr output seems limited to 72 characters when leafpad can be forced to print landscape up to around 130 or 140 characters.
This is the default width.

Code: Select all

pr -mt -o2 -S"*  " -W130 <(fold -s -w60 left.txt) <(fold -s -w60 right.txt) | expand >merged.txt
where -o2 indentation (witn 2 spaces)
-W130 page width (without the indent)

More information about pr

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#68 Post by greengeek »

Burunduk wrote:

Code: Select all

pr -mt -o2 -S"*  " -W130 <(fold -s -w60 left.txt) <(fold -s -w60 right.txt) | expand >merged.txt
Wow ! - thank you. That is very powerful and opens up some great possibilities for me.

The man page for "pr" says in a couple of places that the output page width is divided such that columns have the same width. Did I understand that correctly? Is that a global default with any pr syntax - so there is no option to have columns of different widths?

cheers!

EDIT : I changed the separator from * back to pipe |
Attachments
merged_text_leafpad.jpg
(70.11 KiB) Downloaded 223 times

Burunduk
Posts: 80
Joined: Sun 21 Aug 2011, 21:44

#69 Post by Burunduk »

greengeek wrote:so there is no option to have columns of different widths?
It is not difficult to work this limitation around. When the width of the first column is decreased, there is a gap between the right margin and the separator but we can use cut to exclude it from the output.

Code: Select all

pr -mt -S"*  " -W130 -o2 <(fold -s -w30 left.txt) <(fold -s -w60 right.txt) | expand | cut -c-33,64- >merged.txt
Ah, and I've found this elegant solution here

Code: Select all

paste one.txt two.txt | expand --tabs=50
After some tweaking it looks considerably less elegant:

Code: Select all

paste <(fold -s -w30 left.txt) <(fold -s -w60 right.txt) | sed 's/\t/&|  /' | expand -t33 >merged.txt
There should be no tabs in the input files.

But if your texts may contain non-ASCII characters, it's probably better to use a text processor.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#70 Post by greengeek »

Burunduk wrote:After some tweaking it looks considerably less elegant:

Code: Select all

paste <(fold -s -w30 left.txt) <(fold -s -w60 right.txt) | sed 's/\t/&|  /' | expand -t33 >merged.txt
Many thanks! This is getting very close to what I was looking for. The syntax works well in a terminal and works well in a bash script - but won't run for me in a shell script which means I can't seem to build it into a gtk gui.

I guess this means that the syntax contains a "bashism" or else I am failing to escape some variable properly. I have tried various quotes, backslashes, parentheses etc but just not hitting on the sweet spot.

Does anyone know how this can be changed to run in a shell script? What am I missing?

Code: Select all

# ./shellmerge
./shellmerge: line 3: syntax error near unexpected token `('
./shellmerge: line 3: `paste <(fold -s -w30 left.txt) <(fold -s -w60 right.txt) | sed 's/\t/&|  /' | expand -t33 >merged.txt'
#
(I am using a Slacko 5.6 derivative)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#71 Post by MochiMoppel »

greengeek wrote: works well in a bash script - but won't run for me in a shell script which means I can't seem to build it into a gtk gui.
??? A bash script is a shell script and I see no reason why it can't be used in a gtk gui.

The <(...) construct requires bash, so you have to make sure that your gui uses bash and not sh or anything else. Or use tmp files instead.

Burunduk
Posts: 80
Joined: Sun 21 Aug 2011, 21:44

#72 Post by Burunduk »

In other words, is there any good reason why you can't start your script with #!/bin/bash instead of #!/bin/sh?

The script below doesn't use process substitution. It also doesn't work very well. Perhaps I need to actually read that sed script inside.

Code: Select all

#!/bin/sh

# halfedit -- version 0.1

# Edit each column of a two column text file separately.

# This script is a draft.


editor="geany -i"

col1=30
col2=60
sep="|  "

seppos=$((col1 + 2))

# fmt.sed http://sed.sourceforge.net/grabbag/scripts/fmt.sed

fmtsed1=':a
/^.\{'$col1'\}/!{
	$q
	N
	s/ *\n */ /
	ba
}
s/^.\{'$col1'\}/&\
/
s/^\(.*\) \(.*\)\n/\1\
\2/
P
s/^.*\n//
ba'

fmtsed2=':a
/^.\{'$col2'\}/!{
	$q
	N
	s/ *\n */ /
	ba
}
s/^.\{'$col2'\}/&\
/
s/^\(.*\) \(.*\)\n/\1\
\2/
P
s/^.*\n//
ba'


if [ ! -f "$1" ]; then
    echo "usage: this_script file.txt"
    exit 1
fi

filename="${1##*/}"
filename="${filename%%.*}"

# split the file in two

sed -r 's/(.{'$seppos'}).*/\1/' "$1" >"$filename"-left.txt
sed -r 's/.{'$seppos'}.{'${#sep}'}(.*)/\1/' "$1" >"$filename"-right.txt

# start the text editor

echo "Opening files for editing."
echo "Close the editor to continue."

$editor "$filename"-left.txt "$filename"-right.txt

# merge the edited files

sed -i "$fmtsed1" "$filename"-left.txt
sed -i "$fmtsed2" "$filename"-right.txt
mv "$1" "$1".bak
paste "$filename"-left.txt "$filename"-right.txt | sed "s/\\t/&$sep/" | expand -t$seppos >"$1"
rm -v "$filename"-left.txt "$filename"-right.txt

echo done


User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#73 Post by greengeek »

MochiMoppel wrote:??? A bash script is a shell script and I see no reason why it can't be used in a gtk gui.
Burunduk wrote:In other words, is there any good reason why you can't start your script with #!/bin/bash instead of #!/bin/sh?
I did originally try substituting /bin/bash at the beginning of my gtk gui but it did not work so I assumed that the gui must remain in a /bin/sh form.

However, after your comments I am left with the inescapable conclusion that the problem is how I used variables within my gtk gui. I am trying to bring a variable ("$LEFTCOLUMNTEXTFILE") from the user gui into the "fold" portion of the bash script merge syntax and I obviously did it wrongly (many times and in many different ways...!)

I will change the gui to /bin/bash and will also focus on getting that fold syntax right. I had tried using /tmp files but that did not work so i must have stuffed that up too.... :?

Back to the drawing board for a while...

Just for the record here is where I got to with my gui iterations before my brain melted:

Code: Select all

#!/bin/bash

#greengeek 2019Apr30 GTK gui to assist with forming text version of ToDo list
#Merge syntax provided by Burunduk

export TEXTDOMAIN=ToDo2ColTextLister
export OUTPUT_CHARSET=UTF-8

DEFAULTBKUPDIR=/root
DEFAULTOUTNAME=/root/ToDo.txt

TMP="/tmp"

BUILD2COLUMNSOUT() {
	
STARTTIME=$(echo `date +%Y-%m-%d_%H%M%S`)
#gxmessage "start time = $STARTTIME"

COLUMN1=`cat "$LEFTCOLUMNTEXTFILE"`
COLUMN2=`cat "$RIGHTCOLUMNTEXTFILE"`

#Describe source file:
gxmessage "The text output file will be called '$OUTFILENAME-$STARTTIME'"

echo $LEFTCOLUMNTEXTFILE
echo $RIGHTCOLUMNTEXTFILE

#Script to build 2 column text output file
#paste "$LEFTCOLUMNTEXTFILE" "$RIGHTCOLUMNTEXTFILE" | expand --tabs=50 > "$OUTFILENAME-$STARTTIME"
paste <(fold -s -w30 "$LEFTCOLUMNTEXTFILE") <(fold -s -w60 "$RIGHTCOLUMNTEXTFILE") | sed 's/\t/&|  /' | expand -t33 > "$OUTFILENAME-$STARTTIME"
}
export -f BUILD2COLUMNSOUT

HELP () {
  defaultbrowser http://www.murga-linux.com/puppy/viewtopic.php?p=1027150#1027150
}
export -f HELP

# -------- End of Functions -----------

export PROG="
<window title="ToDo_2COLTEXT_gui_gg_0.1">
<vbox>
  <frame $(gettext 'Select text file to load to left hand column')>
    <hbox>
      <entry accept="columnleft">
      <variable>LEFTCOLUMNTEXTFILE</variable>
      </entry>
      <button>
        <input file stock="gtk-open"></input>
        <action type="fileselect">LEFTCOLUMNTEXTFILE</action>
      </button>
      <button>
       	<input file stock="gtk-clear"></input>
       	<action>Clear:LEFTCOLUMNTEXTFILE</action>
      </button>
    </hbox>
  </frame>
    <frame $(gettext 'Select text file to load to right hand column')>
    <hbox>
      <entry accept="columnright">
      <variable>RIGHTCOLUMNTEXTFILE</variable>
      </entry>
      <button>
        <input file stock="gtk-open"></input>
        <action type="fileselect">RIGHTCOLUMNTEXTFILE</action>
      </button>
      <button>
       	<input file stock="gtk-clear"></input>
       	<action>Clear:RIGHTCOLUMNTEXTFILE</action>
      </button>
    </hbox>
  </frame>
  <frame $(gettext 'Save the 2column text output as this filename:
(Timestamp will be appended automatically)')>
    <hbox>
      <entry accept="outfilename">
      <variable>OUTFILENAME</variable>
      <default>"$DEFAULTOUTNAME"</default>
      </entry>
      <button>
        <input file stock="gtk-open"></input>
        <action type="fileselect">OUTFILENAME</action>
      </button>
            <button>
       	<input file stock="gtk-clear"></input>
       	<action>Clear:OUTFILENAME</action>
      </button>
    </hbox>
  </frame>
  <hbox>
    <button>
       <label>$(gettext 'Create 2column ToDo list')</label>
       <action>BUILD2COLUMNSOUT</action>
    </button>
  </hbox>
    <hbox>
    <text><label>"   "</label></text>
  </hbox>
    <hbox>
    <button>
     <label>$(gettext 'Help')</label>
     <action>HELP &</action>
    </button>
    <text><label>"   "</label></text>
    <button><label>$(gettext 'Quit')</label></button>
  </hbox>
</vbox>
</window>
"

gtkdialog3 -c -p PROG

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#74 Post by MochiMoppel »

<action>bash -c BUILD2COLUMNSOUT</action>

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#75 Post by greengeek »

MochiMoppel wrote:<action>bash -c BUILD2COLUMNSOUT</action>
Thank you very much! There is no way in 10million iterations i would have stumbled across that answer :)

Attached below is version 0.2 of the two column text-output gui which produces columns of unequal width (as requested).

This is very close to what I was hoping for and I am getting quite excited. :P

Line 8 in the output has minor misalignment but i cannot see what is unusual about that line in the source "personal" file. (I am putting the personal todo in the skinny column on the left and the work todo where there is more space on the right)
(EDIT : Seems to be the apostrophe in "don't" that is upsetting the alignment. Maybe different encoding or something. That text was imported from an email)

I have attached a sample persToDo and workToDo.

Remove fake .gz suffixes

EDIT :New version of this gui here
Attachments
output.jpg
Line 8 funny but otherwise good progress
(45.04 KiB) Downloaded 72 times
ToDo_2colTEXT_gui_gg-0.2.gz
Remove fake .gz suffix then make executable.
(2.82 KiB) Downloaded 69 times
ToDoPers_Sample.txt.gz
Remove fake .gz suffix
(325 Bytes) Downloaded 63 times
ToDoWork_Sample.txt.gz
Remove fake .gz suffix
(383 Bytes) Downloaded 68 times
Last edited by greengeek on Sat 11 May 2019, 18:18, edited 1 time in total.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#76 Post by musher0 »

Maybe redact Sara's phone number...? One ever knows, in this day and age...
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#77 Post by greengeek »

musher0 wrote:Maybe redact Sara's phone number...? One ever knows, in this day and age...
Actually thats not anyone's number. It's fictitious. Well - supposed to be fictitious anyway. Probably connects to someone in the Bahamas or Moscow. :?

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#78 Post by musher0 »

Ah. Ok.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#79 Post by MochiMoppel »

greengeek wrote:Line 8 in the output has minor misalignment but i cannot see what is unusual about that line in the source "personal" file. ...
(EDIT : Seems to be the apostrophe in "don't" that is upsetting the alignment. Maybe different encoding or something.
Not maybe. If you *would* use MMview (SCNR :wink: ) you could see that this file is UTF-8 encoded while the file ToDoWork_Sample.txt is US_ASCII encoded. And if you would use the newest version you could even spot the problem, using 'Hexview' (not ''Hexdump'). It's indeed the apostrophe, which in this case is not the usual ASCII apostrophe but Unicode U+2019 (RIGHT SINGLE QUOTATION MARK). This character takes up 3 bytes instead of the usual 1 byte. The expand command calculates the tab stop based on bytes, not characters, so it sees 25 bytes in the text when you see only 23 characters, adds 8 spaces and voilà, 33 bytes.

Well, let's call this a bug. This is not how the expand command should work. Buggy in version 8.19 and still buggy in 8.24.
The solution: use busybox expand instead. It's not the first time that busybox is better maintained than the GNU utilities.

Still your code will fail if your text files contain tabs. Never ever happens? OK, that's for you to decide. Otherwise you will need a more robust solution, e.g. awk instead of expand.
Attachments
3_byte_character_in_text.png
(69.48 KiB) Downloaded 159 times

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#80 Post by greengeek »

MochiMoppel wrote:Well, let's call this a bug. This is not how the expand command should work. Buggy in version 8.19 and still buggy in 8.24.
The solution: use busybox expand instead. It's not the first time that busybox is better maintained than the GNU utilities.
Awesome analysis, thank you.

Now corrected to

Code: Select all

busybox expand
pending future experimentation with other options.

Meantime I will work with what you guys have kindly given me so far and hope to structure the gui so that I can easily choose between equal or non-equal column widths, and between portrait and landscape text outputs.

Will post another gui when tested.

cheers!

PS: I am not sure how i end up with some files ascii and some utf-8. I suspect my leafpad and geany are set up differently.

Post Reply