[Solved] Help with grep script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Help with grep script
# 1  
Old 01-26-2013
[Solved] Help with grep script

Hi,
I'm having trouble with a script to copy one line out of multiple files in a directory and copy to a file called test. I've tried the code below but it copies one line out of the first file multiple times not one line out of all the files. Would someone help? I'm very new to all this.

Many thanks,
Bob

Code:
#!/bin/bash
for i in *.CSV
do
    sed -n '10p' *.CSV >> test
done


Last edited by radoulov; 01-26-2013 at 01:31 PM.. Reason: Marked as solved.
# 2  
Old 01-26-2013
Code:
#!/bin/bash
for i in *.CSV
do
    sed -n '10p' "${i}" >> test
done

# 3  
Old 01-26-2013
Thank you but I still get the same result. One line out of the first file multiple times.
# 4  
Old 01-26-2013
It should work, remove the file: test and rerun the script.
# 5  
Old 01-26-2013
help with grep script

These are two of the files in the directory I want to copy line 10 from and copy to file "test":

574 McClain.0101.CSV 574 McClain.0103.CSV

when I run the script with the ${i} included, the file "test" is generated but is empty and Unix responds with:

Code:
sed: 574: No such file or directory
sed: McClain.0101.CSV: No such file or directory
sed: 574: No such file or directory
sed: McClain.0103.CSV: No such file or directory

Do you think this is a grep issue?

Many thanks for your help.
Bob

Last edited by radoulov; 01-26-2013 at 01:31 PM..
# 6  
Old 01-26-2013
Did you wrap variable ${i} around double quotes like I posted above?
Code:
sed -n '10p' "${i}" >> test

# 7  
Old 01-26-2013
help with sed script

It worked with the quotes!! but it copies each file twice. Any thoughts on why?
I can live with this if there are no fixes.

Thanks you so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] Having trouble with simple grep search

I have a text file (allWords.txt), that I would like to search through. Here is a snippet of what it looks like... a aah aahed aahing aahs aardvark aardvarks aardwolf ab abaci aback abacus abacuses abaft ...... I would like to use the grep search to search, line by line, for... (8 Replies)
Discussion started by: blackvelvet
8 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Grep multiple files and display first match

I have a need to grep a large number of files, but only display the first result from each file. I have tried to use grep, but am not limited to it. I can use perl and awk as well. Please help! (9 Replies)
Discussion started by: dbiggied
9 Replies

3. Shell Programming and Scripting

[Solved] Issue with grep

Hi everyone I am trying to write a script to check if file systems are mounted, and also validate the permission; then do a whole bunch of other things. I am facing a problem with grep. For example, if the mountpoints are: /dev/XYZ_lv /abc/XYZ jfs2 Nov 25 20:36... (4 Replies)
Discussion started by: nimo
4 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Grep in bash script

Hi Experts, I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script. Error message: "grep: sales.txt: No such file or directory" #!/bin/bash var=1 var1=`awk '{n++} END {print n}' sales.txt` while ] do prod=$var... (6 Replies)
Discussion started by: parpaa
6 Replies

5. UNIX for Advanced & Expert Users

[SOLVED] Grep IP's from xml with name of file

Hi all, actually i have a lot of IP addresses on multiple .xml files on my folder, my tag on xml files are like this, <db-url>jdbc:oracle:thin:@10.100.5.56:1521:DWH01</db-url> i actually can print only the IP addresses awk -F"" '/url/ { print $3 }' *.xml 10.100.5.56 i would like to grep... (2 Replies)
Discussion started by: charli1
2 Replies

6. Shell Programming and Scripting

[Solved] Grep within find command

Platform: AIX 6.1/ksh Question1. I want to grep for the string "CUSTOM_PKMS" in all the files in server except those files with extensions .dbf , .ctl and .dmp I started running the following command but it is taking too long because there are lots of .dbf , .ctl and .dmp files in this... (6 Replies)
Discussion started by: John K
6 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Wildcards used in find, ls and grep commands

Platforms : Solaris 10 and RHEL 5.6 I always get double quotes , single quotes and asteriks mixed up for find, ls and grep commands. The below commands retrieve the correct results. But , unders stress , I get all these mixed up :mad: .So, i wanted to get a clear picture. Please check if... (7 Replies)
Discussion started by: John K
7 Replies

8. Shell Programming and Scripting

[Solved] perl and grep: get a script to look at more then one file

hi guys i have this very messy script, that looks in /var/log/messages.all for an error and reports if it finds the key works how can i get it to look at more then one file, i.e /var/log/message.all * so it looks in old logs as well thanks exit 0 if (isRenderNode(hostname)); my... (4 Replies)
Discussion started by: ab52
4 Replies

9. Shell Programming and Scripting

[solved] Diff between two files by grep

My requiremeny is as follows, I have two files file a A BONES RD,NHILL,3418,VIC 37TH PARALLEL RD,DEEP LEAD,3385,VIC 4 AK RD,OAKEY,4401,QLD A & J FARRS RD,BARMOYA,4703,QLD A B PATTERSON DR,ARUNDEL,4214,QLD A BLAIRS RD,BUCKRABANYULE,3525,VIC file b A BONES... (12 Replies)
Discussion started by: feelmyfrd
12 Replies

10. UNIX for Dummies Questions & Answers

[solved] Script creation (how to include options in the script)

Hi guys i have written a script which takes the options given to him and execute itself accordingly. for example if a script name is doctortux then executing doctortux without option should made doctortux to be executed in automatic mode i.e. doctortux -a or if a doctortux is needed to run in... (4 Replies)
Discussion started by: pinga123
4 Replies
Login or Register to Ask a Question