If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Would like to reverse order of items on a line

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-14-03, 13:04
jawemer jawemer is offline
Registered User
 
Join Date: Aug 2003
Location: Waukee, Iowa
Posts: 3
Question 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
Reply With Quote
  #2 (permalink)  
Old 08-14-03, 13:18
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,543
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

Quote:
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.
Reply With Quote
  #3 (permalink)  
Old 08-14-03, 13:48
jawemer jawemer is offline
Registered User
 
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!

Quote:
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
Reply With Quote
  #4 (permalink)  
Old 08-14-03, 14:29
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,543
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


Quote:
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.
Reply With Quote
  #5 (permalink)  
Old 08-14-03, 14:35
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
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
Reply With Quote
  #6 (permalink)  
Old 08-15-03, 05:40
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: Would like to reverse order of items on a line

Quote:
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.
Reply With Quote
  #7 (permalink)  
Old 08-15-03, 11:47
jawemer jawemer is offline
Registered User
 
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!


Quote:
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On