- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: swap the last 2 fields
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 05:41 AM
тАО07-21-2009 05:41 AM
I need to swap the last 2 fields in every line.
Input File :
B L1983A B1N 20090701 A QBL1
B L1985A 20090701 A QBL1
Output File should be:
B L1983A B1N 20090701 QBL1 A
B L1985A 20090701 QBL1 A
Could somebody let me know if this can be done using awk?
Thanks & Regards,
Sathis Kumar.B
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 06:10 AM
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 06:11 AM
тАО07-21-2009 06:11 AM
Re: swap the last 2 fields
echo $line|awk {'print $1" "$2" "$3" "$5" "$4'}
done
provided all lines have exactly 5 fields.
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 06:15 AM
тАО07-21-2009 06:15 AM
Re: swap the last 2 fields
Of course it can be done.
You need to use for loop:
awk '{for (i=1; i<=NF-2; ++i) printf "%s ", $i; print $(NF), $(NF-1)}' filename
This worked fine on my Linux server at home.
Cheers,
VK2COT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 06:23 AM
тАО07-21-2009 06:23 AM
Re: swap the last 2 fields
There are more than a way always. If the line lenths(field) are varied use this for loop. Assuming the file name is /tmp/test.txt
for A in `cat /tmp/test.txt`
do
echo $A | awk '{tmp = $NF; $NF = $(NF-1); $(NF-1) = tmp; print }' >> /tmp/test1.txt
done
Now you have the desired output in /tmp/test1.txt
Ganesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 04:03 PM
тАО07-21-2009 04:03 PM
Re: swap the last 2 fields
cat my file | while read line; do
echo $line|awk {'print $1" "$2" "$3" "$5" "$4'}
done
you can eliminate the cat, while loop and echo. simply
awk '{print $1" "$2" "$3" "$5" "$4'} old_file > new_file
lets awk do the read and it will loop for every line in the original file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 04:06 PM
тАО07-21-2009 04:06 PM
Re: swap the last 2 fields
awk '{tmp = $NF; $NF = $(NF-1); $(NF-1) = tmp; print }' /tmp.test >> /tmp/test1.txt