I have a list of names in a file.
i want to assign those names to a variable in such a manner
eg:
$cat file.txt
pete
lisa
john
var=pete-lisa-john
how do i do this in shell scripting?
I have a list of names in a file.
i want to assign those names to a variable in such a manner
eg:
$cat file.txt
pete
lisa
john
var=pete-lisa-john
how do i do this in shell scripting?
Something like this:
$cat test
123
aaa124
125bbb
126
127
123
$a=$(awk 'BEGIN {ORS="-"}{print}' test)
a=${a%-}
$echo $a
123-aaa124-125bbb-126-127-123
I dont want the last hifen to appear
A-B-C-D
not
A-B-C-D-
var=$( sed -n -e ":a" -e "$ s/\n/-/gp;N;b a" file )
or
var=$( tr '\n' '-' <file )
var=${var%-}
I have edited my post, have a look at it.
With bash:
$ cat file.txt
pete
lisa
john
$ var=$(<file.txt) && var=${var//'
'/-} && echo $var
pete-lisa-john
Regards
Dimitre
It works with ksh too. Great work.
Yes, with ksh93,
with my ksh88 - no
$ what /usr/bin/ksh
/usr/bin/ksh:
Version M-11/16/88i
SunOS 5.8 Generic 110662-14 Apr 2004
$ var=$(<file.txt) && var=${var//'
> '/-} && echo $var
ksh[2]: var=${var//"^J"/-}: bad substitution
Regards
Dimitre
You are right, I'm at ksh93
$what /usr/bin/ksh
/usr/bin/ksh:
Version M-12/28/93e-SCO
cut (AT&T Bell Laboratories) 04/01/93
dirname (AT&T Bell Laboratories) 07/17/92
getconf (AT&T Bell Laboratories) 05/09/95
head (AT&T Bell Laboratories) 04/01/92
logname (AT&T Bell Laboratories) 04/01/92
ast (AT&T Bell Laboratories) 07/17/95
hash (AT&T Bell Laboratories) 05/09/95
getconf (AT&T Bell Laboratories) 07/17/95
sfio (AT&T Bell Laboratories) 05/09/95
UnixWare 7.1.1 uw711m5/bl1ai 2004-11-11:built on uw713mp
I am getting an error :
when i run the following
p= ${p%-}
basd substitution
p=A-B-C-D-
try this
len=$(( ${#p}-1 ))
p=$( expr substr "$p" 1 $len )