The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assign o/p of awk to a variable c2b2 Shell Programming and Scripting 8 04-01-2009 07:50 AM
assign value to variable using AWK HAA Shell Programming and Scripting 4 12-06-2006 11:43 AM
assign a value to variable markjason Shell Programming and Scripting 3 10-10-2006 02:05 PM
hot to assign output to a variable walnut Shell Programming and Scripting 1 02-22-2006 09:19 PM
assign to variable AkumaTay UNIX for Dummies Questions & Answers 1 05-18-2002 02:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-16-2007
Shivdatta Shivdatta is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 73
assign a value to a variable

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?
  #2 (permalink)  
Old 01-16-2007
tayyabq8's Avatar
tayyabq8 tayyabq8 is online now Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Something like this:
Code:
$cat test
123
aaa124
125bbb
126
127
123
Code:
$a=$(awk 'BEGIN {ORS="-"}{print}' test)
a=${a%-}
$echo $a
123-aaa124-125bbb-126-127-123

Last edited by tayyabq8; 01-16-2007 at 08:47 AM..
  #3 (permalink)  
Old 01-16-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
var=$( sed -n -e ":a" -e "$ s/\n/-/gp;N;b a" file )
or
Code:
var=$( tr '\n' '-' <file )
var=${var%-}
  #4 (permalink)  
Old 01-16-2007
Shivdatta Shivdatta is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 73
I dont want the last hifen to appear

A-B-C-D

not

A-B-C-D-
  #5 (permalink)  
Old 01-16-2007
tayyabq8's Avatar
tayyabq8 tayyabq8 is online now Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Quote:
Originally Posted by Shivdatta
I dont want the last hifen to appear

A-B-C-D

not

A-B-C-D-
I have edited my post, have a look at it.
  #6 (permalink)  
Old 01-16-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by Shivdatta
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?
With bash:

$ cat file.txt
pete
lisa
john

$ var=$(<file.txt) && var=${var//'
'/-} && echo $var
pete-lisa-john



Regards
Dimitre
  #7 (permalink)  
Old 01-16-2007
tayyabq8's Avatar
tayyabq8 tayyabq8 is online now Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Quote:
Originally Posted by radoulov
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0