LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-4175504038/)

AndrewNZ 05-06-2014 12:19 AM

Bash script
 
What is wrong with this?

for i in `seq 1 4`;do /home/andrewc/Downloads/test/ cat $i.csv >> bigfile.csv done

When I run it I just get ">" on the next line

I am trying to merge 4 files into 1

Thank you

Frozwire 05-06-2014 12:31 AM

Hi,

You might try doing it this way:

/home/andrewc/Downloads/test/ cat file1.csv file2.csv file3.csv file4.csv > mergedfilename.csv

Good Luck

eklavya 05-06-2014 12:34 AM

Quote:

#!/bin/bash
for i in `seq 1 4`
do
cd /home/andrewc/Downloads/test
cat $i.csv >> bigfile.csv
done
If you want to write the loop in single line,
Quote:

for i in `seq 1 4`; do `cd /home/andrewc/Downloads/test` `cat $i.csv >> bigfile.csv`; done
I hope there are 4 files 1.csv, 2.csv, 3.csv & 4.csv in the directory /home/andrewc/Downloads/test

Frozwire 05-06-2014 12:47 AM

Quote:

Originally Posted by eklavya (Post 5165438)
If you want to write the loop in single line,

for i in `seq 1 4`; do `cd /home/andrewc/Downloads/test` `cat $i.csv >> bigfile.csv`; done

I hope there are 4 files 1.csv, 2.csv, 3.csv & 4.csv in the directory /home/andrewc/Downloads/test

I agree, this looks much better

AnanthaP 05-06-2014 12:53 AM

Based on the example, there is a space between the last forward slash and the cat command. ie after ../Downloads/test/
Quote:

for i in `seq 1 4`;do /home/andrewc/Downloads/test/ cat $i.csv >> bigfile.csv done
So as far as the shell is concerned, it is still waiting for you to complete the do.

Anyway it should be
Quote:

for i in `seq 1 4`;do cat /home/andrewc/Downloads/test/ $i.csv >> bigfile.csv done
or
Quote:

for i in `seq 1 4`;do cd /home/andrewc/Downloads/test; cat $i.csv >> bigfile.csv done
The > symbol you see is the prompt continuation.

Have you considered that if you run it again, the result will have 8 files?

So it should be
Quote:

cd /home/andrewc/Downloads/test; rm bigfile.csv; for i in `seq 1 4`;do cat $i.csv >> bigfile.csv done
OK

AndrewNZ 05-06-2014 01:10 AM

Ok , thanks for the info , out of all the ones given
for i in `seq 1 4`; do `cd /home/andrewc/Downloads/test` `cat $i.csv >> bigfile.csv`; done seems to be the best one , and the one that actually works
so I need to change to the directory even if I am in it?

chrism01 05-06-2014 01:37 AM

No need to change dir if you're in it already.

Do fold your code over several lines for readability and use indentation
Code:

#!/bin/bash
for i in `seq 1 4`
do
    cat $i.csv >> bigfile.csv
done

and use code tags for posting code or data https://www.linuxquestions.org/quest...do=bbcode#code

In general when using vars in the middle of a string, use ${var} notation to ensure the parser does not get confused eg
Code:

cat ${i}.csv >> bigfile.csv

grail 05-06-2014 02:10 AM

You also do not require the backticks around every command. These (or the $() construct) are only required where you wish to return information to something else, so in your first
portion you wish to return the result from seq to for, but none of the other commands are returning to anything else.


All times are GMT -5. The time now is 02:03 PM.