LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-21-2011, 08:59 PM   #1
antcuenco
LQ Newbie
 
Registered: Aug 2010
Location: Singapore
Posts: 10

Rep: Reputation: 0
For loop


Hi Guys,

Need your help. I am just starting out scripting and am dealing with loops today. My server is receiving random numbers of xml files daily, and I need to write a script that gets the file name with a certain line (8th line) on each of the daily xml file to be written on to a new file. But, I cannot get it right. Tried out to get the file name first, but cant seem to make it right, it's still writing files that are not the present day file. Help me guys! Thanks in advance.

for file in [find . -name '*.xml' -mtime -1]; do echo $file; done
[find
.
-name
ABC-123456-2011-11-22.xml ABC-148415-2011-11-17.xml CDE-150078-2011-11-17.xml CDE-678910-2011-11-22.xml
-mtime
-2]
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-21-2011, 09:08 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
I suggest you match content in the xml files, and print out those lines, instead of looking for the 8th line.

This command will dump all lines containing STUFF from all xml files into one new file called new_file.txt.

Code:
grep 'STUFF' *.xml > new_file.txt
BTW, if you really want the 8th lines, you can get them with something like:
Code:
find . -name '*.xml' -exec sed -n '8p' {} \; > new_file.txt
That said, to the point of your original question: your for loop is also malformed. The bash manual says:
Code:
 for name [ [ in [ word ... ] ] ; ] do list ; done
There, from a documentation standpoint, the [ means "this clause is optional". If you use a [ in a script, it means to start evaluating a conditional expression.

Last edited by jhwilliams; 11-21-2011 at 09:39 PM.
 
1 members found this post helpful.
Old 11-21-2011, 09:13 PM   #3
ydjluv
LQ Newbie
 
Registered: Apr 2005
Posts: 8

Rep: Reputation: 1
agreed

I agree with jhwilliams.. this would be your best option considering it gives you a way to differentiate the lines so you would just make the 8th line say STUFF or whatever and use the following command to dump it into another file...

grep 'STUFF' *.xml > new_file.txt
 
Old 11-21-2011, 09:17 PM   #4
antcuenco
LQ Newbie
 
Registered: Aug 2010
Location: Singapore
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks! But I got to do it for all present day files that came in that day. Was thinking of using loops to get it done and then add in your code in the command part of the loop.
 
Old 11-21-2011, 09:25 PM   #5
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Quote:
Originally Posted by antcuenco View Post
Thanks! But I got to do it for all present day files that came in that day. Was thinking of using loops to get it done and then add in your code in the command part of the loop.
Code:
find . -name '*.xml' -mtime 0 -exec sed -n '8p' {} \; > new_file.txt

Last edited by jhwilliams; 11-21-2011 at 09:39 PM.
 
2 members found this post helpful.
Old 11-21-2011, 09:45 PM   #6
antcuenco
LQ Newbie
 
Registered: Aug 2010
Location: Singapore
Posts: 10

Original Poster
Rep: Reputation: 0
Works great!!! Thank you jhwilliams! Really appreciate it. Have a great day ahead sir.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration