Solved

Concatenate in Unix (ksh)

Posted on 2008-06-13
7
1,395 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
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

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

Expert Comment

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

Accepted Solution

by:
omarfarid earned 500 total points
Comment Utility
try

#!/bin/ksh
cat 55.txt | while read line
do
    fileb=$fileb",'"$line"'"
done
echo $fileb | sed 's/^\,//' > fileB.txt
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
please replace 55.txt with your fileA.txt
0
 
LVL 1

Expert Comment

by:Joomlajohn
Comment Utility
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
Comment Utility
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
Comment Utility
what was wrong with
while read line;
do
    fileb="$fileb,'$line'"
done < fileA.txt
echo ${fileb#,}> fileb.txt
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now