Loop for n lines - create csv - bash

I've a text file that look likes this :
data1-1
data1-2
data1-3
data2-1
data2-2
data2-3
data3-1
data3-2
data3-3
And I want to transform it to a csv that look like :
data1-1,data1-2,data1-3
data2-1,data2-2,data2-3
data3-1,data3-2,data3-3
What is the best way to solve this problem? I can create my csv with with a echo command
echo "object1,object2,object3" > test.csv
But after that, I'm not sure about what is the best loop method. Please advise. Thanks.

paste -d "," - - - <file >test.csv
Output to test.csv:
data1-1,data1-2,data1-3
data2-1,data2-2,data2-3
data3-1,data3-2,data3-3

Related

bash, get line form file with condition for particular column

I've got file with two columns, for example line of this file looks like this: username,data
username,20150706
I'm trying to get lines that dates in the second column are older than eg. 20151231.
EDIT, SOLUTION: I want to do it in a loop and read the dates (the second column of the file) then compare it with another date, eg. 20151231. Then if date is let say older than 20151231 write whole line (username,date) to file. And here's how I do it:
date1=20151231
while read -r line;
do
date2=$(echo $line | cut -d "," -f2);
if (( date2 < date1 ));
then
echo "$line" >> filename2;
fi
done < filename
So one little problem solved :)
Thanks in advance for any suggestions.
you can solve this with awk:
awk -F',' -v threshold=20151231 '$2<=threshold' file
hope this helps you!

Formatting need to change for text file in bash scripting

I have below output from a text file. This is long file i just copy here some rows only.
HP83904B74E6
13569.06
7705.509999999999
HP4DC2EECAA8
4175.1
2604.13
And i want to print it like below.
HP83904B74E6 13569.06 7705.509999999999
HP4DC2EECAA8 4175.1 2604.13
I have tried by reading the file line by live using while loop and try to store the value of variable e.g. variablename$i so that i can print it like variablename0 and after every 3 line i have used If statement to print the value of variablename0 variablename1 variablename2, but did not work for me.
Use pr:
$ pr -a3t tmp.txt
HP83904B74E6 13569.06 7705.509999999999
HP4DC2EECAA8 4175.1 2604.13
i have tried by reading the file line by live using while loop and try to store the value of variable e.g. variablename$i so that i can print it like variablename0 and after every 3 line i have used If statement to print the value of variablename0 variablename1 variablename2, but did not work for me. I am just learning bash.
while read -r a; do
read -r b;
read -r c;
echo "$a $b $c";
done < file
you get,
HP83904B74E6 13569.06 7705.509999999999
HP4DC2EECAA8 4175.1 2604.13

how to write bash script to read from a text file and filter based on nth read line

i have this text file
Zipcode:123
ExpectedAge:24
ActualAge:26
Zipcode:12333
ExpectedAge:21
ActualAge:24
...
...
I am trying to write a script to compare Expected and Actual age for a zipcode and print that.
output rule
print the zipcodes for which expectedage > actualAge
Is bash/sed/awk is a good idea to achieve this.
you can try to use modulus operator,
awk 'BEGIN{FS=":"}
{d[$1]=$2}
!(NR%3) && d["ExpectedAge"]>d["ActualAge"]{
print d["Zipcode"];
}' file

Convert csv to html table using

How can I convert a CSV file into html table?
I got a csv file with comma "," and I want this file to convert to Html table.
OK, you really want it only in bash? Mission accomplished.
cat > input.csv
a,b,c
d,e,f
g,h,i
echo "<table>" ; while read INPUT ; do echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" ; done < input.csv ; echo "</table>"
<table>
<tr><td>a</td><td>b</td><td>c</td></tr>
<tr><td>d</td><td>e</td><td>f</td></tr>
<tr><td>g</td><td>h</td><td>i</td></tr>
</table>
My first try used "cat" but I figured that was cheating, so I rewrote it using "while read"
XmlGrid.net has a nice tool to convert a CSV file into HTML table. Here is the link:
http://xmlgrid.net/csvToHtml.html

how to read rows?

I'm trying to read first row from the file
> source ./rank file
using this script
set line = ($<) <- inside rank
but when I enter
echo $line I receive nothing, how can I change it? thanks in advance
Since csh is brain-dead, you'll have to do something like this:
set line = `head -n 1 filename`
It's built-in in Bash as:
read -r line < filename
set line = `cat file | sed 1q`

Resources