Solved

Concatenate in Unix (ksh)

Posted on 2008-06-13
7
1,407 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
Independent Software Vendors: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

724 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