Convert .txt to .csv in shell
awk
may be a bit of an overkill here. IMHO, using tr
for straight-forward substitutions like this is much simpler:
$ cat ifile.txt | tr -s '[:blank:]' ',' > ofile.txt
How to convert text file to csv file using bash script
cat file|xargs -n5
works for the given example.
If this doesn't help, please tell the exact requirements.
update, add the fixed title
cat <(echo "a b c d e") file|xargs -n5
if you want better format in output:
cat <(echo "a b c d e") file|xargs -n5 |column -t
Convert txt file to CSV? - On a Linux Server
It's not a CSV file but a TSV file, you can download and convert it like so:
curl -s https://www.cdkeys.com/feeds/cdkeys_affiliate_feed_eur.txt | tr "\t" "," > output.csv
Or if you want the format that excel likes by default, iirc you need to use a semicolon instead of a comma (so substitute the ","
with a ";"
)
Convert .txt file to .csv with header in bash
This should do it, if your fields don't contain any funny business:
(echo "Date;Visit;Login;Euro;Rate" ; cat file.txt) | sed 's/;/<tab>/g' > file.csv
You'll just have to type a tab literally in bash (^V TAB). If your version of sed supports it, you can write\t
instead of a literal tab.
Converting text data file to csv format via shell/bash
This line should help:
awk 'BEGIN{FS=":|\n";RS="Gender";OFS=",";print "Gender,Age,History"}$0{print $2,$4,$6}' file
With your example as input, it gives:
Gender,Age,History
M, 46, 01305
F, 46, 01306
M, 19, 01307
M, 19, 01308
Format and then convert txt to csv using shell script and awk
You may use this awk
:
awk -v OFS=, '{k=$1 OFS $2 OFS $3}
!($4 in hdr){hn[++h]=$4; hdr[$4]}
k in row{row[k]=row[k] OFS $5; next}
{rn[++n]=k; row[k]=$5}
END {
printf "%s", rn[1]
for(i=1; i<=h; i++)
printf "%s", OFS hn[i]
print ""
for (i=2; i<=n; i++)
print rn[i], row[rn[i]]
}' file
x,y,z,t,01hr01Jan2018,02hr01Jan2018,03hr01Jan2018
1,1,5,3,3.1,3.2
1,3.4,3,4.1,6.1,1.1
1,4.2,6,6.33,8.33,5.33
3.4,1,2,3.5,5.65,3.66
3.4,3.4,4,6.32,9.32,12.32
3.4,4.2,8.1,7.43,7.93,5.43
4.2,1,3.4,6.12,7.15,9.12
4.2,3.4,5.5,2.2,3.42,3.21
4.2,4.2,6.2,1.3,3.4,1
How to Convert text file to CSV in python
Add shell=True
subprocess.call("sed 's/[[:space:]]\+/,/g' test.txt > test.csv", shell=True)
Related Topics
How to Fix Java.Lang.Module.Findexception: Module Java.Se.Ee Not Found
Save Modifications in Place With Awk
How to Merge Two "Ar" Static Libraries into One
What Is the Runtime Performance Cost of a Docker Container
How to Escape Single Quotes in Bash/Grep
Git Asks For Username Every Time I Push
How to Programmatically Create a New Cron Job
How to Single Step Arm Assembly in Gdb on Qemu
How to Run a Shell Script on a Unix Console or MAC Terminal
What Killed My Process and Why
Why Should Eval Be Avoided in Bash, and What Should I Use Instead
How Does "Cat ≪≪ Eof" Work in Bash
What Does "&" At the End of a Linux Command Mean
How to Replace Spaces in File Names Using a Bash Script
Hello, World in Assembly Language With Linux System Calls
Shell Command to Tar Directory Excluding Certain Files/Folders