awk - how to print specific field if a string is matched


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - how to print specific field if a string is matched
# 1  
Old 03-12-2015
awk - how to print specific field if a string is matched

hi gurus,

I would like to be able to use awk to process 1 file as such:

Code:
abc 1 2 3 4 5 6 7 8 9 10
flags 1 2 4 
flags 1 2 5
abc 2 3 4 5 6 7 8 9 10 11
flags 1 2 3
abc 4 5 6 7 8 9 6 7 78 89 
flags 1 2 3 
flags 1 2 4
flags 1 2 3 4

I would like to be able to print field 1 and 5 when the line starts with abc and also print field 2 3 on the lines that start with flags
Any tips?

Thanks
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 03-12-2015 at 03:27 PM.. Reason: Add CODE tags.
# 2  
Old 03-12-2015
Code:
awk '{if($0~/^abc/){print $1,$2,$3,$4,$5}else if($0~/^flags/){print $2,$3}}' FileName.txt

# 3  
Old 03-12-2015
Perfect, thank you so much....
# 4  
Old 03-12-2015
Try also
Code:
awk   '/^abc/      {print $1,$5}
       /^flags/    {print $2,$3}
      ' file

# 5  
Old 03-12-2015
Thanks Rudi, that works great too Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to print string if tag is specific value

In the below awk I am trying to print expName only if another tag planExecuted is true. In addition to the expName I am also printing planShortID. For some reason the word experiment gets printed so I remove it with sed. I have attached the complete index.html as well as included a sample of it... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

awk - Print whole string ending with a Tab if key matched

Hi , I am looking to print the whole string from file2.txt but it is only printing 77 but not the whole matched string from File2.txt Any help is appreciated. Thanks, Script awk ' BEGIN { OFS="\t" out = "a.txt"} NR==FNR && NF {a=$0; next} function print_65_11() { if... (11 Replies)
Discussion started by: High-T
11 Replies

4. UNIX for Dummies Questions & Answers

Commenting a line matched with a specific string in a file

Hi, I would like to comment a line that matched a string "sreenivas" in a file without opening it. Thanks in advance. Regards, Sreenivas (3 Replies)
Discussion started by: raosr020
3 Replies

5. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

6. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

7. Shell Programming and Scripting

AWK Print Line If Specific Character Is Matched

Hello, I have a file as such: FFFFFFF6C000000 225280 225240 - - rwxs- FFFFFFFF79C00000 3240 3240 - - rwxs- FFFFFFFF7A000000 4096 4096 - - rwxs- FFFFFFFF7A400000 64 64 ... (3 Replies)
Discussion started by: PointyWombat
3 Replies

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies

10. Shell Programming and Scripting

print only matched string instead lines in grep

frnd, Is there any way to print only the string that matched the pattern instead printing the whole line? thanks in advance. (3 Replies)
Discussion started by: clx
3 Replies
Login or Register to Ask a Question