How to use sed example?
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.
.
# 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.
.
Did you try double quotes around the sed arg? Singles use the literal string.
Puppy Linux Blog - contact me for access
Maybe I'm not getting what your trying to do.
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?
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
You'd probably have to do a while read line loop?
Puppy Linux Blog - contact me for access
I don't know sed too well, but if you don't mind using bash-specific way, it's still possible with 'paste':
Greetings!
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
#
[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]
[b][color=green]Omnia mea mecum porto.[/color][/b]
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:
Thanks 01micko; SFR did the trick. But thanks for the reply.
Code to put a list into an array:
I`m sure technosaurus would do the sed better with awk.
.
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]}
.
### 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:
It works in rxvt, but in the script the error:
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
Code: Select all
paste: <(echo -e dhakaCom Limited
dhakaCom Limited
IS Pros Limited): No such file or directory
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):
Anyway, here's more "pure" and bash-free attempt, using 'while' loop as Mick suggested:
Greetings!
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"
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"
Well, I'm sure there must be also some elegant sed/awk one-liner, but I'm not an expert in those areas.# ./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
#
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]
[b][color=green]Omnia mea mecum porto.[/color][/b]
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.
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
}
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.why #!/bin/bash is necessary, even if /bin/sh is a symlink to /bin/bash
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.
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.
.
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.
.
Aaah, so it's an equivalent of:amigo wrote:When bash is run as 'sh', it runs with (nearly) only POSIX behavior[...]
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.
Will try - might be kinda fun.heirloom-sh
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]
[b][color=green]Omnia mea mecum porto.[/color][/b]