Reading the text file for particular format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading the text file for particular format
# 1  
Old 06-12-2013
Reading the text file for particular format

Hi All,

Need your help!!

I have particular host file with below format:

Code:
172.34.45.67      Host1 Host2
134.45.56.67      Host3 Host4 Host5

I need shell script snippet which read this file and change the format of the file to the below format

Code:
172.34.45.67      Host1
172.34.45.67      Host2
134.45.56.67      Host3 
134.45.56.67      Host4
134.45.56.67      Host5


Thanks in advance

Last edited by Scrutinizer; 06-12-2013 at 01:44 PM.. Reason: code tags
# 2  
Old 06-12-2013
Code:
awk '{for(i=2;i<=NF;i++)print $1,$i}' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-13-2013
Thanks JODA for the prompt reply, It is working and priniting on the console. I want to change the same thing in the file. It is not changing that thing in the input file.

And one more thing, if am passing "file" alone at the end of the awk file itis throwing error and than I pass it as $file in the awk command
Code:
awk '{for(i=2;i<=NF;i++)print $1,$i}' $file

it is working and printing on the console but not changing in the file

Could you please suggest for this solution also?

Thansk in advance.

Last edited by Scott; 06-13-2013 at 08:49 AM.. Reason: Code tags
# 4  
Old 06-13-2013
Have you stored file name in variable file?

Awk does not change the input file. You have to redirect the output to another file and rename it
Code:
awk '{for(i=2;i<=NF;i++)print $1,$i}' $file > temp
mv temp $file

This User Gave Thanks to anbu23 For This Post:
# 5  
Old 06-13-2013
Thanks Anbu

It is working for me thanks a lot.

---------- Post updated at 06:56 AM ---------- Previous update was at 06:37 AM ----------

Hi One more question:- Need a best solution fit to read the line by line and store each value in the variables and use that variables to ping thee ip and host name for e.g:-
Code:
172.34.45.67      Host1
172.34.45.67      Host2
134.45.56.67      Host3 
134.45.56.67      Host4
134.45.56.67      Host5

Storing each ip in one $ip variable and host name into $host
Implemented below solution :-

Code:
counter=0
typeset -a Server
while IFS=" " read name ip; do
Server[counter].name=$name
Server[counter].ip=$ip

any straight forward solution for this?

Last edited by Scott; 06-13-2013 at 09:00 AM.. Reason: Code tags, please...
# 6  
Old 06-13-2013
If you simply want to ping the IP & host name. Then no need to store them in array, just read and ping:
Code:
while read ip host
do
        # ping $ip
        # ping $host
done < file

But if you want to use these values later by storing them in an array:
Code:
typeset -a HOST
typeset -a IP
typeset -i idx

while read ip host
do
        (( idx++ ))
        HOST[$idx]="$host"
        IP[$idx]="$ip"
done < file

This User Gave Thanks to Yoda For This Post:
# 7  
Old 06-13-2013
Thanks Yoda,

Am storing that IP and host for future purpose to use it to compare them.
I applied that above mentioned logic but it is giving me error
Code:
 
"[: -lt: unary operator expected"

code snippet of my script
Code:
 
while read ip host
do
(( idx++ ))
HOST[$idx]="$host"
IP[$idx]="$ip"
if [ $(ping -q -c 1 -t 1 "$host" |grep PING | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}') = "$ip" ]

At below line it is giving me above error

Code:
 
if [ $(ping -q -c 1 -t 1 "$host" |grep PING | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}') = "$ip" ]

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

3. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

4. Programming

reading a text file in c++

hello all , im trying to read a text file and display its contents. While i got the code running and the output was displayed perfectly for sometime , i started getting Abort(core dump) error . Am i missing something here ? im using HP-UX. #include <iostream.h> #include <fstream.h> #include... (1 Reply)
Discussion started by: vishy_85
1 Replies

5. Programming

Reading a binary file in text or ASCII format

Hi All, Please suggest me how to read a binary file in text or ASCII format. thanks Nagendra (3 Replies)
Discussion started by: Nagendra
3 Replies

6. Shell Programming and Scripting

Help with Reading Text from file

Hello, I am having a tough time reading data from an input text file. The input file has lines of data which is comma seperated. Each line represents a record/row, but unfortunately the data in the individual column/fields have spaces and `cat filename`is not returning entire line ( to read... (4 Replies)
Discussion started by: yajaykumar
4 Replies

7. UNIX for Dummies Questions & Answers

Help with reading text file

How can i have a while loop as follows while read inputline do <task> done < name_list and also store the values (delimited) on each line to temp variables so as to print them on screen as follows while read inputline do set name | cut -d "," -f1 name_list # #i know this is not... (1 Reply)
Discussion started by: bilal05
1 Replies

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

9. Shell Programming and Scripting

Reading text from a file

Guys, I am trying to read text from a file, into arrays. The format of the file is: @DATABASE femotest @PACKAGE_SPECS /usr/home/oracle92/sosa/scripts/test.pks /usr/home/oracle92/sosa/scripts/rep.pks @PACKAGE_BODIES ... (12 Replies)
Discussion started by: LiquidChild
12 Replies

10. Shell Programming and Scripting

reading text file

I have a text file with 50 munbers. I wanna read these numbers and append "@yahoo.com" and send emails to them using shell scripting.......... How do i read the inetegres from the text file. (1 Reply)
Discussion started by: jaan
1 Replies
Login or Register to Ask a Question