The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Ignore some lines with specific words from file comparison jakSun8 Shell Programming and Scripting 2 03-13-2008 12:11 AM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 04:38 PM
reading variables from a file ttshell Shell Programming and Scripting 4 03-13-2006 11: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 05:25 AM
Reading a CSV file into shell variables problem multidogzoomom Shell Programming and Scripting 6 10-11-2005 04:43 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-07-2007
konark konark is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 14
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?
  #2 (permalink)  
Old 11-08-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
awk

Hi,
Try this one:

Code:
awk '{
if (index($1,"#")==0)
print $1".ksh""  "$2
}' filename
  #3 (permalink)  
Old 11-08-2007
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 242
try using

Quote:
echo "${x}.ksh ${y}" |grep -v ^#
instead. Not the most elegant way perhaps...
  #4 (permalink)  
Old 11-08-2007
veer veer is offline
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.
  #5 (permalink)  
Old 11-08-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

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

while read x y
do
   [[ "$x" != \#* ]] && echo "${x}.ksh ${y}"
done < file.txt
Jean-Pierre.
Sponsored Links
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 12:15 PM.


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