Read from a file in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from a file in unix
# 1  
Old 10-15-2012
Read from a file in unix

Hi All
I have a file type such as
Code:
[TEST]
client=SSH
server=testing
user=abc
password=xyz
dir=home

[Run]
client=putty
server=running
user=pqr
password=sas
dir=main

[dump]
client=nothing
server=down
user=aaa
password=bbb
dir=lost

I should pass the two value two the script and it should give the result
example ./script Run user
It should give result pqr

Moderator's Comments:
Mod Comment Restored post after user deleted it. Banned for his trouble.

Last edited by parthmittal2007; 10-16-2012 at 04:09 AM..
# 2  
Old 10-15-2012
Using Klashxx command:-

Code:
#!/bin/ksh

param_1=$1
param_2=$2

cat input.txt | egrep -v "^#" | awk -v tag=$param_1 -v field=$param_2 'match($0, tag){t=1};match($0, field) && t{print $2;exit}' FS="="


Last edited by Yoda; 10-15-2012 at 04:08 PM..
# 3  
Old 10-15-2012
An awk parser:
Code:
awk -v tag="Run" -v field="user" 'match($0, tag){t=1};match($0, field) && t{print $2;exit}' FS="="  infile

# 4  
Old 10-15-2012
Comment Section

Last edited by parthmittal2007; 10-16-2012 at 04:09 AM..
# 5  
Old 10-15-2012
Just look for the complete string:
Code:
awk -v tag="Run" -v field="user" 'match($0, tag){t=1;next};$1 == field && t{print $2;exit};/^\[/ && t {print "Not found";exit}' FS="=" infile

# 6  
Old 10-16-2012
With some assumptions:
Code:
#!/usr/bin/ksh
awk -F'\n|=' '$1==label{
for(i=2;i<=NF;i+=2)
 if($i==attrib)
  print $(i+1)
}' label="[$1]" attrib="$2" RS= file

# 7  
Old 10-16-2012
Code:
awk -F'[]=[]' '$2==t{p=1} p && $1==f{print $2; exit}' t=Run f=user infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. Shell Programming and Scripting

File read from UNIX to Oracle

hi all, I have flat file in unix. By using sql loader i need to import the data from the flat file. I have created a control file, table with the below data structure and pasted the sample input file. I am not sure whether it is a right way to do it. can anyone provide the thought on this. ... (2 Replies)
Discussion started by: arun888
2 Replies

3. Shell Programming and Scripting

how to read only the first two values from a file in unix

Hi, I'm new to unix.. I need to read only the first two values from a file that contains many data....My record is like this FTG_ver_num=7.0 RUN_ID=2 $$tgt_envrnmt_cd=FTG $$WRK_FLW_CD=NPL I need oly the values of ETG_ver_num and RUN_ID. i.e) output to be 7.0 and 2 The code i used... (2 Replies)
Discussion started by: raghulshekar
2 Replies

4. UNIX for Advanced & Expert Users

How can i read a non text file in unix - ELF-64 executable object file - IA64

The binary file is ELF-64 executable object file - IA64. How i know that the source is Is there any comamnd in unix i can read these kind of files or use a thirty party software? Thanks for your help (8 Replies)
Discussion started by: alexcol
8 Replies

5. Shell Programming and Scripting

Read Ms-excel file in unix

Hi, I have the Ms Excel file(test.xls) in my UNIX box. I would like to read the excel file and create files for each column. Please find an example. My excel file like this data: Num Data 1 a1 2 b2 3 c3 4 d4 5 e5 6 f6 7 h7 My output: I want create 2 files(num.log and... (3 Replies)
Discussion started by: koti_rama
3 Replies

6. Shell Programming and Scripting

How to read from a file in unix

I have one file data.txt. This data.txt file contain "no of head=3" string. From a shell script I want to check whether data.txt file contains "no of head" string or not, if yes then want to fetch the value 3 and store it in a variable. Anyone plz help me. (4 Replies)
Discussion started by: Dip
4 Replies

7. UNIX for Dummies Questions & Answers

How to read a file in unix using do....done loop

Hi , can some give me idea about how to use do...done while loop in UNIX to read the contents of a file.. (2 Replies)
Discussion started by: sreenusola
2 Replies

8. Shell Programming and Scripting

How to read from a .dat file in Unix

Hi All, I have a .dat file named test.dat where I have stored some process IDs. Now I need to pick a process ID, one by one and then fire kill -9 for each of those. The logic should be: 1. open file <filename.dat> 2. read until last line of file 3. if process ID is found fire kill -9... (5 Replies)
Discussion started by: Sibasish
5 Replies

9. UNIX for Dummies Questions & Answers

How do u open a read only file in Unix?

How do u open a read only file in Unix? (1 Reply)
Discussion started by: JosephGerard
1 Replies

10. UNIX for Dummies Questions & Answers

file read + unix script

hi, how can i read line by line from a file using unix shalle script? Thanks and Regards Vivek.S (2 Replies)
Discussion started by: vivekshankar
2 Replies
Login or Register to Ask a Question