How to display the header of a matched row in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to display the header of a matched row in a file?
# 1  
Old 01-05-2015
How to display the header of a matched row in a file?

Hi,

So I am trying to print the first row(header) first column alongwith the matched value. But I am not sure how do I print the same, by matching a pattern located in the file


eg
File contents
Code:
Name Place 
Jim     NY
Jill      NJ
Cathy CA
Sam   TX
Daniel FL

And what I want is to print the first column first row(i.e header) for Place matching CA

and the matched value row

o/p is

Code:
Name
Cathy CA

So I know with awk
Code:
 awk /CA/ File

will give me just

Code:
Cathy CA

How do I get the first column title "Name"?
# 2  
Old 01-05-2015
Perhaps something like:
Code:
/usr/xpg4/bin/awk -v place="CA" '
FNR == 1 {
	print $1
	next
}
$2 == place' File

# 3  
Old 01-05-2015
Sidnow,

Are you looking for:
Code:
awk 'NR == 1 { print $1; } /CA/ { print; }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude the header row while splitting the file

Hi All, i have script like ... "TYPE_ID" "ID" "LIST_ID" "18" "52010" "1059" "18" "52010" "1059" "18" "52010" "1059" "18" "52010" "1059" i am using the below code it's not taking the header row. awk -F"\t" -v file=test1.txt -v file1=test2.txt ' { if(... (7 Replies)
Discussion started by: bmk
7 Replies

2. UNIX for Dummies Questions & Answers

File Row Line Count without Header Footer

Hi There! I am saving the file count of all files in a directory to an output file using: wc -l * > FileCount.txt I get: 114 G4SXORD 3 G4SXORH 0 G4SXORP 117 total But this count includes header and footer. I want to subtract 2 from the count and get ... (7 Replies)
Discussion started by: gagan8877
7 Replies

3. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

4. Shell Programming and Scripting

Test command:Duplicate Header row in Log File

I have a script that is inventorying (not sure if thats a word) my environment. It goes out and pulls Hostname OS, IP, and ENV (dev, prod, etc)..then spits all that to a logfile At the top of my script i have a check to see if the logfile exist. ] || touch $LOGFILE && echo "ENV" "\t"... (3 Replies)
Discussion started by: nitrobass24
3 Replies

5. Shell Programming and Scripting

Exclude the header row in the file to validate

Hi All, File contains header row.. we need to exclude the header row...no need to validate the first row in the file. Data in the file should take valid data(two columns)..we need to exclude the more than two columns in the file except the first line. email|firstname a|123|100 b|345... (4 Replies)
Discussion started by: bmk
4 Replies

6. Shell Programming and Scripting

print the whole row in awk based on matched pattern

Hi, I need some help on how to print the whole data for unmatched pattern. i have 2 different files that need to be checked and print out the unmatched patterns into a new file. My sample data as follows:- File1.txt Id Num Activity Class Type 309 1.1 ... (5 Replies)
Discussion started by: redse171
5 Replies

7. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

8. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

9. Shell Programming and Scripting

Get header and its matched value in perl?

Hi, I have the file contents: Start,End,Req,Resp 12:39,12:40,4,5 The sting to be matched is: Req and Resp. parsefile("Req,Resp"); Here is the code. sub parsefile ($) { $header=shift; (2 Replies)
Discussion started by: vanitham
2 Replies

10. Shell Programming and Scripting

how can I bcp out a table into a text file including the header row in the text file

Hi All, I need to BCP out a table into a text file along with the table headers. Normal BCP out command only bulk copies the data, and not the headers. I am using the following command: bcp database1..table1 out file1.dat -c -t\| -b1000 -A8192 -Uuser -Ppassword -efile.dat.err Regards,... (0 Replies)
Discussion started by: shilpa_acc
0 Replies
Login or Register to Ask a Question