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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Ignore some lines with specific words from file comparison jakSun8 Shell Programming and Scripting 2 03-12-2008 09:11 PM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 01:38 PM
reading variables from a file ttshell Shell Programming and Scripting 4 03-13-2006 08:04 AM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 02:25 AM
Reading a CSV file into shell variables problem multidogzoomom Shell Programming and Scripting 6 10-11-2005 01:43 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-07-2007
Registered User
 

Join Date: Oct 2007
Posts: 13
Question reading from a file and pass as variables and ignore # in the file

file.txt contains
------------------
sat1 1300
#sat2 2400
sat3
sat4 500
sat5


I need to write a shell script that will output like the below
#output

sat1.ksh 1300
sat3.ksh
sat4.ksh 500
sat5.ksh


my try
-------
#!/bin/ksh

while read x y
do
echo "${x}.ksh ${y}"
done < file.txt

issues:
1) It doesnt ignore the 2nd line with # in the beginning.how to do it here.
2) Can we use awk or sed for the file reading? if yes then how?
Reply With Quote
Forum Sponsor
  #2  
Old 11-07-2007
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 483
awk

Hi,
Try this one:

Code:
awk '{
if (index($1,"#")==0)
print $1".ksh""  "$2
}' filename
Reply With Quote
  #3  
Old 11-07-2007
Registered User
 

Join Date: Oct 2007
Posts: 170
try using

Quote:
echo "${x}.ksh ${y}" |grep -v ^#
instead. Not the most elegant way perhaps...
Reply With Quote
  #4  
Old 11-07-2007
Registered User
 

Join Date: Nov 2007
Posts: 23
OK..chk this cmdline out

sed '/^#/d;s/\(.*\) \(.*\)/\1.sh \2/' file.txt

The above is the best fix with sed cmd.

Looping with while or for is not recommended unless u feel the need for the same. But if still u insist with while cmd then it goes as below

while read x y
do
echo "$x $y" | grep "^#" > /dev/null 2>&1
[ $? -ne 0 ] && echo "${x}.sh $y"
done < file.txt

With both sed and while u will get the similar output. But while is timeconsuming and too many cmds involved.
Reply With Quote
  #5  
Old 11-08-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Code:
#!/bin/ksh

while read x y
do
   [[ "$x" != \#* ]] && echo "${x}.ksh ${y}"
done < file.txt
Jean-Pierre.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0