reading from a file and pass as variables and ignore # in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading from a file and pass as variables and ignore # in the file
# 1  
Old 11-07-2007
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  
Old 11-08-2007
awk

Hi,
Try this one:

Code:
awk '{
if (index($1,"#")==0)
print $1".ksh""  "$2
}' filename

# 3  
Old 11-08-2007
try using

Quote:
echo "${x}.ksh ${y}" |grep -v ^#
instead. Not the most elegant way perhaps...
# 4  
Old 11-08-2007
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  
Old 11-08-2007
Code:
#!/bin/ksh

while read x y
do
   [[ "$x" != \#* ]] && echo "${x}.ksh ${y}"
done < file.txt

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Pass variables from a text file to a shell script

Hi, I have a text file as follows: a.txt ------ STEPS=3 STEP_DURATION=100 INTERVAL=60 I want to use these values in a shell script. How to go about this? (3 Replies)
Discussion started by: akarnya
3 Replies

2. Shell Programming and Scripting

how to pass variables to s3cmd in a script file

Hi All, How to pass the variable to s3cmd put command? I'm trying to use this command in a script as below: s3cmd put ${upload_path} s3://${upload_dest} where With the above command in script, the file is not getting uploaded. Please help!!! (4 Replies)
Discussion started by: mjavalkar
4 Replies

3. Shell Programming and Scripting

how to ignore multiline comment from a file while reading it

Hi friends , I want to ignore single and multiline comment( enclosed by " \* *\" ) of a file whle reading it. I am using the below code. nawk '/\/\*/{f=1} /\*\//{f=0;next} !f' proc.txt | while read str do ... done The problem is its working partially. that is its failing in one... (1 Reply)
Discussion started by: neelmani
1 Replies

4. Shell Programming and Scripting

How to ignore single or multiple lines between /* and */ while reading from a file in unix?

I have a file proc.txt: if @debug = 1 then message 'Start Processing ', @procname, dateformat(now(*), 'hh:mm:ss'), @julian type info to client; end if; /* execute immediate with quotes 'insert into sys_suppdata (property, value, key_name) location ''' || @supp_server || '.' ||... (5 Replies)
Discussion started by: kidncute
5 Replies

5. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

6. Shell Programming and Scripting

Reading variables from a file

Hi, I have an issue that hope someone will be able to help me with.... I'm using a while-read loop to read lines from a file that contain variables, and I want the variables substituted, but I can't get it to work - the below example with explain: while read line do echo $line ... (5 Replies)
Discussion started by: AndyG
5 Replies

7. Shell Programming and Scripting

Pass variables to a Unix script from a file

Hi, I am running a Java program from a unix script. I need to pass a variable to the Java code from a file. Here are teh details: cat Parm <<this is my Parameter file>> queuename=queue1 and my shell script is : #!/bin/ksh . ./Parm /opt/java1.5/bin/java -classpath ./java.jar... (1 Reply)
Discussion started by: sangharsh
1 Replies

8. Shell Programming and Scripting

Reading variables from CSV file

Hi I am using KSH and trying to read variables from a csv file. I've set the IFS=, and it workds. Problem is where one of the values is text containing a comma. For example the following lines exist in my file. How can I read everything between the quotes into a single variable? APW13812,,1... (2 Replies)
Discussion started by: ventris
2 Replies

9. Shell Programming and Scripting

reading variables from a file

Hi, Could anyone help me in understanding what I am missing.. I have a text file with the following info. INFILE=> #Name Variable=<value> #--------------------------------- name1 inargs="-a Filename1.$VAR.csv -f Filename2.$VAR.csv -c File.c" name1 ... (4 Replies)
Discussion started by: ttshell
4 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question