Solved

Concatenate in Unix (ksh)

Posted on 2008-06-13
7
1,398 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 92
Adapt this command to show who installed 29 108
ODBC Connection Logging, ADO.NET 6 64
mapBully challenge 6 134
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…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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