Issue
I am stuck with it not getting how to proceed with it
I have a folder called : demo
In that their are n number of files , but files will have only one column data and that to vertical form
Need to convert that vertical data like below to horizontal comma separated form without creating new file using sed
Data in file 1 :
ABX
aHA
AHAK
AFGJK
AA
Data in file 2 :
1234
hakk
1567
gahsll
using sed
command both file data should be converted to horizontal comma separated format
My command :
sed -i 's/\n//g' /Demo/*.*
Output :
ABX,aHA,AHAK,AFGJK,AA
Similarly for file 2 as well
Note : Demo folder can contain n number of files
Solution
This might work for you (GNU paste):
paste -sd, file > tempFile && mv tempFile file
The -s
pastes serially and the -d
is the separator.
or use sed:
sed -zi 's/\n/,/g;s/,$/\n/' file
The -z
option slurps the whole file into the pattern space.
The -i
option allows editing inplace.
Substitute all newlines by ,
's and restore the last newline if the file ends in a newline.
Alternative:
sed -i 'H;$!d;x;s/.//;s/\n/,/g' file
Copy the whole of the file into the hold space, remove the first newline and then replace all other newlines by ,
's.
Answered By - potong Answer Checked By - Senaida (WPSolving Volunteer)