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

So basically I have a 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'm trying to get is the following:

.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 [ 4]is there is a space inside , which means that in the line 4-9I have to use

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

Can I use the single command when getting all the columns that I want?

+4
source share
8 answers

You can also try:

awk '/PROGBITS/{print $(NF-9),$(NF-7),$(NF-6),$(NF-5)}' file

If you want to keep something readable by choosing the width of the columns:

awk '/PROGBITS/{printf "%-18s %-10s %-10s %-10s\n", $(NF-9),$(NF-7),$(NF-6),$(NF-5)}' file

It is also possible that your file has \t(tabs) as field separators; if so, you can try:

awk -F"\t" '{print $2,$4,$5,$6}' file

Hope this helps.

+2

gnu awk . .

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
+2

, cut :

cut -c 6-22 -c 32-62 file
+2

perl:

perl -lne '/\] \K(.*)PROGBITS\s+(\w+)\s+(\w+)\s+(\w+)/ && print "$1 $2 $3 $4" '

:

perl -lne '/\] \K(.*)PROGBITS\s+(\w+)\s+(\w+)\s+(\w+)/ && print "$1 $2 $3 $4" ' 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
+1

-F:

awk -F'^\\[ *[0-9]+\\] | +' '{printf "%-24s %-8s %-6s %-6s\n", $2, $4, $5, $6}' file

, , / .

+1

:

awk '/PROGBITS/ {if (NF==12) print $3,$5,$6,$7; else print $2,$4,$5,$6}'
0

[:

sed 's_\[\s_[_'

,

echo '[ 1]' | sed 's_\[\s_[_'

[1].

0

A sed (GNU sed FreeBSD/OS X sed) - @Tiago Perl:

sed -E 's/^.*\] (.*)PROGBITS( +[^ ]+)( +[^ ]+)( +[^ ]+).*$/\1 \2 \3 \4/' file
  • , , ((...)), ( ), - \1 , \2 ,...

, POSIX- , :

sed 's/^.*\] \(.*\)PROGBITS\( \{1,\}[^ ]\{1,\}\)\( \{1,\}[^ ]\{1,\}\)\( \{1,\}[^ ]\{1,\}\).*$/\1 \2 \3 \4/' file
0

Source: https://habr.com/ru/post/1547426/


All Articles