Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-23-2005, 04:57 AM
|
#1
|
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,089
Rep:
|
shell script writing
I am new to shell writing , could suggest how to write the below script,
I have the below file , the content as below
#vi file.txt
10 11 12 13
20 21 22 23
30 31 32 33
then run the script , the new output as below,
10 13 11 12
20 23 21 22
30 33 31 32
could suggest how to write the script ? thx
|
|
|
|
01-23-2005, 05:28 AM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: Ubuntu, FreeBSD, NetBSD
Posts: 1,449
Rep:
|
Not really a shell script... one command to do that.
Code:
awk 'BEGIN{FS=" "} {print $1, $4, $2, $3}' file.txt
|
|
|
|
01-23-2005, 08:09 PM
|
#3
|
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,089
Original Poster
Rep:
|
thx reply , it work,
but how to add space to each column , eg. if I want to have 3 space line between $1 and $4 , how to set it ? thx
|
|
|
|
01-23-2005, 08:29 PM
|
#4
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Just put the spaces in quotes
awk '{print $1" "$4" "$2" "$3}' file.txt
|
|
|
|
01-24-2005, 12:45 AM
|
#5
|
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,089
Original Poster
Rep:
|
thx reply , but still have a problem if add the space to each column
the output is not tidy now, because the space seems start from the last character of previous word ,as the file.txt not exactly as the above exapmle
my file.txt should be as below ,
10 133 11 12
200 23333 211 22
30000 33 3111 32
if I add 4 spaces to each column , the out will be as below.
10 133 11 12
200 23333 211 22
30000 33 3111 32,
could suggest how to make the result is more tidy as below? thx
|
|
|
|
01-24-2005, 09:17 AM
|
#6
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Quote:
|
the output is not tidy now, because the space seems start from the last character of previous word
|
I think tabs would be better in this case. If some nunbers are quite large, you may want more than one tab..... "\t\t"
Code:
awk '{print $1"\t"$4"\t"$2"\t"$3}' file.txt
|
|
|
|
01-24-2005, 01:06 PM
|
#7
|
|
Member
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108
Rep:
|
You can also use c style format strings with the awk printf command:
awk '{printf "%6d%6d%6d%6d\n",$1,$2,$3,$4}'
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:46 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|