LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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

Reply
 
Search this Thread
Old 01-23-2005, 04:57 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,129

Rep: Reputation: 30
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
 
Old 01-23-2005, 05:28 AM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: Ubuntu, FreeBSD, NetBSD
Posts: 1,449

Rep: Reputation: 47
Not really a shell script... one command to do that.

Code:
awk 'BEGIN{FS=" "} {print $1, $4, $2, $3}' file.txt
 
Old 01-23-2005, 08:09 PM   #3
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,129

Original Poster
Rep: Reputation: 30
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
 
Old 01-23-2005, 08:29 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Just put the spaces in quotes
awk '{print $1" "$4" "$2" "$3}' file.txt
 
Old 01-24-2005, 12:45 AM   #5
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,129

Original Poster
Rep: Reputation: 30
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
 
Old 01-24-2005, 09:17 AM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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
 
Old 01-24-2005, 01:06 PM   #7
LasseW
Member
 
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108

Rep: Reputation: 15
You can also use c style format strings with the awk printf command:

awk '{printf "%6d%6d%6d%6d\n",$1,$2,$3,$4}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Writing a shell script mattz40 Debian 5 04-13-2005 06:56 PM
writing a shell script tchoua Linux - General 8 02-24-2005 06:18 PM
writing my first shell script speedhead34 Linux - Newbie 4 12-20-2004 12:50 AM
help writing shell script np complete Linux - Newbie 6 08-30-2004 09:43 PM
Need Help in Writing a Shell Script. KptnKrill Linux - General 2 05-19-2003 05:44 AM


All times are GMT -5. The time now is 02:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration