1827295 Members
2390 Online
109717 Solutions
New Discussion

script help

 
jackfiled
Advisor

script help

hello..

I've been getting good tips from you
thank you

so I post a message..like script problem


gaul IN A 211.230.50.34
buksan IN A 211.230.50.34
sangwhang IN A 211.230.50.34
younhz IN A 211.230.50.34
kixx IN A 211.230.50.34
pooh4001 IN A 211.230.50.34
pdy1260 IN A 211.230.50.34
edonlee IN A 211.230.50.34
pun255840 IN A 211.230.50.34
buwon2702 IN A 211.230.50.34
leeys56 IN A 211.230.50.34
hyi2900 IN A 211.230.50.34
gemhee IN A 211.230.50.34
jso9782 IN A 211.230.50.34
arc1370 IN A 211.230.50.34
dong1637 IN A 211.230.50.34
eelchoi IN A 211.230.50.34
junlee IN A 211.230.50.34
jdoryong IN A 211.230.50.34
cjfgh61 IN A 211.230.50.34
sansarang IN A 211.230.50.34
hoamshin IN A 211.230.50.34
kcmkjk IN A 211.230.50.34
bhsong52 IN A 211.230.50.34
bamgol IN A 211.230.50.34
lsk2104 IN A 211.230.50.34
sasum4388 IN A 211.230.50.34
kjs3896 IN A 211.230.50.34
hannul IN A 211.230.50.34
rnwhdah70 IN A 211.230.50.34

....
omitted..

As you see lines 'IN A 211.230.50.34' is
not aligned

so I have been editting it by vi and up and
down keyboard, press tabs or space keys to make them align

it is boring and waste too much time.
totals are over 200 lines.
How can I use script to reduce time like doing
jobs.

Any tips will be helpful


11 REPLIES 11
Bill Hassell
Honored Contributor

Re: script help

This is easy using typeset to define the size of each field:

#!/usr/bin/sh
PATH=/usr/bin
set -u
typeset -L10 MYNAME
typeset -L2 IN
typeset -L1 A
typeset -L15 IPADDR
cat | while read MYNAME IN A IPADDR
do
print "$MYNAME $IN $A $IPADDR"
done

To use the script:

cat unformatted_file | this_script


Bill Hassell, sysadmin
Steven Sim Kok Leong
Honored Contributor

Re: script help

Hi,

Off my head, here's a not-so-clean approach:

# cat $file | awk '{print $1"\t"$2"\t"$3"\t"$4}'

However, you need to set your tab size to a size that exceeds the length of your first name field so that all tabs get aligned.

Hope this helps. Regards.

Steven Sim Kok Leong
jackfiled
Advisor

Re: script help

how to set my tabsize?
Mark Grant
Honored Contributor

Re: script help

setting the tabsize is a function of the terminal you are using to view it. Some terminals allow you set the tab size with escape sequences defined in /etc/termcap or the /etc/terminfo database BUT most don't. I wouldn't use tabs if you really want to make sure it all lines up always.
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: script help

I am attaching a small perl thing tou format this all nicely for you. I'll post it here too but the lack of a
 tag will play havoc with the formatting!

You need to pipe your data into this script.


#!/usr/bin/perl

format =
@<<<<<<<<<<<<<<<<<<< @<< @< @<<<<<<<<<<<<<<<
$text, $ch, $letter, $IP
.

while(<>){
($text,$ch,$letter,$IP)=split " ",$_;
write;
}
Never preceed any demonstration with anything more predictive than "watch this"
Bill Hassell
Honored Contributor

Re: script help

In my script example above, you can change the typeset values to define the width of each field, or leave them as is, and change the print statement to include more spaces as needed. The tab character is only useful for terminals and programs that understand tab as a field separator or white space. To align the DNS data above, just count the spaces and adjust the print statement. Here in the forums, the text optimizer removes blank lines, leading spaces and multiple spaces are truncated to one space. So to illustrate multiple spaces, I'll use the _ underscore character:

print "$MYNAME___$IN___$A________$IPADDR"


Bill Hassell, sysadmin
Leif Halvarsson_2
Honored Contributor

Re: script help

Hi,
An alternative to typeset is to use printf
while read a b c d
do
printf "%10s, %2s, %1s, %15s\n" $a $b $c $d
done
Sergejs Svitnevs
Honored Contributor

Re: script help

My solution:

perl -pi -e 's/(\w{8,})\s(IN)\s(A)\s(.*)/$1\t$2\t$3\t$4/;s/(\w*(?!\t))\s(IN)\s(A)\s(.*)/$1\t\t$2\t$3\t$4/' file

Regards,
Sergejs
Jean-Luc Oudart
Honored Contributor

Re: script help

another awk script

awk '{printf("%s %2s %1s %15s\n",substr($1" ",1,10),$2,$3,$4);}'

Rgds,
Jean-Luc
fiat lux
Jean-Luc Oudart
Honored Contributor

Re: script help

Please note spaces have disappeared !
in substr($1" " ... 9 spaces.
Another strange behaviour here !

Jean-Luc
fiat lux
Michael Morgan_5
New Member

Re: script help

One liner script like this will do the trick:
cat dnsfile | awk '{print $1,"/t",$2,"/t",$3,"/t",$4}' >newdnsfile

Michael
king of the one liners...