Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Location
    Waukee, Iowa
    Posts
    3

    Question Unanswered: Would like to reverse order of items on a line

    Just wondering if there is an easy way to reverse the order of two columns of data in a file separated by a space. I know how to | cut -d" " -f1 (or -f2) and output to separate files (file1.txt and file2.txt, for example) then paste -d" " file2.txt file1.txt. Is there an easier way? Thanks!

    oh, this is NOT homework (some of the stuff on here looks like it is). I work for Wells in Iowa and just wanted to change a file format in an easier method. Here is some some "sample" data:

    ADAM BAKER
    ALANE BENDTSEN
    ANDREW MCLEAN
    ANDREW MUSANTE
    ANGELA ERBSEN
    ANGELA WATERS
    ANNA ELITHORP
    ANSON PANG

    I would like the last name first and first name last (reversed)... I'm not worried about sorting (I know how to | sort). Thank you so much!

    Jason

  2. #2
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650

    Re: Would like to reverse order of items on a line

    I would do

    nawk '{print $2 " " $1}' | fil1.txt > file2.txt

    file2.txt will be

    BAKER ADAM
    BENDTSEN ALANE
    MCLEAN ANDREW
    MUSANTE ANDREW
    ERBSEN ANGELA
    WATERS ANGELA
    ELITHORP ANNA
    PANG ANSON

    HTH

    Sathyaram

    Originally posted by jawemer
    Just wondering if there is an easy way to reverse the order of two columns of data in a file separated by a space. I know how to | cut -d" " -f1 (or -f2) and output to separate files (file1.txt and file2.txt, for example) then paste -d" " file2.txt file1.txt. Is there an easier way? Thanks!

    oh, this is NOT homework (some of the stuff on here looks like it is). I work for Wells in Iowa and just wanted to change a file format in an easier method. Here is some some "sample" data:

    ADAM BAKER
    ALANE BENDTSEN
    ANDREW MCLEAN
    ANDREW MUSANTE
    ANGELA ERBSEN
    ANGELA WATERS
    ANNA ELITHORP
    ANSON PANG

    I would like the last name first and first name last (reversed)... I'm not worried about sorting (I know how to | sort). Thank you so much!

    Jason
    Visit the new-look IDUG Website , register to gain access to the excellent content.

  3. #3
    Join Date
    Aug 2003
    Location
    Waukee, Iowa
    Posts
    3

    Re: Would like to reverse order of items on a line

    Thank you, Sathyaram.

    I haven't used awk before. If I have this file called users.txt can I pipe that to nawk '{print $2 " " $1}' then output (>) to users_reversed.txt? I'm using MKS Toolkit and getting an error message when I tried that. Is the $2 and $1 for columns or files? I'm pretty new to this, sorry! To test I wrote:
    cat ./users.txt | nawk '{print $2 " " $1}' > ./test_rev.txt
    but getting error msg:
    nawk: ./nawk_test.sh 1: not found
    Is it wanting: ./nawk_test.sh ./file1.txt ./file2.txt ?
    Thank you, again!

    Originally posted by sathyaram_s
    I would do

    nawk '{print $2 " " $1}' | fil1.txt > file2.txt

    file2.txt will be

    BAKER ADAM
    BENDTSEN ALANE
    MCLEAN ANDREW
    MUSANTE ANDREW
    ERBSEN ANGELA
    WATERS ANGELA
    ELITHORP ANNA
    PANG ANSON

    HTH

    Sathyaram

  4. #4
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650

    Re: Would like to reverse order of items on a line

    I do not understand what has gone wrong ..

    Well, in nawk (or you can use awk) ... $1 refers to the first filed of the input file , $2 the second and so on (default field seperator is space)

    Sorry for not being able to help further

    Sathyaram


    Originally posted by jawemer
    Thank you, Sathyaram.

    I haven't used awk before. If I have this file called users.txt can I pipe that to nawk '{print $2 " " $1}' then output (>) to users_reversed.txt? I'm using MKS Toolkit and getting an error message when I tried that. Is the $2 and $1 for columns or files? I'm pretty new to this, sorry! To test I wrote:
    cat ./users.txt | nawk '{print $2 " " $1}' > ./test_rev.txt
    but getting error msg:
    nawk: ./nawk_test.sh 1: not found
    Is it wanting: ./nawk_test.sh ./file1.txt ./file2.txt ?
    Thank you, again!
    Visit the new-look IDUG Website , register to gain access to the excellent content.

  5. #5
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Thumbs up

    sathyaram_s is correct, you can use awk.

    also, there was a typo in his script (an unwanted pipe '|' character).

    Try the corrected script:

    awk '{print $2 " " $1}' fil1.txt > file2.txt


    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: Would like to reverse order of items on a line

    Originally posted by jawemer
    Thank you, Sathyaram.

    cat ./users.txt | nawk '{print $2 " " $1}' > ./test_rev.txt
    but getting error msg:
    nawk: ./nawk_test.sh 1: not found
    Is it wanting: ./nawk_test.sh ./file1.txt ./file2.txt ?
    I think maybe your nawk in this case is an alias.

    Try:

    'nawk' '{print $2, $1}' users.txt > test_rev.txt # the single quotes wil prevent the shell using the alias.

    or

    unalias nawk; 'nawk' '{print $2, $1}' users.txt > test_rev.txt

    or

    /usr/bin/nawk '{print $2, $1}' users.txt > test_rev.txt

    Or use awk.

  7. #7
    Join Date
    Aug 2003
    Location
    Waukee, Iowa
    Posts
    3

    Re: Would like to reverse order of items on a line

    Thanks!!!

    These all worked great (of course!) and I have the desired results.
    Thank you, Everyone!


    Originally posted by Damian Ibbotson
    I think maybe your nawk in this case is an alias.

    Try:

    'nawk' '{print $2, $1}' users.txt > test_rev.txt # the single quotes wil prevent the shell using the alias.

    or

    unalias nawk; 'nawk' '{print $2, $1}' users.txt > test_rev.txt

    or

    /usr/bin/nawk '{print $2, $1}' users.txt > test_rev.txt

    Or use awk.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •