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 05-29-2013, 07:03 AM   #1
ajaylinuxrk
LQ Newbie
 
Registered: May 2013
Posts: 1

Rep: Reputation: Disabled
Trying to write a bash script need some suggesstions.


Gurus,

I want to write a bash script to read a line by line from a file and execute in a command and redirect the output to a file.I am able to think through the below syntax but not sure how to redirect the output of the command to another file.Need suggestions.

#/bin/bash

for i in "cat /tmp/file"
do
command | grep "$i"

done.

Thanks in Advance.
 
Old 05-29-2013, 07:07 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Just try:
Code:
#/bin/bash
while read -r i
do
command | grep "$i" > /path/to/output.txt
done < /tmp/file
Use while+read instead of for loop for reading output from a file line by line. The > /path/to/output.txt part redirects output of the command into a file named /path/to/output.txt.
 
1 members found this post helpful.
Old 05-29-2013, 07:12 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Given that each cmd is tun separately, you probably want '>>' i.e. append, instead of > (output), which will overwrite the file.
 
Old 05-29-2013, 11:11 AM   #4
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,668
Blog Entries: 10

Rep: Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077
Code:
REPORT=/home/c9admin/amn_ami_delete3.rpt
... stuff...
... stuff...
echo "" > "$REPORT" #truncates "$REPORT"
... stuff...
	do ec2-deregister $i >> "$REPORT" # appends output of each iteration into "$REPORT"
 
Old 05-29-2013, 12:00 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
A slight alternative to shivaa's example and allowing the incorporation of chrism01's suggestion:
Code:
while read -r line
do
    command | grep "$line"
done< /tmp/file > /path/to/output.txt
 
Old 05-30-2013, 06:32 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
If the "command" prints the same output every time, and you just want to capture all the matching lines from the file, then this would probably do you better:

Code:
command | grep -f /tmp/file > /path/to/output.txt
 
  


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
Can somebody write me a bash script trintukaz Programming 9 09-26-2011 04:02 PM
Trying to write a bash script christianunix Linux - Newbie 5 10-26-2007 06:30 PM
read write in bash script yhus Programming 6 09-18-2006 01:23 PM
Need to write a bash script imagineers7 Linux - General 5 05-10-2006 12:17 AM
Anoyone willing to write a BASH script for me? pilot1 Programming 4 09-16-2003 09:56 PM


All times are GMT -5. The time now is 06:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration