The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
reading from stdin in a shell script mmesford Shell Programming and Scripting 4 07-04-2009 03:28 AM
Reading data from a file through shell script Dip Shell Programming and Scripting 2 10-20-2008 01:51 PM
reading XML datas via Shell Script freepal Shell Programming and Scripting 1 02-08-2008 11:52 AM
Reading a table in a shell script luiscarvalheiro Shell Programming and Scripting 13 08-10-2006 07:16 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 05:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-21-2008
Dip Dip is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 11
file reading through shell script

For reading a file through shell script I am using yhe code :

while read line
do
echo $line
done<data.txt

It reads all the line of that file data.txt.
Content of data.txt looks like:

code=y
sql=y
total no of sql files=4
a.sql
b.sql
c.sql
d.sql
cpp=n
c=y
total no of c files=1
a.c
.....
.......

Now, if sql=y and total no of sql files > 0 then want to fetch the only sql file names via shell script.
the script should fetch
a.sql
b.sql
c.sql
d.sql
  #2 (permalink)  
Old 10-21-2008
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120

Code:
> cat temp.txt
code=y
sql=y
total no of sql files=4
a.sql
b.sql
c.sql
d.sql
cpp=n
c=y
total no of c files=1
a.c

> cat get_sql.ksh
echo "Enter the string.. \c";
read str
option=`grep "$str=" temp.txt|awk -F"=" '{print $2}'`
if [ "$option" = "y" ]
then
        nooffile=`grep "$str files=" temp.txt|awk -F"=" '{print $2}'`
        if [ "$nooffile" > "0" ]
        then
                echo "FILES...."
                grep "\.${str}" temp.txt
        fi
fi

[OUTPUT]
Enter the string.. sql
FILES....
a.sql
b.sql
c.sql
d.sql

Enter the string.. c
FILES....
a.c
[/OUTPUT]
  #3 (permalink)  
Old 10-21-2008
Dip Dip is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 11
Thanks a lot palsevlohit....its working fine.
  #4 (permalink)  
Old 10-21-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,938
It is also possible to do it all within a shell script without using external utilities such as grep i.e.

Code:
#!/usr/bin/bash

yes=0

IFS='='
while read line value
do
    [[ $line == "sql"  && $value == "y" ]] && ((yes++))
    [[ $line == "total no of sql files" && $value > "0" ]] && ((yes++))
    [[ $yes == 2 && ${line: -4} == ".sql" ]] && echo $line
done < file

  #5 (permalink)  
Old 10-21-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092
call below script with whatever parameter you like, such as 'sql' or 'c' will print the result accordingly


Code:
nawk -v f="$1" -F"=" '{
if ($1==f && $2="y")
	f1=1
if(f1==1 && $2>0)
	f2=1
file=sprintf(".%s",f)
if(f2==1 && index($0,file)!=0)
	print $0
}' filename

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0