awk Selective printing of fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Selective printing of fields
# 1  
Old 04-27-2017
RedHat awk Selective printing of fields

Hi Gurus,

I have following input file. I tried multiple awk combinations to print selected columns without success.

Code:
HEX ID    Name      ver  FLRGT Start Time               Total      Shared         End Date
----- --------   --- ------ ------------------------   -------------- ------- ------------------------ 
0006D cad abcName 0  .X... Fri Apr 21 17:56:07 2017      10327       1062                           NA
      opp klmName 3  ..... Mon Apr 17 20:15:19 2017      10364         41     Wed May  3 19:58:38 2017
      yu xyzName  49 ..... Wed Apr 12 19:56:49 2017      13717       3394                           NA
0006E cad abcName 1  .X... Fri Apr 21 17:56:07 2017      10491       1061                           NA
      ret qqqName 2  ..... Mon Apr 17 20:15:19 2017      10531         39     Wed May  3 19:58:38 2017
      ghy qqqName 4  ..... Wed Apr 12 19:56:49 2017      13882       3390                           NA
0006F mnb qqqName 32 .X... Fri Apr 21 17:56:07 2017      10385       1075                           NA
      opp klmName 7  ..... Mon Apr 17 20:15:19 2017      10420         43     Wed May  3 19:58:38 2017
      tyu qqqName 5  ..... Wed Apr 12 19:56:49 2017      13773       3396                           NA
0013F qdy qqqName 12 ..X.. Thu Feb  9 22:39:19 2017     724858     724858                           NA
                                                                           ---------- ----------
                                                                            156125212   39586208

I am expecting output as follows: Would appreciate your help.

Code:
 Name      ver  FLRGT Start Time                End Date																			
--------   --- ------ ------------------------ ------------------------ 
cad abcName 0  .X... Fri Apr 21 17:56:07 2017                        NA
opp klmName 3  ..... Mon Apr 17 20:15:19 2017  Wed May  3 19:58:38 2017
yu xyzName  49 ..... Wed Apr 12 19:56:49 2017                        NA
cad abcName 1  .X... Fri Apr 21 17:56:07 2017                        NA
ret qqqName 2  ..... Mon Apr 17 20:15:19 2017  Wed May  3 19:58:38 2017
ghy qqqName 4  ..... Wed Apr 12 19:56:49 2017                        NA
mnb qqqName 32 .X... Fri Apr 21 17:56:07 2017                        NA
opp klmName 7  ..... Mon Apr 17 20:15:19 2017  Wed May  3 19:58:38 2017
tyu qqqName 5  ..... Wed Apr 12 19:56:49 2017                        NA
qdy qqqName 12 ..X.. Thu Feb  9 22:39:19 2017                        NA

Thanks in advance.
# 2  
Old 04-27-2017
Please show your attempts "without success" and the failure modes and reasons.
# 3  
Old 04-27-2017
Hi RudiC,

Please my attempts below

1st attempt:
Code:
cat filename99 |awk '{
if (NF > 13)
print $0 |cut -f 1 -d '
else
print $0
}'

I get following o/p
Code:
HEX ID    Name      ver  FLRGT Start Time               Total      Shared         End Date
----- --------   --- ------ ------------------------   -------------- ------- ------------------------
0006D cad abcName 0  .X... Fri Apr 21 17:56:07 2017      10327       1062                           NA
      yu xyzName  49 ..... Wed Apr 12 19:56:49 2017      13717       3394                           NA
0006E cad abcName 1  .X... Fri Apr 21 17:56:07 2017      10491       1061                           NA
      ghy qqqName 4  ..... Wed Apr 12 19:56:49 2017      13882       3390                           NA
0006F mnb qqqName 32 .X... Fri Apr 21 17:56:07 2017      10385       1075                           NA
      tyu qqqName 5  ..... Wed Apr 12 19:56:49 2017      13773       3396                           NA
