473,387 Members | 1,540 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,387 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 6105
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...

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.