Quick Question concerning Reading and Writing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick Question concerning Reading and Writing
# 1  
Old 11-09-2011
Quick Question concerning Reading and Writing

First off, this is not homework nor for any class.

I have a file (column) of data such as:

12.56
2.78
0.54
22.7
... etc.

(call the file "num") I want to read in each line, then check to see whether it's greater than or equal to a value, say "1". If it is, save it in one file, if it's not, save it in another file. So in other words, I'll end up with two files, each a column of data. Below is what I tried:

Code:
read a < num
if [ "${a}" -ge 1 ]; then 
    ${a} >> OK.txt
else
    ${a} >> BAD.txt
fi

It appears to work for the very first line, but then stops. I'm sure there's a simple way to get what I want, but I don't know how. Help, would certainly be appreciated! Thanks.
# 2  
Old 11-09-2011
Try..

Code:
while read a 
do
  if [ "${a}" -ge 1 ]; then 
      ${a} >> OK.txt
  else
      ${a} >> BAD.txt
  fi
done < num

# 3  
Old 11-10-2011
Quote:
Originally Posted by Ygor
Try..

Code:
while read a 
do
  if [ "${a}" -ge 1 ]; then 
      ${a} >> OK.txt
  else
      ${a} >> BAD.txt
  fi
done < num

Thanks Ygor, that *almost* works!
The problem I get is that for some reason no values are written to OK.txt
Also, for each line that is run, I get an error similar to this:

./num.sh[26]: 1.3004: not found [No such file or directory]

so for the above value (1.3004) it does not write it to OK.txt
However, it appears to write out the values that it should to BAD.txt for some reason? I don't know if it matters, but I'm running the script in ksh. Thanks!

Last edited by astropi; 11-10-2011 at 01:40 AM.. Reason: clarity
# 4  
Old 11-10-2011
Code:
 nawk '{if($0>1){printf("%s\n",$0) >> "ok.txt" }else{printf("%s\n",$0) >> "bad.txt"}}' inputfile

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 11-10-2011
Quote:
Originally Posted by itkamaraj
Code:
 nawk '{if($0>1){printf("%s\n",$0) >> "ok.txt" }else{printf("%s\n",$0) >> "bad.txt"}}' inputfile

Thanks, it looks like it should work, but unfortunately nawk does not work, since I get:

-bash: nawk: command not found

... so, if possible it would be great to get the code Ygor started on working.

---------- Post updated at 02:26 AM ---------- Previous update was at 02:24 AM ----------

Quote:
Originally Posted by astropi
Thanks, it looks like it should work, but unfortunately nawk does not work, since I get:

-bash: nawk: command not found

... so, if possible it would be great to get the code Ygor started on working.
Of course, if I just run it in awk instead of nawk it appears to work Smilie

If possible, I would still love to know why the code Ygor gave does not work? Thanks!
# 6  
Old 11-10-2011
use awk ( instead of nawk )
# 7  
Old 11-10-2011
Yes, I did get it to work using awk.
As noted, if someone can tell me why Ygor's code did not work, I would like to know. Thanks for everyone's help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading and writing in same file

Hi All, Here is my requirement. I am grepping through the log files and cutting some fields from the file to generate a csv file. Now I have to check if 2nd field is having some fixed value then with the help of 4th field I have to look in same log and run another grep command to retrieve the... (11 Replies)
Discussion started by: kmajumder
11 Replies

2. Shell Programming and Scripting

reading from and writing to serial port

hello all, please any one can provide me the shell script to write data to /dev/ttyS1 and read responce from /dev/ttyS1 and compare the responce to some string .. thnxx ---------- Post updated at 03:35 PM ---------- Previous update was at 11:46 AM ---------- any body help me.. how do i... (1 Reply)
Discussion started by: yashwantkumar
1 Replies

3. UNIX for Dummies Questions & Answers

Reading and writing data to and from multiple files

Hi, I have several text files. One main file contains the detail data, other have some information to extract data from the main file, and some are empty files. Examples are shown below: The main file look like: MainFile.txt >Header1 data1...data1... >Header2 data2...data2... ... ...... (2 Replies)
Discussion started by: Fahmida
2 Replies

4. Programming

I need help with file reading/writing in C

Hello everybody, I'm trying to code a program which makes the following: It sends an ARP request frame and when it gets the reply, extracts the IP address of source and writes it to a .txt file. This is gonna be done with many hosts (with a for() loop), so, the text file would look like... (2 Replies)
Discussion started by: Zykl0n-B
2 Replies

5. UNIX for Dummies Questions & Answers

Log File Writing and Reading

Hi all, I have the following shell script code which tries to sftp and writes the log into the log file. TestConnection () { echo 'Connection to ' $DESTUSERNAME@$DESTHOSTNAME $SETDEBUG if ]; then rm $SCRIPT ; fi touch $SCRIPT echo "cd" $REMOTEDIR >> $SCRIPT echo "quit" >>... (10 Replies)
Discussion started by: valluvan
10 Replies

6. UNIX for Dummies Questions & Answers

reading ,writing,appending ,manipulating a file.

Hi my prob statement is to create a new file or to append to the 1tst file the followign chages. File 1: txt file. portfolio No a b c d abc 1 Any Any Any charString cds 2 values values values charString efd 3 can can can charString fdg 4 come come come charString... (4 Replies)
Discussion started by: szchmaltz
4 Replies

7. UNIX and Linux Applications

Reading and Writing from Excel using Unix scripting

Hi All, I want to read and write into Excel files. I want my script to generate the required output directly in the Excel format. Is it possible through unix scripting or shall I have to use some other language for this purpose? Waiting for your suggestion. Thanks in Advance. ... (3 Replies)
Discussion started by: AshishK
3 Replies

8. Shell Programming and Scripting

reading/ writing to sockets

Is it possible to read from or write to sockets with ksh? I use AIX 5.3 (1 Reply)
Discussion started by: rein
1 Replies

9. Programming

Reading and Writing file on LAN

Hi gurus I am not a C programmer but I need to read and write files on a computer on LAN using IP address. Suppose on a computer that has an IP 192.168.0.2 Any help or code example. I did in JAVA using URL, but do not know how to do in ANSI-C. In java: ------- URL url = new... (3 Replies)
Discussion started by: lucky001
3 Replies

10. UNIX for Dummies Questions & Answers

Reading and Writing Files ?

Hello, Our group is just starting to get into UNIX here. We are former REXX users on a VM Mainframe. Can someone point me to the documentation to read a file and write a file in a Unix Shell Script or does this have to be done in another language? Thank you in advance... Dave (3 Replies)
Discussion started by: tracydp
3 Replies
Login or Register to Ask a Question