Gawk Line removal, Splitter is :

bash grep gawk

87 просмотра

3 ответа

Is it possible to move certain columns from one .txt file into another .txt file?

I have a .txt that contains:

USERID:ORDER#:IP:PHONE:ADDRESS:POSTCODE
USERID:ORDER#:IP:PHONE:ADDRESS:POSTCODE

With gawk I want to extract ADDRESS & POSTCODE columns into another .txt, so for this given file the output should be:

ADDRESS1:POSTCODE1
ADDRESS2:POSTCODE2

etc.

Автор: wolf pkrs Источник Размещён: 08.11.2019 11:24

Ответы (3)


0 плюса

Решение

This is a classic AWK transform. You want to use "-F :" to specify that the input is delimited by ":" and print a new ":" on output:

awk -F: '{ print $5 ":" $6 }' <input.txt >output.txt
Автор: TomOnTime Размещён: 20.08.2016 02:28

0 плюса

Try that:

awk -F: '{printf "%s:%s ",$5,$6}' ex.txt

input is

USERID:ORDER#:IP:PHONE:ADDRESS1:POSTCODE1
USERID:ORDER#:IP:PHONE:ADDRESS2:POSTCODE2

output is (on one line if I understand correctly)

ADDRESS1:POSTCODE1 ADDRESS2:POSTCODE2

only default is that it ends with a trailing space and does not end with a newline.

Which can be fixed with the slightly more complex (but still readable):

  awk -F: 'BEGIN {z=0;} {if (z==1) { printf " "; } ; z=1; printf "%s:%s",$5,$6} END{printf"\n"}' ex.txt
Автор: Jean-François Fabre Размещён: 20.08.2016 02:06

0 плюса

awk -F: 'NR==1 {print $5"1:"$6"1"};NR==2 {print $5"2:"$6"2"}' file
ADDRESS1:POSTCODE1
ADDRESS2:POSTCODE2
Автор: Claes Wikner Размещён: 21.08.2016 05:35
Вопросы из категории :
32x32