The UNIX and Linux Forums  

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
splitting words from a string suresh_kb211 Shell Programming and Scripting 2 09-14-2009 06:16 AM
Grap data/string from lines between words andrisetia UNIX for Dummies Questions & Answers 2 08-05-2008 07:48 AM
FInd the String between Two words koti_rama Shell Programming and Scripting 6 08-05-2008 04:10 AM
creating a delimiter string satish@123 Shell Programming and Scripting 0 05-21-2008 06:38 AM
To split a string to obtain the words delimited by whitespaces Sudhakar333 Shell Programming and Scripting 4 08-06-2007 03:26 PM

Reply
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 09-15-2009
deepakthaman deepakthaman is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
Creating String from words in a file

Hi i have a file called search.txt

Which contains text like

Car
Bus
Cat
Dog

Now i have to create a string from the file which should look like

Car,Bus,Cat,Dog

( appending , is essential part) String must be stored in some variable so i can pass it as argument to some other command.


my attempt so far

while read line
do
var1=$(awk '{print $NF}')
var2=','
var3=$var1$var2
done

cat var3


but no out put .... plz help
Thnx in advance
  #2 (permalink)  
Old 09-15-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 536
Code:
$
$ cat f1
Car
Bus
Cat
Dog
$
$ cat f1 | tr '\n' ',' | sed 's/,$/\n/'
Car,Bus,Cat,Dog
$
$
tyler_durden
  #3 (permalink)  
Old 09-15-2009
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Hi, try to integrate this in your script:
Code:
awk -F"\n" -v RS="" '{for(i=1;i<=NF;i++) printf "%s%s",(i==1)?"":",",$i}'
  #4 (permalink)  
Old 09-15-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 536
Yet another awk script:

Code:
$
$ awk '{s=s","$0}END{sub(/^,/,"",s); print s}' f1
Car,Bus,Cat,Dog
$
$
Or some Perl:

Code:
$
$ perl -nle 'chomp; push @x,$_; END{print join(",",@x)}' f1
Car,Bus,Cat,Dog
$
tyler_durden
  #5 (permalink)  
Old 09-15-2009
deepakthaman deepakthaman is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
hi tyler_durden
plz can u explain the code i am new to awk

awk '{s=s","$0}END{sub(/^,/,"",s); print s}' f1

??
  #6 (permalink)  
Old 09-15-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 536
Quote:
Originally Posted by deepakthaman View Post
...
i am new to awk
...
If you are new to awk, then I recommend some reading up:

The GNU Awk User's Guide + lots of playing around with that (and other) scripts.

Everything about awk you seek is there in that one web page.

tyler_durden
Reply

Bookmarks

Tags
shell scripting

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 02:19 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