Reading Characters from txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading Characters from txt file
# 1  
Old 03-04-2005
Bug Reading Characters from txt file

Hello,

I am new to shell scripting, and I am trying to create a script that reads an input like the following

firstname:lastname:age
firstname:lastname:age
firstname:lastname:age

in a text file. I have a 2 part question. First how do I open the file in a shell script. And then how can I read the "firstname" and put it into a variable, then read the "lastname" into a different variable, and so on until I reach the end of the file. I dont want the ':' in the variables at all.

Thanks
# 2  
Old 03-04-2005
#!/bin/ksh

file='path2inputFile'

while IFS=: read firstname lastname age junk
do
echo "firstname->[${firstname}] lastname->[${lastname}] age->[${age}]
done < "${file}"
# 3  
Old 03-04-2005
Code:
#!/usr/bin/ksh

while read line
do
  fName=`echo $line | awk -F":" '{ print $1 }'`
  lName=`echo $line | awk -F":" '{ print $2 }'`
  age=`echo $line | awk -F":" '{ print $3 }'`

done < file1

# 4  
Old 03-04-2005
Quote:
Originally Posted by vgersh99
#!/bin/ksh

file='path2inputFile'

while IFS=: read firstname lastname age junk
do
echo "firstname->[${firstname}] lastname->[${lastname}] age->[${age}]
done < "${file}"
This method works great, although I am writing the output to a file, and it puts the new line ^M character in it. How can I remove it?
# 5  
Old 03-04-2005
Quote:
Originally Posted by TexasGuy
This method works great, although I am writing the output to a file, and it puts the new line ^M character in it. How can I remove it?
the 'echo' is simply for the demonstration purpose on how to access the 'read' variable.
# 6  
Old 03-05-2005
You can use awk to do this simply as,

Code:
awk -F ":" '{ print "Firstname="$1" Lastname="$2" Age="$3 }' <filename>

IF you are getting ^M characters then your input file is using DOS encoding. To remove that use as,

Code:
dos2ux <filename>

HTH.
# 7  
Old 03-05-2005
It would be dos2unix rather than dos2ux.

Correct me if Iam wrong.

Vino
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

' for reading (No such file or directory) `Solaris_websummary.txt

echo "1.1 Apply latest OS patches;" awk '/1.2 Install/ {P=0} P {print $0} FNR==1{printf("From file %s:\n", FILENAME)} /1.1 Apply/ {P=1}' solarisappsummary.txt solarisdbsummary.txt solaris_websummary.txt echo "1.2 Install TCP Wrappers;" awk '/1.3 Install/ {P=0} P {print $0}... (1 Reply)
Discussion started by: alvinoo
1 Replies

2. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

3. UNIX for Dummies Questions & Answers

C-Shell script help reading from txt file

I need to write a C-Shell script with these properties: It should accept two arguments on the command line. The first argument is the name of a file which contains a list of names, and the second argument is the name of a directory. For each file in the directory, the script should print the... (1 Reply)
Discussion started by: cerce
1 Replies

4. Shell Programming and Scripting

Reading txt files using the awk

Dear Shell scripters, I have a small code which copy the txt files from some destination to file name OutPutFile. I want to modify this script to introduce several constant. The string it is reading is like ... (2 Replies)
Discussion started by: nrjrasaxena
2 Replies

5. Programming

Reading a particular line from a .txt file

Hi, I have a .txt file which contains the x, y and z co-ordinates of particles which I am trying to cast for a particular compound. The no. of particles present is of the order of 2 billion and hence the size of the text file is of the order of a few Gigabytes. The particles have been casted layer... (5 Replies)
Discussion started by: mugga
5 Replies

6. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

7. Shell Programming and Scripting

Reading a file having junk characters in perl

Can anyone tell me how to read a file in perl having junk characters . I have only one junk character which is repeated many times in the file. While i'm reading and printing the file , it is displaying till the 1st occurence of that junk character and rest of the file is not being read. (1 Reply)
Discussion started by: k_surya
1 Replies

8. Shell Programming and Scripting

sed to remove last 2 characters of txt file

sed 's/^..//' file1.txt > file2.txt this will remove the first two characters of each line of a text file, what sed command will remove the last two characters? This is a similar post to my other....sry if I'm being lazy.... I need a file like this (same as last post) >cat file1.txt 10081551... (1 Reply)
Discussion started by: ajp7701
1 Replies

9. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

10. Shell Programming and Scripting

reading the txt file

hi to all im having some 20,000 files in that im having some contents say the tabulation of biophysics lab readings ... and i want read tat file and look into tat wether a number say -18.90 is there r not .. and if there print tat no wit file name beside thank you:D (1 Reply)
Discussion started by: maximas
1 Replies
Login or Register to Ask a Question