Hello,
I have a text file as:-
ABC
BCD
CDF
DEF
EFG
I need to convert as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG' using a unix command
anybody can help me out on this.
Regards,
Jas
Hello,
I have a text file as:-
ABC
BCD
CDF
DEF
EFG
I need to convert as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG' using a unix command
anybody can help me out on this.
Regards,
Jas
It depends what you really want to do.
Do you want to convert this to an output file or string as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG'
If so, you could:-
while read line
do
my_var="$my_var, '$line'"
done < text_file
my_var="${my_var#, }" # Trim off leading comma and space
echo "$my_var"
Does this meet your needs? If not, can you explain a bit more about what you do need.
Regards,
Robin
it would be good if its in string
Hello jassi,
Following may help you in same, please use code tags as per forum rules too.
awk -vs1="'" '{A=A?A OFS s1 $0 s1:s1 $0 s1} END{print A}' OFS=, Input_file
Output will be as follows.
'ABC','BCD','CDF','DEF','EFG'
EDIT: Adding one more solution a little different from above.
awk -vs1="'" '{gsub(/^|$/,s1,$0);A=A?A OFS $0:$0} END{print A}' OFS=, Input_file
Thanks,
R. Singh
Does the above (reading the input file text_file) not work for you?
The value is stored in $my_var
at the end of these few lines and you can use it as you please.
What is the eventual purpose? Perhaps there is a smarter way to achieve your overall aim that we can help with.
Robin
Try also this sed solution (need EREs!):
sed -rn 's/^|$/\o047/g;1h;1!H;${x;s/\n/,/gp}' file
Each on new line, based in one of the answers above:
awk -vs1="'" '{gsub(/^|$/,s1,$0);A=A?A OFS $0:$0} END{print A}' OFS="\n" file
Unix sed:
sed -e ':L1' -e N -e '$!bL1' -e "s/.*/'&'/;s/\n/','/g" file
Read the whole file into the memory buffer, then
insert a '
at the beginning and end of the buffer,
replace all intermediate \n
characters by ','
.
Variant2, use the hold buffer:
sed -e '1h;1!H;$!d;g' -e "s/.*/'&'/;s/\n/','/g" file
Variant3, converted RudiC solution to Unix sed, also using the hold buffer:
sed 's/^/'\''/;s/$/'\''/;1h;1!H;$!d;x;s/\n/,/g' file
sed "s/.*/'&'/" file | paste -sd, -
--
to put it in a variable, use command substitution:
var=$(sed "s/.*/'&'/" file | paste -sd, -)
and the Oscar goes to
In pure bash you could try:
# Change IFS to Support white space on lines
IFS=$'\n'
printf -v var "'%s', " $(< file)
IFS=$' \t\n'
# remove trailing ", "
var="${var%, }"
echo "$var"
For input:
ABC DEF
GHI
JKL
MNO
Produces 'ABC DEF', 'GHI', 'JKL', 'MNO'
for output 'ABC', 'DEF', 'GHI', 'JKL', 'MNO'
(which the thread subject seems to imply)
simply use:
printf -v var "'%s', " $(< file)
echo "${var%, }"
Another shell solution:
var=$( while read L; do printf "%s'%s'" "$sep" "$L"; sep=", "; done < file )
Translating to awk saves the explicit loop:
var=$( awk '{printf "%s'\''%s'\''",sep,$0; sep=", "}' file )
Another pure bash/ksh93/zsh solution (does not require changing IFS):
rec=$(<file); var="'${rec//$'\n'/','}'"
--
to print:
rec=$(<fname); printf "%s\n" "'${rec//$'\n'/','}'"
--