Inputting multiple files into one command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inputting multiple files into one command
# 1  
Old 07-21-2013
Inputting multiple files into one command

I am trying to read 30 files into a command. The first file contains 10 lines and goes into this part of the command as "x"

/tmp/filearrange.sh $x

The group of files (20 files, I call them variable $i) need to be the second argument in the command and they need to be read so that they are paired with the other command.

So each line is read one at a time, example
Code:
invfile contents (variable x)     20 files (these files are separate files) ($filex)
inventory-usage-4                  inventory.txt
inventory-usage-3                  shelflist.txt
inventory-usage-4                  stocklist.txt
etc                                           etc,  up to 20 files

So
Code:
/tmp/filearrange.sh $x  -add $filex

looks like this (example)

/tmp/filearrange.sh inventory-usage-4 -add inventory.txt
/tmp/filearrange.sh inventory-usage-3 -add shelflist.txt

What I have tried so far is to use paste but it takes only 12 lines. Then I tried filedescriptors, but it appears I would need to use as input a variable to the file descriptor like this:

Code:
exec 3<$filex
where filex= `cat /tmp/*.txt`

and I can't get this working.


Code:
while read i && read x<&3
do
  /tmp/filearrange.sh "$i" -add -f "$filex"
done < /tmp/ exec 3<$filex

I'm really confused about this in bash. Any help is appreciated.

Last edited by Scrutinizer; 07-21-2013 at 06:14 PM..
# 2  
Old 07-21-2013
I don't understand what you're trying to do.

If invfile contains:
Code:
inv-1
inv-2

and your "group of files" is files named g1, g2, and g3, what arguments do you want to pass to your bash script and exactly how many times should that script invoke /tmp/filearrange.sh?

And for each of those invocations, exactly what arguments should be passed to /tmp/filearrange.sh (after variable expansion)?
# 3  
Old 07-21-2013
Need to iterate through files

I need to have each line of the first file become the first argument for the command:

Code:
/tmp/filearrange.sh  invfile_line1  -add -f

The second group of 20 files is the problem. I need to have each one of those files plug into the -add -f portion of the command. Like this:

Code:
/tmp/filearrange.sh  line1_of_invfile -add -f inventory.txt

Then the script iterates like this:

Code:
/tmp/filearrange.sh  line2_of_invfile -add -f shelflist.txt

and then iterates again like this:

Code:
/tmp/filearrange.sh  line3_of_invfile -add -f stocklist.txt

and so on. So the script should iterate through invfile and then use the .txt files as the input for the -add -f argument in the command.

I have tried read -r but as I need more than one file for the second argument of the command this doesn't work.
# 4  
Old 07-21-2013
Stop saying what you have tried and clearly explain what you want!

You have said that there are 10 lines in the 1st file and there are 20 other files. You have not shown what happens after the 10 lines have been read from the 1st file. The and so on gives no indication of whether the last 10 files are ignored, the 1st 10 files are repeated, or something completely different is desired.

I repeat: In a simplified example, if invfile contains:
Code:
inv1
inv2

and you invoke your script with the arguments:
Code:
yourscript invfile g1 g2 g3

I'm guessing that you want to invoke:
Code:
/tmp/filearrange.sh inv1 -add -f g1
    and
/tmp/filearrange.sh inv2 -add -f g2

but I have no idea what you want to have happen with g3.

With this simplified example, please show us exactly what other invocations of /tmp/filearrange.sh should occur or, if I guessed wrong on the 1st two invocations, show us all of the invocations that your script should produce!
# 5  
Old 07-22-2013
Sorry it is not too clear, here is more info.

Here is what it should look like:

The invfile line1 means line 1 of invfile, and the g1 means file1, the g2 means file2. So for each line of invfile, there is a corresponding separate file that is the next argument for the command ( the -add -f processes each file).
Let me know if it still doesn't make sense.

Code:
/tmp/filearrange.sh invfile line1 -add -f g1
    
/tmp/filearrange.sh invfile line2 -add -f g2

/tmp/filearrange.sh invfile line3 -add -f g3

/tmp/filearrange.sh invfile line4 -add -f g4

/tmp/filearrange.sh invfile line5 -add -f g5

/tmp/filearrange.sh invfile line6 -add -f g6

/tmp/filearrange.sh invfile line7 -add -f g7

/tmp/filearrange.sh invfile line8 -add -f g8

/tmp/filearrange.sh invfile line9 -add -f g9

/tmp/filearrange.sh invfile line10 -add -f g10

/tmp/filearrange.sh invfile line11 -add -f g12

/tmp/filearrange.sh invfile line12 -add -f g13

/tmp/filearrange.sh invfile line13 -add -f g14

# 6  
Old 07-22-2013
Quote:
Originally Posted by newbie2010
Here is what it should look like:

The invfile line1 means line 1 of invfile, and the g1 means file1, the g2 means file2. So for each line of invfile, there is a corresponding separate file that is the next argument for the command ( the -add -f processes each file).
Let me know if it still doesn't make sense.

