You are not logged in.

#1 2017-06-24 08:01:27

Naik
Member
From: the edge of insanity
Registered: 2015-10-03
Posts: 328

[solved]need help processing text in bash

Hello folks!

I have written a little script to show what packages are upgradeable:

1. the crontab:

@reboot apt-get update && apt list --upgradable > /home/naik/upgradeable

2. the script:

#!/bin/bash

list=$(cat ~/upgradeable | sed -n '1!p' | sed  -e 's/\[*\]/\n/g')
date=$(ddate)
usr=$USER
echo 'Hallo '$usr'!'
echo $date
echo '#############UPGRADEABLE############'
echo $list

As you can see I inserted the 2nd sed call to replace anything between [] with a newline to beautify the output, but the result still looks like this:

Bildschirmfoto_2017-06-24_09-27-03.th.png

(I`m piping the output through ponysay so i`m a little limited in terms of line length)

I tried several methods to achieve this, but none of them seems to work. Can somebody provide me with a solution to replace anything written in square brackets with newlines in any given text?

Thanks in advance and have a nice day Folks!

naik --greetz

Last edited by Naik (2017-06-24 18:06:59)


"Kaum macht [Mensch]* es richtig, funktioniert es sofort!"
BL-Kitchen Codeberg

Offline

#2 2017-06-24 10:46:43

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: [solved]need help processing text in bash

Hey Naik smile

This works for me:

empty@testbed:~ $ list=$(sed -n '1!p' upgradable | sed 's/\[.*\]//')
empty@testbed:~ $ echo "$list"
exim4-base/stable 4.89-2+deb9u1 amd64
exim4-config/stable 4.89-2+deb9u1 all
exim4-daemon-light/stable 4.89-2+deb9u1 amd64
libc-bin/stable 2.24-11+deb9u1 amd64
libc-dev-bin/stable 2.24-11+deb9u1 amd64
libc-l10n/stable 2.24-11+deb9u1 all
libc6/stable 2.24-11+deb9u1 amd64
libc6-dev/stable 2.24-11+deb9u1 amd64
linux-image-4.9.0-3-amd64/stable 4.9.30-2+deb9u1 amd64
linux-libc-dev/stable 4.9.30-2+deb9u1 amd64
locales/stable 2.24-11+deb9u1 all
multiarch-support/stable 2.24-11+deb9u1 amd64
empty@testbed:~ $

No need for bash with this script though (no bashims), a simple /bin/sh shebang will suffice  wink

EDIT:

Naik wrote:

I`m piping the output through ponysay so i`m a little limited in terms of line length

Try the -W flag in ponysay to set the maximum width of the character's speech (or thought) bubble:

empty@Xanadu:~ $ screenfetch | ponysay -W120
 ______________________________________________________________________________________
/                    -`                                                                \
|                   .o+`                 empty@Xanadu                                  |
|                  `ooo/                 OS: Arch Linux                                |
|                 `+oooo:                Kernel: x86_64 Linux 4.11.6-1-hardened        |
|                `+oooooo:               Uptime: 1h 10m                                |
|                -+oooooo+:              Packages: 939                                 |
|              `/:-:++oooo+:             Shell: mksh                                   |
|             `/++++/+++++++:            Resolution: 1920x1080                         |
|            `/++++++++++++++:           DE: GNOME                                     |
|           `/+++ooooooooooooo/`         WM: GNOME Shell                               |
|          ./ooosssso++osssssso+`        WM Theme: CustomFontTheme                     |
|         .oossssso-````/ossssss+`       GTK Theme: Arc [GTK2/3]                       |
|        -osssssso.      :ssssssso.      Icon Theme: Arc                               |
|       :osssssss/        osssso+++.     Font: Sans 11                                 |
|      /ossssssss/        +ssssooo/-     CPU: Intel Core i5-4330M @ 4x 3.5GHz [14.0°C] |
|    `/ossssso+/:-        -:/+osssso+-   GPU: Mesa DRI Intel(R) Haswell Mobile         |
|   `+sso+:-`                 `.-/+oso:  RAM: 1421MiB / 7908MiB                        |
|  `++:.                           `-/+/                                               |
\  .`                                 `/                                               /
 --------------------------------------------------------------------------------------
                    \
                     \
                      \
                       ▄▄▄▄▄  ▄▄▄▄▄
                     ▄▄█████▄▄█████▄▄ ▄▄▄▄▄
                     ███▄▄▄██████████▄█▄████
                     ▄▄▄▄▄██▄█████████████▄▀
                   ▄▄████████▄▄▄▄▄▄▄█▄▀▄█▄▀
                   ▀▄█▄███████▄▄▄██▄▄  ▀▄▄█
                     ██▄████▄▄███▄▄███ █▄▄▀
                   ▄▄███▄▄█████▄█▄█████▀
     ▄▄▄▄▄▄▄▄      ▀▄█▄▄█▄▄█████▄███████
   ▄▄████████▄▄      ▀▀▄█▄▄██▄██████▄▄▄▀
   ███████▄▀▀▄▄▄▄▄▄▄▄▄▄▄█▄████▄█▀▀▀▀▀▀
   ▀▄███▄██  ▄▄▄▄▄▄▄▄▄▄██▄▄█████
     ▀▄█████ ██▄▄██▄▄▄▄▄███████
  ▄▄▄▄█▄█████▀▄▄█▄█▄█▄▄▄▄███▄▀
 ████████▄▄▄▀▄▄███▄█▄▄▄▄█████
  ▀▄▄█▄██   ██████▄▄▀  ██████
  █▄ ▀▄▄██  ████████   ████▄█▄
  ▀▄▄▄▄▄██ █████████   ███████
    ▀▄▄▄▀  █████████   █████▄▄█▄
          ███████▄▄▄█  ███████▄█
          █▄▄▄▄▄▄█      █▄▄▄▄▄█
