Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 01 Oct 2014, 22:44
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to use sed example?
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
sunburnt


Joined: 08 Jun 2005
Posts: 5032
Location: Arizona, U.S.A.

PostPosted: Thu 07 Nov 2013, 17:16    Post_subject:  How to use sed example?  

From the "Sed 1 liners" examples ( very good ).

# join pairs of lines side-by-side (like "paste")
sed '$!N;s/\n/ /'

I can`t see how to get 2 variables to feed into it.

I`d hope that it would do 2 variables with lists.
But it states lines so it may be pretty worthless.

paste only uses files, and the same with comm.
So nice if both of them would take variables too.
.
Back to top
View user's profile Send_private_message 
01micko


Joined: 11 Oct 2008
Posts: 7805
Location: qld

PostPosted: Thu 07 Nov 2013, 17:18    Post_subject:  

Did you try double quotes around the sed arg? Singles use the literal string.
_________________
Woof Mailing List | keep the faith Cool |
Back to top
View user's profile Send_private_message Visit_website 
sunburnt


Joined: 08 Jun 2005
Posts: 5032
Location: Arizona, U.S.A.

PostPosted: Thu 07 Nov 2013, 17:57    Post_subject:  

Right. But I just don`t see how it works.

# Example please: How to feed 2 variables to it and have them pasted?

Again... If they`re single lines I can do that with: pasted="$var1 $var2"
But this doesn`t work with lists of course.
.
Back to top
View user's profile Send_private_message 
01micko


Joined: 11 Oct 2008
Posts: 7805
Location: qld

PostPosted: Thu 07 Nov 2013, 18:22    Post_subject:  

Maybe I'm not getting what your trying to do.
Code:
bash-4.2$ echo -e "hello\nworld" >somefile.txt
bash-4.2$ cat somefile.txt
hello
world
bash-4.2$ cat somefile.txt|sed '$!N;s/\n/ /'
hello world


Are you trying to make an array of strings out of a list of strings?

You'd probably have to do a while read line loop?

_________________
Woof Mailing List | keep the faith Cool |
Back to top
View user's profile Send_private_message Visit_website 
SFR


Joined: 26 Oct 2011
Posts: 1072

PostPosted: Thu 07 Nov 2013, 18:43    Post_subject:  

I don't know sed too well, but if you don't mind using bash-specific way, it's still possible with 'paste':
Code:
# var1="abc\ndef\nghi"; var2="123\n456\n789"; paste -d ' ' <(echo -e $var1) <(echo -e $var2)
abc 123
def 456
ghi 789
#

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
sunburnt


Joined: 08 Jun 2005
Posts: 5032
Location: Arizona, U.S.A.

PostPosted: Thu 07 Nov 2013, 19:44    Post_subject:  

SFR; Thank you... You have shown me how to make a file out of a variable.!
I have looked for this so long it`s sad. I`m sure it`ll work for comm also.

File posing as a variable: $(</path/file)
Variable posing as a file: <(echo -e "$variable")

# The working command:
Code:
Pasted=`paste <(echo -e "$List1") <(echo -e "$List2")



Thanks 01micko; SFR did the trick. But thanks for the reply.
Code to put a list into an array:
Code:
List=`echo -e '1\n2\n3'`
Items=`echo $List |sed 's#^#"#;s#$#"#;s# #" "#g'`
declare -a Array=($Items)
echo ${Array[0]}

I`m sure technosaurus would do the sed better with awk. Cool
.
Back to top
View user's profile Send_private_message 
sunburnt


Joined: 08 Jun 2005
Posts: 5032
Location: Arizona, U.S.A.

PostPosted: Thu 07 Nov 2013, 20:16    Post_subject:  

### Problem: It doesn`t seem to work in a script function.
It does need Bash to work: #!/bin/bash

The script command:
Site=`paste <(echo -e "$NAME") <(echo -e "$BPS")`
And this gives an empty file:
paste <(echo -e "$NAME") <(echo -e "$BPS") > /tmp/AAA

The variables have:
Code:
dhakaCom Limited
dhakaCom Limited
IS Pros Limited

1 Gbps
100 Mbps
100 Mbps

It works in rxvt, but in the script the error:
Code:
paste: <(echo -e dhakaCom Limited
dhakaCom Limited
IS Pros Limited): No such file or directory
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1072

PostPosted: Fri 08 Nov 2013, 10:32    Post_subject:  

Yeah, I don't really know why #!/bin/bash is necessary, even if /bin/sh is a symlink to /bin/bash, as it is in my case...
However, it still works for me (/bin/bash case):
Code:
#!/bin/bash

pasteit () {
 paste -d ' ' <(echo -e "$1") <(echo -e "$2")
}

NAME="dhakaCom Limited\ndhakaCom Limited\nIS Pros Limited"
PBS="1 Gbps\n100 Mbps\n100 Mbps"

PASTED=`pasteit "$NAME" "$PBS"`

echo "$PASTED"

Anyway, here's more "pure" and bash-free attempt, using 'while' loop as Mick suggested:
Code:
#!/bin/busybox sh

# Note: in Slacko '/bin/sh' is just a symlink to '/bin/bash',
# so I had to use '/bin/busybox sh' in order to test it properly

