LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-29-2012, 09:06 PM   #1
WeiYi
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Rep: Reputation: Disabled
How to run a single program on consecutive files and send output to new files.


Hello all, I'm new to linux and I have a question about running a java program on text files and sending the output to new files with the same name but adding a prefix.

Right now I can do this with a single file using the following command.

cat filename.txt | java -cp (java program stuff) > newfile.txt

I want to know if there is a way I can take a mass of files (all located in a single folder), run this program on each individual file and send the output to new files using the filename of the original as part of the new file name?

If this is possible it would be great.
 
Old 06-29-2012, 09:36 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
You can use the for loop. To copy all files from a specific directory and append '_new' to the new file:
Code:
for files in /path/to/files/* ; do cat "$file" | java -cp (java program stuff) > "$file"_new ; done
 
Old 06-29-2012, 10:02 PM   #3
WeiYi
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
I'd like ask just two questions, I'd prefer to add a prefix to the front would it still work if I did new_"$file" if it won't work I'll deal with it.

Secondly would the output be in the directory I'm currently in, or would it be in the directory the files come in.

Ok I lied three questions do I need to specify the full directory structure?

Either way thank you for your prompt reply.
 
Old 06-29-2012, 10:16 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
Quote:
Originally Posted by WeiYi View Post
I'd like ask just two questions, I'd prefer to add a prefix to the front would it still work if I did new_"$file" if it won't work I'll deal with it.
Yes, it would.

Quote:
Originally Posted by WeiYi View Post
Secondly would the output be in the directory I'm currently in, or would it be in the directory the files come in.
The new files would be in the directory you're working in. To save it to a specific directory, specify the directory in the re-direction.

Quote:
Originally Posted by WeiYi View Post
Ok I lied three questions do I need to specify the full directory structure?
If you issue the commands from the directory with the files, no.
 
Old 06-29-2012, 11:10 PM   #5
WeiYi
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
I tried the for loop and an odd thing is happening. This error continues to repeat itself. "cat: : No such file or directory" Oddly in the folder I directed the output to a single empty file named with only the prefix I wanted to add now exits.

I am typing in the command as such. Am I doing something wrong?

for files in FilesToProcess/* ; do cat "$file" | java -cp (java program stuff) > newFolder/new_"$file" ; done
 
Old 06-29-2012, 11:20 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
There are a few errors

1) it needs to be "for file in ...", not "for files in ....". Notice how later on you're referencing $file, this needs to match the variable you are using at the start of the for loop.

2) you can't prefix the file with new_"$file" if you give it the full path, otherwise it'll stick the new_ before the directory structure (ie: FilesToProcess/file.txt becomes new_FilesToProcess/file.txt). You'll need to use a combination of dirname and basename if you want to prefix it:
Code:
for file in /path/to/files/* ; do dir=$(dirname "$file"); base=$(basename "$file"); cat "$file" | java -cp (java program stuff) > "$dir/new_$base" ; done
 
1 members found this post helpful.
Old 06-29-2012, 11:59 PM   #7
WeiYi
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
@suicidaleggroll

Thank you, your solution worked. Though I must've done something wrong as the processed files ended up in the original folder rather than the destination, but do to the prefix being added moving them to the proper folder wasn't a problem.

Anyway thank you again, it was all very helpful.
 
Old 06-30-2012, 12:29 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Didn't notice you wanted to move them to a new folder, in that case just remove the dir=$(dirname "$file") line, and hard code your destination folder in the java output:

Code:
for file in /path/to/files/* ; do base=$(basename "$file"); cat "$file" | java -cp (java program stuff) > "/path/to/newFolder/new_$base" ; done
 
  


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
Perl Extracting 450000+ files in subdirs into single output file in current dir vamsiv Programming 2 10-05-2011 09:39 PM
i want to run c++ program on fedora and it give errors of its header files. iqra Fedora 7 05-23-2010 02:07 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
consecutive 'write's or 'send's on a socket ananthbv Programming 2 09-26-2005 04:51 AM
Running a program inside my QT application does not produce needed output files??? keos Programming 0 02-23-2004 12:37 AM


All times are GMT -5. The time now is 10:43 PM.

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