empty@Xanadu:~ $

Ponysay rules!

Last edited by Head_on_a_Stick (2017-06-24 10:56:32)

Offline

#3 2017-06-24 12:11:09

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: [solved]need help processing text in bash

Thread moved to Basic Help & Support, the Scripts &c section is for guides and tutorials.

Offline

#4 2017-06-24 12:37:37

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: [solved]need help processing text in bash

Shorter version:

empty@testbed:~ $ list=$(awk -F"/" '!/Listing/{print $1}' upgradable)
empty@testbed:~ $ echo "$list"
exim4-base
exim4-config
exim4-daemon-light
libc-bin
libc-dev-bin
libc-l10n
libc6
libc6-dev
linux-image-4.9.0-3-amd64
linux-libc-dev
locales
multiarch-support
empty@testbed:~ $

EDIT: hah! testbed is still vulnerable to the Stack Clash...

Last edited by Head_on_a_Stick (2017-06-24 12:41:40)

Offline

#5 2017-06-24 12:59:23

Naik
Member
From: the edge of insanity
Registered: 2015-10-03
Posts: 328

Re: [solved]need help processing text in bash

Wow! So many nice ideas. Thank you!

I think i`ll try the first one because ...well there is sed already.
Thanks also for the tip with the -w flag.

..will mark this as solved when i have had time to test it.


"Kaum macht [Mensch]* es richtig, funktioniert es sofort!"
BL-Kitchen Codeberg

Offline

#6 2017-06-24 13:07:36

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: [solved]need help processing text in bash

Just to clarify: the "upgradeable" [sic] text file already has line breaks so you don't need to add them at all, just remember to always double quote the $list variable to keep these intact.

See http://mywiki.wooledge.org/Quotes for more on this.

I don't know why you're getting the output in the scrot though, that looks like the content of the "upgradeable" text file hasn't been changed at all.

Last edited by Head_on_a_Stick (2017-06-24 13:09:59)

Offline

#7 2017-06-24 18:06:37

Naik
Member
From: the edge of insanity
Registered: 2015-10-03
Posts: 328

Re: [solved]need help processing text in bash

Jeah I just played around with ponysays mentioned -W option and found the line breaks to be ok. But never the less it is good to know how to eliminate the stuff in the square brackets.
So this is solved better than i have imagined it would...

H_o_a_S wrote:

See http://mywiki.wooledge.org/Quotes for more on this.

bookmarked for reference!

Thanks again!

naik --greetz


"Kaum macht [Mensch]* es richtig, funktioniert es sofort!"
BL-Kitchen Codeberg

Offline

#8 2017-06-24 18:55:44

DeepDayze
Like sands through an hourglass...
From: In Linux Land
Registered: 2017-05-28
Posts: 1,901

Re: [solved]need help processing text in bash

Ponysay is not in Debian but would cowsay work for this?


Real Men Use Linux

Offline

#9 2017-06-24 20:24:34

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Re: [solved]need help processing text in bash

DeepDayze wrote:

Ponysay is not in Debian

A Debian Developer has a private repository for it:

http://www.vcheng.org/ponysay/

Offline

#10 2017-06-24 22:11:33

DeepDayze
Like sands through an hourglass...
From: In Linux Land
Registered: 2017-05-28
Posts: 1,901

Re: [solved]need help processing text in bash

Head_on_a_Stick wrote:
DeepDayze wrote:

Ponysay is not in Debian

A Debian Developer has a private repository for it:

http://www.vcheng.org/ponysay/

Oh ok thanks for that.


Real Men Use Linux

Offline

#11 2017-06-24 22:18:14

Naik
Member
From: the edge of insanity
Registered: 2015-10-03
Posts: 328

Re: [solved]need help processing text in bash

^nice tip, but it also builds very smooth from source.

naik --greetz


"Kaum macht [Mensch]* es richtig, funktioniert es sofort!"
BL-Kitchen Codeberg

Offline

Board footer

Powered by FluxBB