Manusha
Manusha

Reputation: 11

Print 1st word of 1st column and 2nd word of 2nd column in a single line

I would like to print first word of first column and second word of second column in to a single column Example:

ABC 123
BCD 987

output:

ABC 987

Upvotes: 0

Views: 850

Answers (6)

potong
potong

Reputation: 58371

This might work for you (GNU sed):

sed 'N;s/ .* / /' file

Append the second line to the first and replace everything between the first two spaces with another space.

Upvotes: 0

Rahul Verma
Rahul Verma

Reputation: 3089

using awk

$ awk '{printf $NR (NR==2?ORS:" ") }' file
ABC 987

Upvotes: 0

ctac_
ctac_

Reputation: 2471

With sed

sed 'N;s/\([^ ]*\)\(.*\n[^ ]*\)\(.*\)/\1\3/' infile

Or

sed -E 'N;s/([^ ]+)(.*\n[^ ]+)(.*)/\1\3/' infile

Upvotes: 0

karakfa
karakfa

Reputation: 67467

this will extract the diagonal for arbitrary size table

$ awk '{print $NR}' file | paste -sd' ' -

Upvotes: 1

Akshay Hegde
Akshay Hegde

Reputation: 16997

 awk 'FNR==1{printf $1 OFS;next}FNR==2{print $2;exit}' infile

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133458

Sweet and simple awk may help you in same.

awk 'FNR==1{val=$1;next} FNR==2{print val,$2}'  Input_file

Upvotes: 1

Related Questions