Problem in reading a file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in reading a file content
# 1  
Old 01-15-2010
Problem in reading a file content

Hi,
I am reading a file line by line using read line function of while loop. Each line contains 4 fields. I want to take these 4 values in 4 variables in each iteration so that i can use them in my script. The issue here is that my awk command is returning awkward results -
Here is a sample line -
ABCD|*[!Z!gz]|1|7
The command i am using - dir=$(echo $line | awk '{ print $1}') -- this is fine
But when i run the command - dir=$(echo $line | awk '{ print $3}') -- it doesn't give me the value 1...May be due to special characters in the data (*[!Z!gz]).
Can anyone please suggest how to handle it.
Regards,
Manoj
# 2  
Old 01-15-2010
One way (without awk):

Code:
$ cat Test
while IFS="|" read A B C D; do
  echo "$A"
  echo "$B"
  echo "$C"
  echo "$D"
done < file1

Code:
$ cat file1
ABCD|*[!Z!gz]|1|7

Code:
$ ./Test
ABCD
*[!Z!gz]
1
7

# 3  
Old 01-15-2010
Or else tell awk that "|" is the separator with:
Code:
awk -F"|" '{ print $3 }'

# 4  
Old 01-15-2010
Quote:
Originally Posted by TonyFullerMalv
Or else tell awk that "|" is the separator with:
Code:
awk -F"|" '{ print $3 }'

I'm not very sure that goes half way to answering the question, but OK Smilie
# 5  
Old 01-15-2010
Thank you! Scottn. The 1st one works. Actually, i was using awk with the seperator but its not giving the correct output.
# 6  
Old 01-15-2010
There's nothing wrong with an awk solution... I've written a million of them... and some of them work!

But when you're talking about "iterating" and "putting the values into variables", sometimes the best solution is the simplest one.
# 7  
Old 01-15-2010
I was anwering the how to extract the 3rd element:
Code:
$ cat garman.txt
ABCD|*[!Z!gz]|1|7
$ awk -F"|" '{ print $3 }' garman.txt
1
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Reading flat file content

is there any unix tools that can read the text files like through SQL queries? (ie in Hadoop, Impala DB support flat file query) (1 Reply)
Discussion started by: omykarshan
1 Replies

2. Shell Programming and Scripting

Trouble reading content of file from a variable

Hi , i have a parameter which has path of a file. Now i need to have another parameter with the content of that file. I tried the belwo script , can any one please help. I dont want to use cat command to read. Can we do it with out using cat command. while read line do... (9 Replies)
Discussion started by: Ravindra Swan
9 Replies

3. Shell Programming and Scripting

problem with file content extraction

I need to extract some content of a file. Example file abc vi abc ooooooooo bbbbbbbbb vvv 1234 5 vvv 6789 3 xxxxxxxxxx xxxxxxxxxx i want to extract only the following content from file abc and store in another file say temp. 1234 5 6789 3 what should be my approach? (2 Replies)
Discussion started by: priya_ag04
2 Replies

4. Shell Programming and Scripting

Reading files under a folder and formatting content of each file

I have 'n' number of files in a folder .each file in the folder "myfolder" is having the content like. COLNAME ------------ AAAAAA BBBBBB CCCCCC DDDDDD ... ... ... ZZZZZZ 26 recrod(s) selected. My request is by reading each file in "myfolder" and format each file such a way... (18 Replies)
Discussion started by: rocking77
18 Replies

5. Shell Programming and Scripting

Problem in reading file (bash)

i get a name from user first name : last name, in this format. Now i am saving this to a file. what i want is, I do not want to save any name if I already have one entry o that same name..what should i do for example user give robert fernandez this will save in file as robert:fernandez. if... (5 Replies)
Discussion started by: Learnerabc
5 Replies

6. Shell Programming and Scripting

Problem in reading a file

Hi Guys, I am having a file which does not have any name when i do a ls -l -rw-r--r-- 1 dctrdat1 dctrdata 35 Feb 09 08:04 -rw-r--r-- 1 dctrdat1 dctrdata 11961 Feb 08 06:40 DAI_data.txt Now i want to see what is inside that file. Can you please let me know how to read... (9 Replies)
Discussion started by: mac4rfree
9 Replies

7. Shell Programming and Scripting

Problem with reading from a properties file

Hi, i have a properties file a.prop where entry is like PROCESS_IDX=0 Now in my shell schript i am doing like this. #!/bin/sh . a.prop .............. -....................... while read line do # tokenize the string by ",". var=(`echo $line | tr ',' ' '`) echo $PROCESS_IDX -->... (6 Replies)
Discussion started by: sailaja_80
6 Replies

8. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

Hi Gurus, I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file. eg. if file a.txt exists then send a mail to a@a.com simillary for b.txt,c.txt etc. Thanks for your help, Nimu (6 Replies)
Discussion started by: nimu1979
6 Replies

9. Shell Programming and Scripting

problem in reading a file

i need to read record by record i use script #!/bin/ksh for i in 'cat filename' do echo $1 done but i dont get expected result i just get filename echoed on screen (4 Replies)
Discussion started by: er_zeeshan05
4 Replies

10. Shell Programming and Scripting

help me ...problem in reading a file

hi, while reading a file line by line # name of the script is scriptrd while read line do echo $line done while executing bash$ ./scriptrd if i give the input as * the output is like it displays the contents of the current directory i jus wanted it to print as * (6 Replies)
Discussion started by: brkavi_in
6 Replies
Login or Register to Ask a Question