Retrieving particular row from a dat file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Retrieving particular row from a dat file
# 1  
Old 03-12-2009
Retrieving particular row from a dat file

Hi..

I have a dat file containing both column names and data. Now I want to get only Particular row along with the column names.
My dat fiel is as below

EmpName Dept Salary
Shally Admin 20000
Swati HR 15000
Deepali IT 45000
Preetika EEE 60000

Now I want to display only row 3rd along with column name using awk command.

I tried as below to get the column name ;
awk 'NR==1 {print $0}' Employee.dat
This retrives the column name... now how to get particular row.

Smilie
# 2  
Old 03-12-2009
try something like this
Code:
awk 'NR==1{print}NR==3{print}' filename

# 3  
Old 03-12-2009
Bug

Smilie Thanks alot. Its working fine with the current scenario...Smilie

I have one more query. If we know exactly which row to retrieve then this will work fine. Suppose the given dat file contains thousands of records, I just want to retrieve all the rows whose salary is greater tha 15000. How can v acheive that ? In other words can v use expressions(logical,arthmetic, regular...) to retireve the particular rows along with the column name..
# 4  
Old 03-12-2009
Use awk...

Code:
$ cat emp.dat

EmpName Dept Salary
Shally Admin 20000
Swati HR 15000
Deepali IT 45000
Preetika EEE 60000
john     ECE  5000

$ awk '{if ($3 > 15000) {print $0}}' emp.dat

EmpName Dept Salary
Shally Admin 20000
Deepali IT 45000
Preetika EEE 60000

# 5  
Old 03-13-2009
Quote:
Originally Posted by Mayuri P R
Smilie Thanks alot. Its working fine with the current scenario...Smilie

I have one more query. If we know exactly which row to retrieve then this will work fine. Suppose the given dat file contains thousands of records, I just want to retrieve all the rows whose salary is greater tha 15000. How can v acheive that ? In other words can v use expressions(logical,arthmetic, regular...) to retireve the particular rows along with the column name..
yes you can
Code:
 
awk 'BEGIN{NR==1{print}$3>15000{print}' filename

# 6  
Old 03-13-2009
Quote:
Originally Posted by vidyadhar85
yes you can
Code:
 
awk 'BEGIN{NR==1{print}$3>15000{print}' filename

Are you sure this will work????
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use 'ls' command to list files like *.dat, not *.*.dat?

How to use 'ls' command to list files like *.dat, not *.*.dat (5 Replies)
Discussion started by: pmcginni777
5 Replies

2. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

3. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

4. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

5. Shell Programming and Scripting

Retrieving File name

Hi All.. I have a Filename as FAB1_600015_CONRAD.A0_7XYZ12345.000_LT-SWET.01_LTPA25L_20110622-161429_07_WFR12345_20110622-161429_20110712-125228.data.dis I want to get the result as... (5 Replies)
Discussion started by: asheshrocky
5 Replies

6. Shell Programming and Scripting

Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem. the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example. I have different html files and each of them have a consecutive lines group inside that i want to extract. example: ... (8 Replies)
Discussion started by: sbobotex
8 Replies

7. Shell Programming and Scripting

Append Second Row to First Row @ end in a file

Hi I want to append line 2n to 2n-1 line where n=1...LastLine in my file. eg: Actual File: Hello Everyone Welcome TO Unix I need Your help Required File: HelloEveryone WelcomeTO Unix I needYour help (5 Replies)
Discussion started by: dashing201
5 Replies

8. UNIX for Dummies Questions & Answers

Retrieving PID from a file

Hello I need to retrieve the content of a file in the shell script file(.sh file). I store the Process ID of the a process in file.Only the PID is available in that file. Inside the shell script i want to retireve the content(PID) and need to check for the existence of the Process.Basically... (5 Replies)
Discussion started by: appleforme1415
5 Replies

9. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies

10. UNIX for Advanced & Expert Users

retrieving a deleted file

hi!, is there any way to retrieve a file that I have deleted few minutes back?? I am using Solaris- 5.6.. :rolleyes: (2 Replies)
Discussion started by: jyotipg
2 Replies
Login or Register to Ask a Question