Output numbers from a file in a different order

4

1

I have a file that contains:

1 2 3 4
1 3 5 4 8
3 2 1 

Each line has a different number of digits. And there are 1000 more like this.

I want the output like this:

1 2
2 3
3 4
4 0
1 3
3 5
5 4
4 8
8 0
3 2
2 1
1 0

edit made to this post!!

i mean if this is last digit in line put that digit and then zero besides it.

for example if digit is last in that line we put in output new line that digit and zero

4 0 and others:see output sample tnx

in above example and

How can I do that, in perl awk or bash

i use this but its not working :((

awk '{
for (i=1; i<NF; i++)
{
if ( $(i+1) == "")
print $i, "0"
else
print $i, $(i+1)
}
}' UniqASinline> inTestAst

the outpute file of Mr.glenn jackman way:

for this imput:

3549 
3549 10026 
3549 10026 10010 
3549 10026  
awk '{for (i=1; i<=NF; i++) printf("%d %d\n", $i, $(i+1))}' filename

3549 
3549 10026
10026 
3549 10026
10026 10010
10010 
3549 10026
10026

but we expect:

3549 0
3549 10026
10026 0
3549 10026
10026 10010
10010 0
3549 10026
10026 0

tnx

Arash

Posted 2012-12-19T19:35:14.823

Reputation: 678

Sounds like a homework =) – Gilles Quenot – 2012-12-19T19:38:01.743

i can do it by sending item into array and then process the array but if we have 1000 lines its too slow in bash :(( !!!! – Arash – 2012-12-19T19:42:16.667

Answers

3

awk '{for (i=1; i<NF; i++) print $i, $(i+1)}' filename

EDIT: to reflect your new requirement

awk '{for (i=1; i<=NF; i++) printf("%d %d\n", $i, $(i+1))}' filename
# ----------------^ 

This takes advantage of the fact that awk treats uninitialized values (here $(NF+1)) as an empty string (in string context) or zero (in numeric context).

glenn jackman

Posted 2012-12-19T19:35:14.823

Reputation: 18 546

oh i'm sorry ive change the out put i need this , in above post edited – Arash – 2012-12-21T09:50:53.843

i mean if this is last digit in line put that digit and then zero besides it. – Arash – 2012-12-21T09:59:23.683

how could i chane this awk?? tnx alot – Arash – 2012-12-21T10:11:26.247

i want too put 0 not free space – Arash – 2012-12-21T16:11:17.143

tnx but your script is doing 1 2,2 3,3 4,4""(free space not "0") – Arash – 2012-12-21T16:13:12.730

hmm, not for me, it prints a zero. Show exactly what you're doing – glenn jackman – 2012-12-21T16:45:18.463

its print nothing in mine :(( – Arash – 2012-12-21T16:58:30.933

i edit my post to show you the results\ – Arash – 2012-12-21T17:02:15.860

What does awk --version show you? – glenn jackman – 2012-12-21T18:21:13.567

on ubuntu 12.10 returns awk: not an option: --version – Arash – 2012-12-21T19:29:59.763

How about type -a awk? – glenn jackman – 2012-12-22T03:56:03.447

awk is /usr/bin/awk is the response – Arash – 2012-12-22T06:21:24.110

3

Try doing this in

 perl -lane '$c=0; for (@F){ print "$F[$c]\t$F[$c+=1]" if $F[$c+1]}' file.txt

Or decomposed :

perl -lane '
    $c=0;
    for (@F) {
        print "$F[$c]\t$F[$c+=1]"
            if $F[$c+1];
    }
' file.txt

EXPLANATIONS

  • lane switchs means : l=newlines ; a=autosplit in @F array ; n=like while (<>) magic diamond operator ; e=basic switch to run a command
  • $c=0 assign 0 to a counter
  • for (@F) { for each element of the current line
  • print "$F[$c]\t$F[$c+=1]" : print array element with indice $c + tab + $c+1
  • if $F[$c+1]; : apply last line only if $F[$c+1] is not null

Or using (same algorithm), maybe more human readable for beginners :

while read a; do
    arr=( $a )
    for ((i=0; i< ${#arr[@]}; i++)); do
        [[ ${arr[i+1]} ]] && echo "${arr[i]} ${arr[i+1]}"
    done
done < file.txt

Gilles Quenot

Posted 2012-12-19T19:35:14.823

Reputation: 3 111

2While this (probably, can't test) gives the OP what they want, it would be nice to explain what exactly it does, so that everyone seeing it can learn a little. – slhck – 2012-12-19T19:45:30.313

1Sure, I do it now – Gilles Quenot – 2012-12-19T19:47:32.460

i'v got the error :(( unexpected EOF while looking for matching `'' – Arash – 2012-12-19T19:52:38.303

1Sorry, a quote forgotten =) – Gilles Quenot – 2012-12-19T19:54:45.843

its take too long!!!how we can optimized it? – Arash – 2012-12-19T20:04:26.727

1Be precise. Have you tested the 3 different versions ? – Gilles Quenot – 2012-12-19T20:07:38.240

2Can you show us a little bit of search effort ? Google have tons of results for this kind of things :/ If it's really a homework, in 1 week you will forget all this stuff... Sniff. – Gilles Quenot – 2012-12-19T20:14:03.567

oh i'm sorry!! tnx. i got it :) – Arash – 2012-12-19T20:15:04.777

3

Here's a pure bash solution:

while read -a a; do
    for ((i=0;i<${#a[@]}-1;++i)); do
        echo "${a[@]:i:2}"
    done
done < file.txt

gniourf_gniourf

Posted 2012-12-19T19:35:14.823

Reputation: 1 882