LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2014, 01:19 AM   #1
AndrewNZ
LQ Newbie
 
Registered: Nov 2013
Posts: 16

Rep: Reputation: Disabled
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
 
Old 05-06-2014, 01:31 AM   #2
Frozwire
LQ Newbie
 
Registered: May 2014
Location: CyberPlanet
Distribution: Slackware | Fedora
Posts: 3

Rep: Reputation: Disabled
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
 
Old 05-06-2014, 01:34 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
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

Last edited by eklavya; 05-06-2014 at 01:40 AM.
 
Old 05-06-2014, 01:47 AM   #4
Frozwire
LQ Newbie
 
Registered: May 2014
Location: CyberPlanet
Distribution: Slackware | Fedora
Posts: 3

Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by eklavya View Post
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

Last edited by Frozwire; 05-06-2014 at 01:48 AM.
 
Old 05-06-2014, 01:53 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 806

Rep: Reputation: 186Reputation: 186
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

Last edited by AnanthaP; 05-06-2014 at 01:57 AM.
 
Old 05-06-2014, 02:10 AM   #6
AndrewNZ
LQ Newbie
 
Registered: Nov 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
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?
 
Old 05-06-2014, 02:37 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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

Last edited by chrism01; 05-06-2014 at 02:38 AM.
 
1 members found this post helpful.
Old 05-06-2014, 03:10 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 02:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 09:25 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM


All times are GMT -5. The time now is 05:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration