?
Solved

Concatenate in Unix (ksh)

Posted on 2008-06-13
7
Medium Priority
?
1,408 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 2000 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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