Solved

Concatenate in Unix (ksh)

Posted on 2008-06-13
7
1,401 Views
Last Modified: 2013-12-26
Hi,

I have a file which contains data as follow in each line.

123
456
789

What I need to do in here is to take the data from the file and put it into  another file (fileb.txt) as follow:

'123', '456', '789'

So far I have the code as follow :

cat fileA.txt | while read line;
do
    fileb=`cat FileA.txt`
    $fileb=$fileb","$fileb
done

Please advise.

Thanks....
0
Comment
Question by:msalam65
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21781085
while read line;
do
    fileb="$fileb,'$line'"
done < fileA.txt
echo $fileb > fileb.txt
0
 
LVL 84

Expert Comment

by:ozo
ID: 21781137
to remove the first , echo ${fileb#,}
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 21781252
try

#!/bin/ksh
cat 55.txt | while read line
do
    fileb=$fileb",'"$line"'"
done
echo $fileb | sed 's/^\,//' > fileB.txt
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:omarfarid
ID: 21781290
please replace 55.txt with your fileA.txt
0
 
LVL 1

Expert Comment

by:Joomlajohn
ID: 21781353
I think awk is the tool your looking for:
This handles what you want except for a final comma at the end.

awk '{printf "\"%s\",",$0; next}{print}' fileA.txt > fileB.txt

awk is a powerful tool more information at this link:
http://www.vectorsite.net/tsawk_1.html

Also, regular expressions will be another great tool in your belt if you choose to learn how to use them.
http://gnosis.cx/publish/programming/regular_expressions.html
http://www.regular-expressions.info/tutorial.html
0
 
LVL 84

Expert Comment

by:ozo
ID: 21782569
perl -le "print join',',map{chomp;qq('"'$_'"')}<>" fileA.txt > fileB.txt
or if you you can use " instead of ' and you don't need to remove the trailing ,
perl -lpe '$\=",";s/(.*)/"$1"/'  fileA.txt > fileB.txt
0
 
LVL 84

Expert Comment

by:ozo
ID: 21782679
what was wrong with
while read line;
do
    fileb="$fileb,'$line'"
done < fileA.txt
echo ${fileb#,}> fileb.txt
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 163
Add content to output file 4 71
tripleUp challenge 7 93
zeroMAx challenge 20 127
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question