0013F qdy qqqName 12 ..X.. Thu Feb  9 22:39:19 2017     724858     724858                           NA
                                                                           ---------- ----------
                                                                            156125212   39586208

# 4  
Old 04-27-2017
Difficult to believe you get that output ...
How about
Code:
cut -c7-51,74- file
    Name      ver  FLRGT Start Time                   End Date
--------   --- ------ --------------------------- ------------------------ 
cad abcName 0  .X... Fri Apr 21 17:56:07 2017                           NA
opp klmName 3  ..... Mon Apr 17 20:15:19 2017     Wed May  3 19:58:38 2017
yu xyzName  49 ..... Wed Apr 12 19:56:49 2017                           NA
cad abcName 1  .X... Fri Apr 21 17:56:07 2017                           NA
ret qqqName 2  ..... Mon Apr 17 20:15:19 2017     Wed May  3 19:58:38 2017
ghy qqqName 4  ..... Wed Apr 12 19:56:49 2017                           NA
mnb qqqName 32 .X... Fri Apr 21 17:56:07 2017                           NA
opp klmName 7  ..... Mon Apr 17 20:15:19 2017     Wed May  3 19:58:38 2017
tyu qqqName 5  ..... Wed Apr 12 19:56:49 2017                           NA
qdy qqqName 12 ..X.. Thu Feb  9 22:39:19 2017                           NA
                                               ---------- ----------
                                                156125212   39586208

This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-30-2017
Thanks for your help RudiC
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

3. Shell Programming and Scripting

Selective printing

I have the following contents in a file ---- CRITICAL: altered for /usr/bin/bin1 ---- OK: /usr/sbin/bin2 result fine ---- OK: /usr/sbin/bin3 result fine ---- CRITICAL: altered for /usr/bin/bin4 ---- OK: /usr/bin/bin5 result fine ---- OK: /usr/bin/bin6 result fine ---- CRITICAL: altered for... (9 Replies)
Discussion started by: anil510
9 Replies

4. Shell Programming and Scripting

selective printing

hi all from below text "abcd,SYS_12345,xyz,PQR, ," I want to print only "abcd,SYS,xyz,PQR, ," i.e. taking only first three 3 chars from 2 string of comma separated file thanks (4 Replies)
Discussion started by: JoeColeEPL9
4 Replies

5. Shell Programming and Scripting

Awk selective printing

Hi, i need help to print number from different field INPUT: Student1 10 20 Student2 30 40 Student3 50 60 Student4 70 80 Desired Output: 1 20-30 2 40-50 3 60-70 Thank you! (5 Replies)
Discussion started by: saint2006
5 Replies

6. Shell Programming and Scripting

sed selective printing

Hi, I have an xml file having serveral smiliar lines as below <INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="19" PHYSICALLENGTH ="15"... (3 Replies)
Discussion started by: dips_ag
3 Replies

7. Shell Programming and Scripting

need help with awk in printing the fields in a file

hi all i get a file from a server and i do not know how many fields that file will contain. i need to cut from the second column of the file to the last, irrespective of how many fields are there. is there any way to make the awk command dynamic for fetching from the second to the last... (4 Replies)
Discussion started by: sais
4 Replies

8. Shell Programming and Scripting

selective printing awk

Hi there my file looks like this 1 a b c d e f 2 a b b c d e f f g h e t t 3 a c b d e f 4 a b c i want to print the line which has the fields containing ONLY a b c, in this case the line 4. How can i awk it !!!? Many Thanks in advance! (8 Replies)
Discussion started by: saint2006
8 Replies

9. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies

10. Shell Programming and Scripting

printing select fields in awk

Hi, I want to print certain fields from my data file depending on certain conditions. Somebody pls let me know how to send it to awk. The command below is the one which I want to use in a shell script and this prints fine cat ./datafile.dat | grep -i $SEARCH_STR | awk -F: '{ print $1 $2 $3... (5 Replies)
Discussion started by: maverix
5 Replies
Login or Register to Ask a Question