pasteit () {
  TMP1=$1
  TMP2=$2

  # Make sure that there's a newline at the end of both strings.
  [ "${TMP1:$((${#TMP1}-2)):2}" != "\n" ] && TMP1="${TMP1}\n"
  [ "${TMP2:$((${#TMP2}-2)):2}" != "\n" ] && TMP2="${TMP2}\n"
 
  while [ "$TMP1" != "" ] || [ "$TMP2" != "" ]; do
    echo "${TMP1%%\\\n*} ${TMP2%%\\\n*}"   # Hmm, sh needs \\\, bash only \\
    # Remove the first element from both strings ('\n' as a delim)
    TMP1="${TMP1#*\\n}"; TMP2="${TMP2#*\\n}"
  done
}

# -----------------------------------------------------------------------------

NAME="dhakaCom Limited\ndhakaCom Limited\nIS Pros Limited"
PBS="1 Gbps\n100 Mbps\n100 Mbps"

PASTED=`pasteit "$NAME" "$PBS"`

echo "$PASTED"

Quote:
# ./script1
dhakaCom Limited 1 Gbps
dhakaCom Limited 100 Mbps
IS Pros Limited 100 Mbps
#
# ./script2
dhakaCom Limited 1 Gbps
dhakaCom Limited 100 Mbps
IS Pros Limited 100 Mbps
#

Well, I'm sure there must be also some elegant sed/awk one-liner, but I'm not an expert in those areas. Wink

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
sunburnt


Joined: 08 Jun 2005
Posts: 5032
Location: Arizona, U.S.A.

PostPosted: Fri 08 Nov 2013, 13:10    Post_subject:  

Thanks again SFR; I tried again and it seems to be the many spaces in the data.
As I have had to use files just to get GtkDialog to work, using files for paste is no big deal.
Code:
paste: <(echo -e RUE Beltelecom, MGTS
Rue Beltelecom, Datacenter): No such file or directory

Code:
sites() {
   info=`sed -n "/$1/,/colspan=/p" $confP/mirrors.gtkD_html`
   [ "$info" = 'Choose a Nation' ]&& exit
   echo "$info" |sed '/>http</!d;s#">.*$##;s#^.*="##' > $confP/mirrors.gtkD_url

   NAME=`echo "$info" |sed '/https:/!d;s#</.*$##;s#^.*>##'`

   echo "$NAME" |sed 's#^ ##;s#amp;##' > $confP/mirrors.gtkD_name

   BPS=`echo "$info" |sed '/<td>[0-9]/!d;s#</.*$##;s#^.*>##'`

   echo "$BPS" |sed 's#^#|#' > $confP/mirrors.gtkD_bps

   paste <(echo -e "$NAME") <(echo -e "$BPS") > $confP/mirrors.gtkD_info

#   paste $confP/mirrors.gtkD_name $confP/mirrors.gtkD_bps > $confP/mirrors.gtkD_info
}
Back to top
View user's profile Send_private_message 
amigo

Joined: 02 Apr 2007
Posts: 2252

PostPosted: Fri 08 Nov 2013, 13:53    Post_subject:  

Quote:
why #!/bin/bash is necessary, even if /bin/sh is a symlink to /bin/bash


When bash is run as 'sh', it runs with (nearly) only POSIX behavior -which means that some things don't work the way they do when the program is executed with the name 'bash'. There's a caveat with the 'nearly': bash-as-sh still supports some bash extensions which one would not find in traditional Bourne shells and similar ash/dash/bsh/jhs, etc.

The lesson is to use /bin/bash as the shebang any time you use any bashisms -or learn to write portable sh-compatible code. I usually do the former, but it's your call. If you want to check for (nearly) POSIX-compliance, use /bin/dash as the shebang. If it will run under dash then it will most likely work(with /bin/sh shebang) under any common shell -excluding light-weight alternatives like 'sash', any of the busbyox shell options. I'm pretty sure that even the fullest busybox shell option (ash??) is not feature equal to traditional ash and certainly not equal to dash.

If you want portability starting from the Big Bang and continuing up to date, then you can use the heirloom-sh (bsh) as the testbed. Anything that runs under bsh should run anywhere, even on your toaster -a Post-WWII toaster, I mean.
Back to top
View user's profile Send_private_message 
sunburnt


Joined: 08 Jun 2005
Posts: 5032
Location: Arizona, U.S.A.

PostPosted: Fri 08 Nov 2013, 14:16    Post_subject:  

Yeah, I don`t see the point in Puppy`s BusyBox, so much of it`s disabled. But boot uses it`s init.
BusyBox can be "custom compiled". It can be made with full capabilities and added stuff.

amigo; What do you think of the new Community Edition effort that`s under way.?
They seem to have settled on Debian stable ( your choice base ). I hope it flies and takes over.
With enough folks working on it, maybe most of the "never been fixed" items will actually get done.
.
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1072

PostPosted: Fri 08 Nov 2013, 16:40    Post_subject:  

amigo wrote:
When bash is run as 'sh', it runs with (nearly) only POSIX behavior[...]

Aaah, so it's an equivalent of:
https://en.wikipedia.org/wiki/Bash_(Unix_shell) wrote:
Invoking Bash with the --posix option or stating set -o posix in a script causes Bash to conform very closely to the POSIX 1003.2 standard.

Quote:
heirloom-sh

Will try - might be kinda fun.

Thanks &
Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0964s ][ Queries: 12 (0.0169s) ][ GZIP on ]