Creating String from words in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating String from words in a file
# 1  
Old 09-15-2009
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  
Old 09-15-2009
Code:
$
$ cat f1
Car
Bus
Cat
Dog
$
$ cat f1 | tr '\n' ',' | sed 's/,$/\n/'
Car,Bus,Cat,Dog
$
$

tyler_durden
# 3  
Old 09-15-2009
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  
Old 09-15-2009
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  
Old 09-15-2009
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  
Old 09-15-2009
Quote:
Originally Posted by deepakthaman
...
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple words in a file with help of fixed string switch

I have multiple strings in a file which have special character $, when i search strings by ignoring $ with \ using single quotes it returns empty results. My search strings are set char_1($lock) and set new_char_clear_3($unlock) I tried searching with but it returns empty results.However... (3 Replies)
Discussion started by: g_eashwar
3 Replies

2. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

3. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

4. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

5. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

6. Shell Programming and Scripting

Creating Frequency of words from a file by accessing a corpus

Hello, I have a large file of syllables /strings in Urdu. Each word is on a separate line. Example in English: be at for if being attract I need to identify the frequency of each of these strings from a large corpus (which I cannot attach unfortunately because of size limitations) and... (7 Replies)
Discussion started by: gimley
7 Replies

7. Shell Programming and Scripting

To extract a string between two words in XML file

i need to extract the string between two tags, input file is <PersonInfoShipTo AddressID="446311709" AddressLine1="" AddressLine2="" AddressLine3="" AddressLine4="" AddressLine5="" AddressLine6="" AlternateEmailID="" Beeper="" City="" Company="" Country="" DayFaxNo="" DayPhone="" Department=""... (5 Replies)
Discussion started by: Padmanabhan
5 Replies

8. Shell Programming and Scripting

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

9. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

10. Shell Programming and Scripting

splitting words from a string

Hi, I have a string like this in a file, I want to retrive the words separated by comma's in 3 variables. like How do i get that.plz advice (2 Replies)
Discussion started by: suresh_kb211
2 Replies
Login or Register to Ask a Question