Code:
/tmp/filearrange.sh invfile line1 -add -f g1
/tmp/filearrange.sh invfile line2 -add -f g2
/tmp/filearrange.sh invfile line3 -add -f g3
/tmp/filearrange.sh invfile line4 -add -f g4
/tmp/filearrange.sh invfile line5 -add -f g5
/tmp/filearrange.sh invfile line6 -add -f g6
/tmp/filearrange.sh invfile line7 -add -f g7
/tmp/filearrange.sh invfile line8 -add -f g8
/tmp/filearrange.sh invfile line9 -add -f g9
/tmp/filearrange.sh invfile line10 -add -f g10
/tmp/filearrange.sh invfile line11 -add -f g12
/tmp/filearrange.sh invfile line12 -add -f g13
/tmp/filearrange.sh invfile line13 -add -f g14

It doesn't make sense!

You said there are 10 lines in invfile. But you invoke /tmp/filearrange.sh with 13 of those 10 lines??? Please explain what is supposed to happen!

You said there are 20 files in your group of files (here called g1 through g20 for convenience), but you don't show what happens to g15 through g20 and you don't explain why g11 wasn't used with invfile line 11. Please explain how g* files are paired with lines, what happens if there aren't enough lines, what happens if there aren't enough g* files, and explain (in English) what is supposed to happen to g* files that don't have a matching line in invfile!

And, you have explained that invfile contains the 1st argument to be passed to /tmp/filearrange.sh, but you have yet to explain how the group of 20 files are passed to your script. Are the names of those files given on the command line to your bash script or are they in another file whose name is passed to your bash script? If their names are in a file, what is the name of that file?

I can't help you write a shell script if I don't understand what that script is supposed to do and how the script is going to be called.
# 7  
Old 07-22-2013
from what i could understand your need...check something like this works out for you

Code:
#!/bin/bash

count=0
for i in $(cat invfile)
do
  count=$((count+1))
  filex=$(cat /tmp/*.txt  | sed -n "$count"p)
  /tmp/filearrange.sh $i -add -f $filex
done

This User Gave Thanks to rajamadhavan For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using -text command and creating multiple files

Currently using the below script to being all compressed files .gz files from source folder and appending to the target txt file uncompressed. Teh target txt file is getting too large in size, right now the size of the target txt file is almost 350GB hadoop fs -text... (3 Replies)
Discussion started by: cplusplus1
3 Replies

2. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

3. Shell Programming and Scripting

SED command using multiple input files

What is the syntax to use multiple input files in a SED command. i.e. substitute a word with a phrase in every file in a directory. for every file in /usr/include that has the word "date" in the file grep -l '\<date\>' /usr/include/*.h find each occurrence of the word "time" in the file &... (3 Replies)
Discussion started by: sheoguey
3 Replies

4. Shell Programming and Scripting

using mv command for moving multiple files in a folder

Hi, I have a requirement where I need to move Bunch of folders containing multiple files to another archive location. i want to use mv command .I am thinking when we use mv command to move directory does it create directory 1st and then move all the files ? e.g source... (4 Replies)
Discussion started by: rkmbcbs
4 Replies

5. Homework & Coursework Questions

C++ inputting multiple strings

Hi All, We've been given the exercise below and I'm stumbling at the first block because we have to take in 20 student names and I don't know how to store them! :( I know that I can create (initialize) 20 different char arrays but this seems wrong somehow... What's the best way to store... (2 Replies)
Discussion started by: pondlife
2 Replies

6. Shell Programming and Scripting

how to use multiple files in sed with w command

i have a command like : sed -n 's/^* /&/w even' <file if i want to write to multiple files like sed -n 's/^* /&/w zero two three' < file its not working it is taking "zero two three" as a single file i want to write to 3 seperate files . pls can anyone help me (2 Replies)
Discussion started by: santosh1234
2 Replies

7. UNIX for Dummies Questions & Answers

Unrar Multiple Files with Command

I work with multiple archive files, mostly rar. I understand the basics of rar and zip. I'm looking for a way to decompress multiple rar files with a single command. Hopefully each file would be unrared into dir with same name as archive. I normally just do this manually, but sometimes I'm... (2 Replies)
Discussion started by: dobbs
2 Replies

8. Shell Programming and Scripting

Splitting input files into multiple files through AWK command

Hi, I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below for file in *.txt do grep -i -h "value1|value2" $file > $file; done. My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)
Discussion started by: arund_01
3 Replies

9. Shell Programming and Scripting

how to rename multiple files with a single command

Hi I have following list of files at a path: 01.AR.asset 01.AR.index 01.AR.asset.vf 01.AR.asset.xv I want to rename all these files as follows: 73.AR.asset.Z 73.AR.index.Z 73.AR.asset.vf.Z 73.AR.asset.xv.Z Can any body give me a single command to acheive the above results. ... (5 Replies)
Discussion started by: tayyabq8
5 Replies

10. UNIX for Dummies Questions & Answers

Perform a command to multiple files

How do I perform a command to multiple files? For example, I want to look at all files in a directory and print the ones that do not contain a certain string. How do I go about doing this? (4 Replies)
Discussion started by: mcgrawa
4 Replies
Login or Register to Ask a Question