Reading a text file using bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a text file using bash
# 1  
Old 07-26-2013
Reading a text file using bash

I've a file in linux with following text:
Code:
;ip address    hostname     put-location      alt-put-location                tftpserver 
192.168.1.1    r01-lab1-net /mnt/nas1/fgbu/   /opt/fgbu/devicebackup          192.168.1.254

Now I want to read these values and assign them to particular variables for later user. e.g.,

Code:
ip_address=192.168.1.1
hostname=r01-lab1-net
put_location=/mnt/nas1/fgbu/

Now I am confused that what can be best way for it. for loop is not working as I'm not using array and also values start from 2nd line.
[MOD]Please use CODE tags; not HTML tags when posting sample input, output, and code.

Last edited by Don Cragun; 07-26-2013 at 04:47 PM.. Reason: HTML tags -> CODE tags
# 2  
Old 07-26-2013
You have lots of answers with examples of basic mechanisms how to parse a file in your other threads like https://www.unix.com/shell-programmin...lues-file.html
So what have you tried so far?
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 07-26-2013
Code:
ip_address=$(awk 'NR==2 {print $1}' file.html)
hostname=$(awk 'NR==2 {print $2}' file.html)
put_location=$(awk 'NR==2 {print $3}' file.html)

This User Gave Thanks to Jotne For This Post:
# 4  
Old 07-26-2013
@Jotne: thank, this fullfil my initail requirement but when i have multiple lines then it doesn't work.

@zaxxon: requirement in this case is different than previous one. Now I 've values in columns. not in rows.

To be more precise. I've file of this formate:

Code:
;ip address    hostname     put-location      alt-put-location                tftpserver
192.168.1.1    r01-lab1-net /mnt/nas1/fgbu/   /opt/fgbu/devicebackup          192.168.1.254
192.168.1.2    r01-lab2-net /mnt/nas2/fgbu/   /opt/fgbu/backup                192.168.1.222

Now what i want is that:
Code:
read one line. 
set its values against different variable, 
do some operations using those variables (ip=192.168.1.1, hostname=r01-lab1-net etc)
move to next line
repeat above mentioned steps

so far, following code gives me each line.

Code:
sed 1d filename | while read  line
do
     echo line
done

but then i m unable to get different values from this line and assign them to variables. please let me know if this can work or there should be some other way. thanks.

Last edited by Don Cragun; 07-26-2013 at 04:48 PM.. Reason: HTML tags -> CODE tags
# 5  
Old 07-26-2013
You're making it too hard. Try something like:
Code:
#!/bin/bash
line=0
while read ip_address hostname put_location alt_put_location tftpserver
do      ((line++))
        # Skip header line.
        if [ $line -eq 1 ]
        then    continue
        fi
        # Process data from other lines.
        printf "Input line #%d:\n" $line
        printf "\tip address: %s\n" "$ip_address"
        printf "\thostname: %s\n" "$hostname"
        printf "\tput-location: %s\n" "$put_location"
        printf "\talt-put-locaiont: %s\n" "$alt_put_location"
        printf "\ttftpserver: %s\n" "$tftpserver"
done < filename

With the input you specified in your last post, this script produces:
Code:
Input line #2:
	ip address: 192.168.1.1
	hostname: r01-lab1-net
	put-location: /mnt/nas1/fgbu/
	alt-put-locaiont: /opt/fgbu/devicebackup
	tftpserver: 192.168.1.254
Input line #3:
	ip address: 192.168.1.2
	hostname: r01-lab2-net
	put-location: /mnt/nas2/fgbu/
	alt-put-locaiont: /opt/fgbu/backup
	tftpserver: 192.168.1.222

You specified bash; this script will also work with any other POSIX conforming shell.
# 6  
Old 07-26-2013
@kashif.live:
We want to have users evolve and try to learn, not overly get their jobs done by others. That's why we ask for. That it isn't the exact solution in your other threads is quite clear.
Also start using code tags, not html tags when you post code, data or logs etc.
# 7  
Old 07-26-2013
ok guys. thanks alot. got answer:

Code:
sed 1d filename | while read -a words
do

ip_address="${words[0]}"
echo $ip_address
hostName="${words[1]}"
echo $hostName
pLocation="${words[2]}"
echo $pLocation
apLocation="${words[3]}"
echo $apLocation
tftpserver="${words[4]}"
echo $tftpserver


done

@zaxxon: I hope I've learned now. Smilie

Last edited by kashif.live; 07-26-2013 at 05:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

Reading from file bash command

Hello, I have a file in the following format id sample platform R1 R2 gene1 gene2 gene3 1 abc llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp asp 2 def llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp 3 ghi llumina ... (3 Replies)
Discussion started by: nans
3 Replies

3. UNIX for Beginners Questions & Answers

Reading a file from a different directory in a Bash script

Hi all, Given here under a section of a script I am using. SIMDIR="/home/Ins/forces" cd $SIMDIR for file in `ls *.forces` do basename=`echo $file | sed 's/\.*$//'` extname=`echo $file | sed 's/*\(*\)\.\(.*\)/\2\1/'` echo "Processing file: "$basename python convert.py... (4 Replies)
Discussion started by: Theo Score
4 Replies

4. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Reading a value from another text file

I am having a text file best = 100 genre = 75 group = 53 . . and so on I need to read those values (100,75,53,...) from my shell script. I have to do the same function for all the variables. So in my script i used for loop to do the same. for { a=best b=100 } Video tutorial on... (3 Replies)
Discussion started by: kishorekumar87
3 Replies

6. 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

7. Shell Programming and Scripting

Help in reading a cv file in bash

Hi All, I am trying to read a .csv file which has some 6 columns. Eg: samp.csv one, two, three, four six, seven, eight, nine I used the following code, for line in `cat samp.csv` do echo "$line" done It displays every comma seperated values in each line like, one,... (1 Reply)
Discussion started by: johnwilliams.sp
1 Replies

8. 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

9. Shell Programming and Scripting

Reading from Text file.....

Hi Im MZ.... please help me with my requirements..... Requirement: I have a text file named information.txt which contains information about Oracle Instances, I want to fetch data's from that text file and want to display an output using shell script. Explanation : i.e. when I execute that... (0 Replies)
Discussion started by: user__user3110
0 Replies

10. Shell Programming and Scripting

bash: reading filenames from file

Hi, I'm trying to write a script that reads filenames from a file and use these filenames in a loop. The filenames are all on one line and the problem is that these filenames have wildcards like * and braces like in them. Right now what I'm doing is something like this: echo "reading from... (0 Replies)
Discussion started by: warp17
0 Replies
Login or Register to Ask a Question