How to use sed example?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

How to use sed example?

#1 Post by sunburnt »

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.
.

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#2 Post by 01micko »

Did you try double quotes around the sed arg? Singles use the literal string.
Puppy Linux Blog - contact me for access

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

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.
.

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#4 Post by 01micko »

Maybe I'm not getting what your trying to do.

Code: Select all

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?
Puppy Linux Blog - contact me for access

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#5 Post by SFR »

I don't know sed too well, but if you don't mind using bash-specific way, it's still possible with 'paste':

Code: Select all

# var1="abc\ndef\nghi"; var2="123\n456\n789"; paste -d ' ' <(echo -e $var1) <(echo -e $var2)
abc 123
def 456
ghi 789
# 
Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#6 Post by sunburnt »

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: Select all

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: Select all

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. 8)
.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#7 Post by sunburnt »

### 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: Select all

dhakaCom Limited
dhakaCom Limited
IS Pros Limited

1 Gbps
100 Mbps
100 Mbps
It works in rxvt, but in the script the error:

Code: Select all

paste: <(echo -e dhakaCom Limited
dhakaCom Limited
IS Pros Limited): No such file or directory

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#8 Post by SFR »

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: Select all

#!/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: Select all

#!/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"
# ./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!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#9 Post by sunburnt »

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: Select all

paste: <(echo -e RUE Beltelecom, MGTS
Rue Beltelecom, Datacenter): No such file or directory

Code: Select all

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
}

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#10 Post by amigo »

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.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#11 Post by sunburnt »

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.
.

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#12 Post by SFR »

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.
heirloom-sh
Will try - might be kinda fun.

Thanks &
Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

Post Reply