msalam65
asked on
Concatenate in Unix (ksh)
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....
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....
to remove the first , echo ${fileb#,}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
please replace 55.txt with your fileA.txt
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
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
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
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
what was wrong with
while read line;
do
fileb="$fileb,'$line'"
done < fileA.txt
echo ${fileb#,}> fileb.txt
while read line;
do
fileb="$fileb,'$line'"
done < fileA.txt
echo ${fileb#,}> fileb.txt
do
fileb="$fileb,'$line'"
done < fileA.txt
echo $fileb > fileb.txt