Loop "for" with 2 input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop "for" with 2 input
# 1  
Old 11-30-2015
Loop "for" with 2 input

Dear All,
I am stacked in how to do a "for" loop with two input. Well, I try to explain better.

In a folder I have a series of files that are coupled;

Code:
a_1.txt
a_2.txt
b_1.txt
b_2.txt
c_1.txt
c_2.txt

if files were independent a "for" loop should be like that:
Code:
 for file in folder/*.txt
do something $file > ${file%%.*}_executed.txt 
done &
wait

My problem is that my command take two file as input, for instance:

Code:
my_command a_1.txt a_2.txt > output

Well, I would like to do it in a for loop but don't know how.

Any suggestion?




PS: Thanks to MadeInGerman for ${file%%.*}
# 2  
Old 11-30-2015
Assuming you're using a recent bash, try (untested):
Code:
for file in *_1.txt; do my_command $file ${file/1/2}; done > output

# 3  
Old 11-30-2015
Try something like:
Code:
for file in folder/*_1.txt
do 
  my_command "$file" "${file%_1.txt}_2.txt" > "${file%_1.txt}_executed.txt" 
done &
wait

This should work with any reasonably POSIX compliant shell, including bash

Last edited by Scrutinizer; 12-01-2015 at 02:11 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-01-2015
Thanks Scrutinizer, works great!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Question about the difference between "for" and "while" loop

dear all, i got some questions about for/while loop when working on redirect command output to for/while loop. take one for example : in for loop : allfiles=`find /var/log -maxdepth 1 -type f -mtime +5` index=1 for ((i=0; i<${#allfiles}; i++)); do echo "$index:${allfiles}" ... (2 Replies)
Discussion started by: tiger2000
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

BASH: Interactive "cp" (and "mv") in a loop

Hi. I have a copy-file script (and a move-file script) that I recently tried to make interactive. I tested the former on three files from a text list, and watched to see what would happen. As the cp command was in a while/do/done loop, there was no pause for input: it wrote the file from the... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

7. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

check input = "empty" and "numeric"

Hi how to check input is "empty" and "numeric" in ksh? e.g: ./myscript.ksh k output show: invalid number input ./myscript.ksh output show: no input ./myscript.ksh 10 output show: input is numeric (6 Replies)
Discussion started by: geoffry
6 Replies
Login or Register to Ask a Question