Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `awk` to grep certain columns like these?

Tags:

bash

awk

So basically I have some text like this:

[ 4] .init             PROGBITS        080481c0 0001c0 00002e 00  AX  0   0  4
[ 5] .plt              PROGBITS        080481f0 0001f0 000110 00  AX  0   0 16
[ 6] .text             PROGBITS        08048300 000300 07c95c 00  AX  0   0 16
[ 7] __libc_thread_fre PROGBITS        080c4c60 07cc60 000076 00  AX  0   0 16
[ 8] __libc_freeres_fn PROGBITS        080c4ce0 07cce0 000b2f 00  AX  0   0 16
[ 9] .fini             PROGBITS        080c5810 07d810 00001a 00  AX  0   0  4
[10] .rodata           PROGBITS        080c5840 07d840 019774 00   A  0   0 32
[11] __libc_thread_sub PROGBITS        080defb4 096fb4 000004 00   A  0   0  4
[12] __libc_subfreeres PROGBITS        080defb8 096fb8 00002c 00   A  0   0  4
[13] __libc_atexit     PROGBITS        080defe4 096fe4 000004 00   A  0   0  4

What I am trying to get is this:

.init                    080481c0 0001c0 00002e 
.plt                     080481f0 0001f0 000110 
.text                    08048300 000300 07c95c 
__libc_thread_fre        080c4c60 07cc60 000076 
__libc_freeres_fn        080c4ce0 07cce0 000b2f  
.fini                    080c5810 07d810 00001a 
.rodata                  080c5840 07d840 019774 
__libc_thread_sub        080defb4 096fb4 000004 
__libc_subfreeres        080defb8 096fb8 00002c  
__libc_atexit            080defe4 096fe4 000004 

I tried something like this:

 awk '/PROGBITS/ {print $2,$4,$5,$6} '

but the problem is that, there is a space inside [ 4] .. , which means in the 4-9 line, I have to use

awk '/PROGBITS/ {print $3,$5,$6,$7} '

Is there anyway to use a single command while getting all the columns I want..?

like image 830
lllllllllllll Avatar asked Dec 06 '22 01:12

lllllllllllll


1 Answers

With gnu awk you have this elegant way to handle text with fixed width on fields. It will also keep the formatting.

awk -v FIELDWIDTHS="5 18 16 8 7 8" '{print $2,$4,$5,$6}' file
.init              080481c0  0001c0  00002e
.plt               080481f0  0001f0  000110
.text              08048300  000300  07c95c
__libc_thread_fre  080c4c60  07cc60  000076
__libc_freeres_fn  080c4ce0  07cce0  000b2f
.fini              080c5810  07d810  00001a
.rodata            080c5840  07d840  019774
__libc_thread_sub  080defb4  096fb4  000004
__libc_subfreeres  080defb8  096fb8  00002c
__libc_atexit      080defe4  096fe4  000004
like image 142
Jotne Avatar answered Dec 30 '22 17:12

Jotne