Search two file types within a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search two file types within a for loop
# 1  
Old 06-18-2009
Search two file types within a for loop

I need to search for a csv and a dat file within a for loop then output them to a log table. I can do it individually but I want them together.

See below code:

check csv

count=0
for file in $(ls *.csv) ;
do
count=`expr $count + 1`

writelogf "$argv0: +%-50.50s+\n" $ul
writelogf "$argv0: |%-50.50s${cs}\n" "FILE IN"
writelogf "$argv0: +%-50.50s+\n" $ul
writelogf "$argv0: |%-50.50s${cs}\n" ${file%*}
writelogf "$argv0: +%-50.50s+\n" $ul

done

this gives me the following output:

x.sh: +--------------------------------------------------+
x.sh: |FILE IN |
x.sh: +--------------------------------------------------+
x.sh: |x.csv |
x.sh: +--------------------------------------------------+

I repeat the same type of code for the .dat files and get
x.sh: +--------------------------------------------------+
x.sh: |FILE OUT |
x.sh: +--------------------------------------------------+
x.sh: |x.dat |
x.sh: +--------------------------------------------------+

How can i merge together within one for loop to give me this output:

x.sh: +------------+------------+
x.sh: |FILE IN | FILE OUT |
x.sh: +------------+------------+
x.sh: |x.csv | x.dat |
x.sh: +------------+------------+

the table does not come out like i want it to on the post but you get the idea

Smilie
# 2  
Old 06-18-2009
Code:
ls *csv *dat 2>/dev/null

see, 2>/dev/null is important
# 3  
Old 06-18-2009
Well, the output format you will need to work on (it wont take much more then what you currently have), to find .csv and .dat files, that wouldn't be too hard.

Code:
find . -type f | egrep -i "(csv|dat)$"

You would want to do an if statement to check to see if the filenames end with the dat/csv extension in the loop and then output appropriate data.
# 4  
Old 06-18-2009
I'm not sure how to use syntax below into my current code to get the answer i'm looking for?

more information: both files csv and dat start with the same name i.e. pb_xxxx.csv pb_xxxx.dat.

I change these files from a csv to a fixed length file

I need them to line up in the table:

x.sh: +------------+------------+
x.sh: |FILE IN | FILE OUT |
x.sh: +------------+------------+
x.sh: |pb_123.csv | pb_123.dat |
x.sh: +------------+------------+
x.sh: |pb_456.csv | pb_456.dat |
x.sh: +------------+------------+

I'm not sure the for loop can do this????? i maybe wrong
# 5  
Old 06-18-2009
Quote:
Originally Posted by Rhije
Well, the output format you will need to work on (it wont take much more then what you currently have), to find .csv and .dat files, that wouldn't be too hard.

Code:
find . -type f | egrep -i "(csv|dat)$"

.
no need to use egrep..
Code:
find . -type f \( -name "*.csv" -o -name "*.dat" \)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a loop that will search for a file to thousand machine and know who owns the file

Run a loop that will search for a file to thousand machine and know who owns the file $ for i in abc{01..02} > do > echo -n $i > ssh $i "sudo find / -name .ssh -exec ls -l {} \;|grep id" > done abc01-rw-------. 1 root root 1675 Nov 10 2018 id_rsa abc01-rw-------. 1 root root 1675 Nov 14... (6 Replies)
Discussion started by: invinzin21
6 Replies

2. Shell Programming and Scripting

Bash loop to search file

In the bash when the user inputs an id to search for the bash currently closes, and if a match is found outputs a new file (match.txt). Is it possible to have not close the bash but rather, on the screen "searching for match" and if a match is found "match found in line.." is displayed... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Using a single "find" cmd to search for multiple file types and output individual files

Hi All, I am new here but I have a scripting question that I can't seem to figure out with the "find" cmd. What I am trying to do is to only have to run a single find cmd parsing the directories and output the different file types to induvidual files and I have been running into problems.... (3 Replies)
Discussion started by: swaters
3 Replies

4. Shell Programming and Scripting

Cp -r except certain file types

the following excludes certain directories successfully cp -r probe/!(dir) /destination I want to exclude certain file types and tried unsuccessfully cp -r probe/!(*.avi) /destination (2 Replies)
Discussion started by: tmf
2 Replies

5. Shell Programming and Scripting

Recursive search for string in file with Loop condition

Hi, Need some help... I want to execute sequence commands, like below test1.sh test2.sh ...etc test1.sh file will generate log file, we need to search for 'complete' string on test1.sh file, once that condition success and then it should go to test2.sh file, each .sh scripts will take... (5 Replies)
Discussion started by: rkrish123
5 Replies

6. Shell Programming and Scripting

loop to display whatever number use types

sorry couldnt think of a proper title lol ok i have the following script it asks the user how many html tags they want. Im not sure how to display 2 tags if the user eneters the want only 2 tags tags as in <p></p> or <h1></h1> read -p "How many tags" tags1 if then echo "<$tags1>... (3 Replies)
Discussion started by: gangsta
3 Replies

7. Shell Programming and Scripting

File 1 Column 1 value search in File 2 records, then loop?

Summary: I planned on using Awk to grab a value from File 1 and search all records/fields in file 2. If there is a match in File 2, print the first column value of the record of the match of File2. Continue this search until the end of file 2. Once at the end of file 2, grab the next value in... (4 Replies)
Discussion started by: Incog
4 Replies

8. Shell Programming and Scripting

How to pass multiple file types search pattern as argument to find?

How can I pass $var_find variable as argment to find command? test.sh var_find=' \( -name "*.xml" -o -name "*.jsp" \) ' echo "${var_find}" find . -type f ${var_find} -print # Below statement works fine.. I want to replace this with the above.. #find . \( -name "*.xml" -o -name... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

how to search a keyword within a file using a for loop

hi guys i have a problem here, im trying to stablish a relationship between a text file and an input user for example the script is going to prompt the user for some football team and what the script is going to do is return the colums in which that input is located so far this is what i have ... (6 Replies)
Discussion started by: lucho_1
6 Replies

10. Filesystems, Disks and Memory

associated file types

I have a file of type .for extension .In a guui based unix environment like solaris if I double click on that file a specific program designed by me has to run which takes this file as the parameter and exceutes the program. Can anyone help me? (8 Replies)
Discussion started by: nhk_srd
8 Replies
Login or Register to Ask a Question