6

I input data into a csv by 'echo'-ing some values into another csv.

I tried echo "" and in the next line echo "values" but i get intermittent empty lines..

I need empty line only at the start....

  • 8
    Please show small example code, input data and output data – grebneke Feb 11 '14 at 16:16
  • give an example of the two csv files, it is unclear how you create one csv by echoing into another csv – Anthon Feb 14 '14 at 5:45
6

There are many ways of doing this. Since you have not explained your issue with any detail, I am assuming that you are doing something like this:

values=$(grep foo csv1.csv)
echo "$values" > csv2.csv

At any rate, you seem to be extracting some lines of one csv file and inserting them into another. To add an empty line to the beginning of the new csv file, you do any of these:

echo  > csv2.csv; grep foo csv1.csv >> csv2.csv

Or, if you have already created csv2.csv:

awk 'NR==1{print ""} {print}' csv2.csv > tmpfile && mv tmpfile csv2.csv

or

perl -i -lpe 'print "" if $.==1' csv2.csv 

or

sed -i '1 x;p;x;' csv2.csv 
5

Use sed as follows to insert a blank line in the beginning of a file x.txt:

 sed '1 i \
 ' x.txt
1

As I understand the question, there are two problems. First, you want an empty line at the start and second, you don't want intermediate blank lines.

First question

For this, you can just do echo "" >> filename at the beginning of the script.

Second question

Info page of echo says

`echo' writes each given STRING to standard output, with a space
between each and a newline after the last one.

So, the newline that you get is due to the previous echo that you have done. For example echo "bulbasaur" >> network.txt inserts "bulbasaur" and also a newline character (\n) in the file. So, when you do the second echo like echo "charmander" >> network.txt, there will be an empty line between bulbasaur and charmander.

To remove the newline-inserting-behaviour of echo, use echo -n (See the info or man page of echo). You can do echo -n "bulbasaur" >> network.txt followed by echo -n "charmander" >> network.txt to remove the inserted newline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.