I have a file with the below lines
1521
1522
1523
1524
1525
1526
...
174 lines all numbers like above. I want the above file to appear as below.
1521,1522,1523,1524.....
All the numbers should be separated by comma.
Pls tell me how to do this ....
Thanks in advance.
{ OIFS="$IFS";IFS='
' set $(<file);IFS=",";echo "$*"; IFS="$OIFS";}
Example:
$ cat file
1521
1522
1523
1524
1525
1526
$ { OIFS="$IFS";IFS='
' set $(<file);IFS=",";echo "$*"; IFS="$OIFS";}
1521,1522,1523,1524,1525,1526
With bash:
$ v="$(<file)";echo "${v//
/,}"
1521,1522,1523,1524,1525,1526
Woops, won't work for long lists; disregard.
I'll try to redeem myself with something that might work:
var=$(<infile.txt); echo $var | sed 's/ /,/g'
This also seems to work:
echo $(<infile.txt) | sed 's/ /,/g'
perl -pi -e 's/^\n/,/' file
if you have a perl on you machine
Neither mine.
May be tr and sed would be more appropriate in those cases.
tr command would also help in this regard
tr -s "\n" "," < file_name
anbu23
9
sed -n -e ":a" -e "$ s/\n/,/gp;N;b a" file