2

I have a shell script which extracts information from Vservers, this is the script:

for i in  {130..136}; do
> ./vserver/Info$i
ssh 132.138.180.$i "hostname;
echo 'Virtual'
echo ''
cat /etc/issue | head -1
echo ''
dmidecode | grep Socket | tail -1 | awk '{print \$4}' 
echo ''
free -g | grep Mem | awk '{ print \$2 }'
echo ''
fdisk -l | grep Disk | wc -l
echo ''
df -h | grep ^/ | wc -l 
echo ''
ifconfig | grep inet | awk '{print  \$2 }' | cut -c 6- | awk '\$1=\$1' ORS=' '
echo ''
ifconfig | grep -b1 inet | grep HWaddr | awk '{ print \$5 }' | awk '\$1=\$1' ORS=' '
echo ''
ip route show | grep default | awk '{ print \$3 }' | awk '\$1=\$1' ORS=' '
echo ''
cat /etc/resolv.conf | grep name | awk '{ print \$2 }' | awk '\$1=\$1' ORS=' '
echo ''
mount | grep el01 | awk '{ print \$1 \" -> \" \$3 }' | awk '\$1=\$1' ORS=' '
echo ''
netstat -nr | awk '{ print \$1, \$2, \$3, \$8 }'
echo ''
" >> ./vserver/Info$i
done

I have the following output(example):

el01test
Virtual

Oracle Linux Server

2

7

1

2

19.16.10.111 12.1.0.1 12.1.0.11 12.1.2.11 127.0.0.1 
00:22:4F:F9:3C:D8 80:22:05:F7:FE:80:00:00:00:00:00:00:00:00:00:00:00:00:00:00 22:44:22:F4:FE:80:00:00:00:00:00:00:00:00:00:00:00:00:00:00 22:44:22:E2:FE:80:00:00:00:00:00:00:00:00:00:00:00:00:00:00 
19.16.10.1 
19.16.10.12 19.16.10.15 
el01:/export/el011->/home/glassfish/glassfish el01:/export/logs/el01vur01->/home/glassfish/logs el01:/export/home/oem12ag/age->/home/oem12ag/agent 
Kernel IP routing 
Destination Gateway Genmask Iface
0.0.0.0 192.168.181.1 0.0.0.0 bond0
169.254.0.0 0.0.0.0 255.255.0.0 bond1
17.1.0.0 0.0.0.0 255.255.0.0 bond2
17.1.0.0 0.0.0.0 255.255.0.0 bond1
19.16.0.0 0.0.0.0 255.255.252.0 bond3
19.16.10.0 0.0.0.0 255.255.252.0 bond0

I would like to format my info like this:

hostname    OS  Core_number Free_memory IP1
                                        IP2
                                        IP3

I've been trying by using awk but I haven't had much luck with it. Thanks for your help!

6
  • Do you really have empty lines between your fields, or is this just a formatting issue? You can indent code by four spaces to preserve formatting. Commented Jun 8, 2016 at 21:07
  • @josseossa I added some formatting for you. If it's not right, edit your question to fix it. Commented Jun 8, 2016 at 21:20
  • Hello, Yes Benjamin, there are empty lines as field separator, but i can delete them if necessary, actually there are a bunch of virtual servers which i need to get some info and this is an example of the script output. I need to format it at the table way as showed. Thanks Glenn for the formatting, this is actually my first post here and didn't know hot to do that. Commented Jun 8, 2016 at 21:26
  • are any/most of the values in that "data" unchanging values or are you using place-holder names? I mean, which of the following are actual text in your file, hostname ,OS ,Core_number. Free_memory IPn ? If you have real data for OS, Free_memory, etc that you want to format per your example it will be much easier to help you if you include real data, and real expected output (not greater than 60-80 chars wide please). also, awk is designed for such problems read about printf("%12s\n") and similar. Good luck. Commented Jun 8, 2016 at 21:39
  • Hello @shelter, the data is actually data from servers, this is an output example from one of them: el01vserver1 Virtual Oracle Linux Server release 5.9 2 7 1 2 132.838.180.112 133.28.0.32 133.28.0.23 133.28.0.24 127.0.0.1 Commented Jun 9, 2016 at 1:28

3 Answers 3

2

You can do it with awk, by reading in the lines into an array. Then output the lines containing the information into the required table format.

Here is an example to give you the rough idea:

script.awk

    { info[ i++ ] = $1 }
END { printf("%s\t%s\t%s\t%s\t%s\n", info[0], info[3], info[5], info[7], info[9])
      printf("\t\t\t\t\t\t\t%s\n", info[10])
      printf("\t\t\t\t\t\t\t%s\n", info[11])
    }

Use it like this: awk -f script.awk yourfile.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, i got it. In this case we should use the delimiter as RS= FS="\n", right?
Hello @Lars Fischer, i read your answer but maybe it's not quite accurate as i thought at first because in some outputs i could have 3 ip and in other i could have 4, so i couldn't arranged it manually as suggested.
@josseossa For the case with a fourth IP just add if( info[12] ) printf("\t\t\t\t\t\t\t%s\n", info[12]) after the third printf.
0

i finally finish my formatting script, thanks to all of you for your help, finally y had to modify the initial script and then format it using the next code:

for q in $(ls); do awk ' NR >= 19 { route[ j++ ] = $0; next } NF==1 { info[ i++ ] = $1; next  } NR == 4 { OS= $0 } NR == 14  { A=split($0, Ip, " "); next } NR == 15 { B=split($0, Mac, " "); next } NR == 17 { C=split($0, Dns, " "); next } NR == 18 { D=split($0, Mount, " "); next } END { if (A > B) max1=A ; else max1=B; if (C > D) max2=C; else max2=D; if (max1 > max2) max=max1; else max=max2; if (max < j) max=j; for (i=0; i<=6; i++) {printf "%s\t ",info[i]} ; for (w = 0; w <= max; w = w+1)  { printf("\t\t\t\t\t\t\t%s\t%s\t%s\t%s\t%s\n",Ip[w], Mac[w], Dns[w], Mount[w], route[w]) }  } ' $q; done > Fullout

Thanks a lot for your help! If maybe someone knows how to mix the printf in order to them be able to print at the same line i would appreciated. I'm also aware that the code could be optimised but i was in a hurry, sorry :(. I posted one of the output of the initial script in the initial answer if anyone wants to test it for your further scripting. Thanks

Comments

0

Instead of writing multiple tabs, you could set tabstops using tabs command, or ANSI code inline.

ANSI/VT100 Terminal Control Escape Sequences

or

Linux tabs command

Then, write your output code with a single \t tab.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.