Mercury
Mercury

Reputation: 7988

How to print each couple of args using xargs from a list

I have this list

111
222
333
444
555

I want to print each line to have 2 coupled args, like this:

111 222
222 333
333 444
444 555
555

Is there a way to do this with xargs? or any single liner command (performance is important)

thanks

Upvotes: 0

Views: 288

Answers (3)

pmf
pmf

Reputation: 36058

You could also use paste with two inputs: One as the original file and one with the first line stripped off (here by using tail -n +2):

Using process substitution:

paste file.txt <(tail -n +2 file.txt)

Using a pipeline (as suggested by @pjh):

tail -n +2 file.txt | paste file.txt -

Both output

111 222
222 333
333 444
444 555
555 

Upvotes: 3

L&#233;a Gris
L&#233;a Gris

Reputation: 19545

With xargs and an inline shell script:

xargs -a list.txt sh -c 'while [ $# -gt 0 ]; do printf %s\ %s\\n "$1" "$2"; shift; done' _

Upvotes: 0

Renaud Pacalet
Renaud Pacalet

Reputation: 28995

awk seems more appropriate than xargs:

$ awk 'NR>1 {print prev, $0} {prev=$0} END {print prev}' file.txt
111 222
222 333
333 444
444 555
555

Upvotes: 2

Related Questions