List files in dir and grep accordingly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List files in dir and grep accordingly
# 1  
Old 10-11-2014
List files in dir and grep accordingly

bash or c-shell On Solaris 10

I need a command that will list the files of a directory, and grep each of those filenames out of a static text file, and then only echo field 2 of the static text file.

Something like this:

HTML Code:
#> ls -1 /home/dir/ | each filename grep $filename static.txt |awk '{print $2}'
# 2  
Old 10-11-2014
To avoid assumptions and guessing, please post desired output example and a representation of `static text file'
# 3  
Old 10-11-2014
this works...

its pretty simple really...here is the command that seems to work in bash. I guess i was just wondering if there was a c-shell way of doing it?


Code:
for file in `ls -1 /home/dir`; do grep $file /home/static.txt |awk '{print $2}'; done

I just free-typed that on my box, seemed good....Anyone know of a C-shell equivalent?
# 4  
Old 10-11-2014
Relying on the output of ls to iterate via a for loop not so good.
Anything that grep could give you, awk can do as well, so there is no need for grep. In fact, Awk most likely can do it all by itself.
... But hey, is your choice.
# 5  
Old 10-11-2014
Most of us find the inconsistencies between csh interactive and non-interactive behavior a strong disincentive for using csh as a scripting language.

As Aia said, (as long as none of the filenames you're processing contain a newline character), the following would be much more efficient than your current script. And, if any of the filenames you're processing do contain a newline character, your current script won't work either. Your current script will also fail if there are any space or tab characters in any of the filenames you're processing.
Code:
#!/bin/ksh
DIR=/home/dir
cd "$DIR"
printf '%s\n' * | awk -v d="$DIR/" '
FNR == NR { fn[++c] = d $0; next }
{ for(i = 0; i <= c; i++) if(index($0, fn[i])) print $2 }
' - /home/static.txt

I generally prefer the Korn shell, but this script will also work with bash or a pure Bourne shell (/bin/sh on Solaris systems).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List no. of files in a directory/sub dir's and also an consolidated report as required

Need help on below query asap. Thanks. The below is the directory structure: /home/suren under /suren the following are the directories /bin /log /error /bin contains the following files abc.txt bcd.ksh cde.sh wer.ksh ghi (file with out any extension) /log contains the following... (1 Reply)
Discussion started by: sureng
1 Replies

2. Shell Programming and Scripting

Copy files to a dir using from a list

Hi all, I'd very grateful for some help with the following: I have a directory with several subdirectories with files in them. All files are named different, even between different subdirectories. I also have a list with some of those file names in a txt file (without the path, just the file... (5 Replies)
Discussion started by: foracoffee
5 Replies

3. UNIX for Dummies Questions & Answers

List files older that 7 days in a dir, excluding all subdirs

Hi, I would like to list all files, older than 7 days, in a directory, but exclude all subdirectories in the find command. If I use find . -type f -mtime +7 all files in the subdirs are also included. How can I exclude them? Regards, JW (6 Replies)
Discussion started by: jwbijl
6 Replies

4. Shell Programming and Scripting

how to list all the files for today in the dir

I am trying following ... ls -ltrh | grep 'Dec 2' but it is displaying files for last year also ..as this dir is full of files from last 3-5 yrs I only want to files for today. e.g . ls -ltrh | grep 'Dec 2' -rw-r----- 1 ajay ajay 0 Dec 2 2010 text23.txt -rw-r----- ... (19 Replies)
Discussion started by: ajaypatil_am
19 Replies

5. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

6. Shell Programming and Scripting

How to get a list of files in a dir w/o sub-directories?

Hi I am looking for the correct syntax to find all files in the current directory without listing sub-directoris. I was using the following command, but it still returns subdirectoris and files inside them: $ ls -laR | grep -v ^./ Any idea? Thanks PS I am in ksh88 (4 Replies)
Discussion started by: aoussenko
4 Replies

7. Shell Programming and Scripting

Generate a change list of files/dir

Is there a tool that can diff a directory and generate a change list of files in that directory based on a previous snapshot on the directory? For example /etc/a.txt:changed /etc/b.txt:removed /etc/c.txt:added Thanks! (1 Reply)
Discussion started by: overmindxp
1 Replies

8. Shell Programming and Scripting

How to copy specified files from list of files from dir A to dir B

Hello, fjalkdsjfkldsajflkajdskl (3 Replies)
Discussion started by: pmeesara
3 Replies

9. Shell Programming and Scripting

grep files without header and move to new dir

Hi, i have number of files in a directory to be processed. the problem is some of the files does not have a header and the process is giving an error of no header found. example of good file : file1 HDR|20080803233401 record 1 record 2 TRA|2 example of a bad file in the same dir ... (6 Replies)
Discussion started by: sitaldip
6 Replies

10. UNIX for Dummies Questions & Answers

Searching list of entries in file for actual files in dir

Hi all, I have a file entries.txt that contains a number of entries all on seperate lines e.g. test1 test2 test3 test4 Then in a directory called /TestFiles I have a number of files that could contain the above text in the file name e.g. qwertytest1.csv qwertytest2.csv... (2 Replies)
Discussion started by: not4google
2 Replies
Login or Register to Ask a Question