Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swap fields in a line of text

Status
Not open for further replies.

alan147

Technical User
Nov 15, 2002
128
GB
Good afternoon

I have a text file, part of the password file, it's format is

username:uid

I need to be able to reverse the output so that the uid is first i.e.

uid:username

Does anyone have a way of doing this? The suggestions from my office are to import it into excel !!!!!!

Thanks

Alan
 
.*" does a "greedy" match so it would in fact match all of the line. in /etc/passwd up to the last ":".

I would recommend:

Code:
sed 's/\([^:]*\):\([^:]*\)/\2:\1/' /input/file

Although feherke probably correctly assumed that your existing file only has two fields anyway?

Annihilannic.
 
Hello

feherke did assume correctly, my file only had the username and uid in it.

However I now have another question. Now that the username and uid have been swapped I now need to extract those lines where the UID is identical, I know they should be unique but that is the purpose of this exercise.

Thanks


Alan
 
[tt]sort -t: -k 1,1 filename | awk '$1==prev{print}{prev=$1}'[/tt]

Annihilannic.
 
cat file | awk -v FS=":" '{print $2":"$1}' > newfile

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
man cut
man uniq

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Here's a Korn shell solution...
Code:
while IFS=: read USER UID; do print "${UID}:${USER}"; done < /input/file
To print the duplicate userids...
Code:
cut -d: -f2 /input/file | sort -n | uniq -d
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top