473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Help me with awk command in Unix please

25
This is my file named students

Expand|Select|Wrap|Line Numbers
  1. Tom     Jones   100     90      80
  2. Nancy   Jones    70     80      90
  3. Terry   Sims     55     65      75
  4. John    Terry    75     76      77
  5. Ruth    Maier    100    100     100
  6. Mike    Wolfe    90     95      98
  7. Dennis  Cole     70     80      89
  8. Ron     Maier    90     85      89
  9. Susan   Miller   65     80      90
  10. Mad     Bill     75     65      77
I want to print out the contents of students file such that first names and last names are swapped.

I plan to write a for loop print out one first name (first column) at the time, the take it to compare if it equal with any last name at second column by another nest for loop. But I can't. UNIX is new to me and awk is weird, it prints all at the time not like in C.

Please help me this!
Feb 26 '07 #1
4 6108
sicarie
4,677 Expert Mod 4TB
This is my file named students

Expand|Select|Wrap|Line Numbers
  1. Tom     Jones   100     90      80
  2. Nancy   Jones    70     80      90
  3. Terry   Sims     55     65      75
  4. John    Terry    75     76      77
  5. Ruth    Maier    100    100     100
  6. Mike    Wolfe    90     95      98
  7. Dennis  Cole     70     80      89
  8. Ron     Maier    90     85      89
  9. Susan   Miller   65     80      90
  10. Mad     Bill     75     65      77
I want to print out the contents of students file such that first names and last names are swapped.

I plan to write a for loop print out one first name (first column) at the time, the take it to compare if it equal with any last name at second column by another nest for loop. But I can't. UNIX is new to me and awk is weird, it prints all at the time not like in C.

Please help me this!
What do you have so far?
Feb 27 '07 #2
cat your_Text_File | awk '{print $2, $1}'

For example:
cat switchTwoColoumn.txt | awk '{print $2, $1}'

This command:
Switch coloumn 1 and 2 in switchTwoColoumn.txt file.
print then on screen.

output of your text file:

Jones Tom
Jones Nancy
Sims Terry
Terry John
Maier Ruth
Wolfe Mike
Cole Dennis
Maier Ron
Miller Susan
Bill Mad
Feb 27 '07 #3
skp246
1
This is my file named students

Expand|Select|Wrap|Line Numbers
  1. Tom     Jones   100     90      80
  2. Nancy   Jones    70     80      90
  3. Terry   Sims     55     65      75
  4. John    Terry    75     76      77
  5. Ruth    Maier    100    100     100
  6. Mike    Wolfe    90     95      98
  7. Dennis  Cole     70     80      89
  8. Ron     Maier    90     85      89
  9. Susan   Miller   65     80      90
  10. Mad     Bill     75     65      77
I want to print out the contents of students file such that first names and last names are swapped.

I plan to write a for loop print out one first name (first column) at the time, the take it to compare if it equal with any last name at second column by another nest for loop. But I can't. UNIX is new to me and awk is weird, it prints all at the time not like in C.

Please help me this!
[code] awk '{print $2 "\t" $1 "\t" $3 "\t" $4 "\t" $5 }' students
Jul 4 '07 #4
ghostdog74
511 Expert 256MB
cat your_Text_File | awk '{print $2, $1}'
For example:
cat switchTwoColoumn.txt | awk '{print $2, $1}'
useless use of cat.
Expand|Select|Wrap|Line Numbers
  1.  awk '{print $2,$1}' file 
Jul 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mathieu Malaterre | last post by:
Hello, I thought this would be easy but I guess I didn't get the distutil feeling. I am trying to write a setup for install my package but I don't understand how to do that. organisation: ...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
5
by: choudhary.poorva | last post by:
I am new to C programming and learning Commands on Unix for my exam on Interprocess communication. I would appreciate if anyone can explain the following command: int chmod ( const char *...
0
by: truthbajaj | last post by:
Hi, I am using Oracle 9i and Unix on my system and trying to execute a UNIX shell command through external procedure in C. I created a shared lib (libextproc.so) for the following function. ...
4
by: Guern1 | last post by:
Hi Need a bit of help here please to point me in the right direction. I have a java class file here which i wish from a menu item to open a web page which contains a help page. ] } else...
2
by: jonathan184 | last post by:
Hi basically what i want this script to do is In a particular dir there are alot of files from today back to 1999 or earlier. So I am trying to have the script search this dir and sort the files...
5
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.