Shell - issue having table output format

Hi,

Im working on a command to first filter some lines from a txt file, filter the fields from these lines and then format it into a table. All of this running these commands with a dif. user from the one that is running the script in the first place.

Here is the "first txt file": (has over 6k lines, all with the smae number of fields...)
.
.
"DpAdminServer" -inst="DpAdminServer" "Client/ProfileAccessRights/OaM/PanelLabel" "O&M" -type=STRING -description="Only change this parameter after consulting engineers from SmartTrust. Configuration of the DP administrator GUI." -visibility=INSTANCE
"DpAdminServer" -inst="DpAdminServer" "Client/ProfileAccessRights/OaM/OrderingNo" "5" -type=FWU8 -description="Only change this parameter after consulting engineers from SmartTrust. Configuration of the DP administrator GUI." -visibility=INSTANCE
.
.

So the script is this one:
rtool_smsc=$(sudo -H -u dpa bash -c 'cd; . .profile; rtool | grep -E 'SMPP_A/ProtocolVersion|SMPP_A/OriginatingAddressTon|SMPP_A/OriginatingAddressNpi|SMPP_A/DestinationAddressTon|SMPP_A/DestinationAddressNpi|SMPP_A/SystemType|SMPP_A/SystemId|SMPP_A/Password|SMPP_A/BindTransceiver|SMSC_CONNECTION_1/SEND/TCPIP/StartConnection|SMSC_CONNECTION_1/ProtocolType|SMSC_CONNECTION_1/ChannelType|SMSC_CONNECTION_1/Protocol_ID|SMSC_CONNECTION_1/TextSMOriginatingAddress|SMSC_CONNECTION_1/OTASMOriginatingAddress|SMSC_CONNECTION_1/RECEIVE/TCPIP/Port|SMSC_CONNECTION_1/SEND/TCPIP/Port|SMSC_CONNECTION_1/SEND/TCPIP/SMSCTcpIpAddress|SMSC_CONNECTION_1/RECEIVE/TCPIP/SMSCTcpIpAddress|SMSC_CONNECTION_1/TextSMOriginatingAddressTON|SMSC_CONNECTION_1/TextSMOriginatingAddressNPI|SMSC_CONNECTION_1/OTASMOriginatingAddressTON|SMSC_CONNECTION_1/OTASMOriginatingAddressNPI' | cut -d " " -f 1,2,3,4,5')
echo "$rtool_smsc" | awk -v W1=30 -v W2=15 -v W3=60 -v W4=40 -v W5=10 LC="=" 'BEGIN {FORMAT="%-"W1"s%-"W2"s%-"W3"s%-"W4"s%-"W5"s\n"; printf FORMAT,"Server","Instance","Parameter","Value","Type"; while (L++<W1+W2+W3+W4+W5) R=R LC; print R} !/^Unable/ {printf FORMAT,$1,$2,$3,$4,$5}'


The command line (the rtool one gives me this:
"TransportServer" -inst="ts1a" "SMSC_CONNECTION_1/RECEIVE/TCPIP/Port" "25505" -type=FWI32
"TransportServer" -inst="ts1a" "SMSC_CONNECTION_1/RECEIVE/TCPIP/SMSCTcpIpAddress" "localhost" -type=STRING
"TransportServer" -inst="ts1a" "SMSC_CONNECTION_1/SEND/TCPIP/Port" "25505" -type=FWI32


So, the first problem is:
The sudo part seems not to like the | in the grep part... its just interrupting the command read from there... Any suggestion? Maybe escape the | ?


And when I run it without the sudo (with the right user just to test the table part it gives me this:

awk: cmd. line:1: LC==
awk: cmd. line:1:     ^ unexpected newline or end of string


The goal is to have this: (with the sudo in place)

Server                           Instance                              Parameter                                             Value                  Type
=============================================================================================
"TransportServer"    -inst="ts1a"     "SMSC_CONNECTION_1/RECEIVE/TCPIP/Port"    "25505"     -type=FWI32


Tks,
Joao
joaotellesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

joaotellesAuthor Commented:
The output for this part:

So, the first problem is:
The sudo part seems not to like the | in the grep part... its just interrupting the command read from there... Any suggestion? Maybe escape the | ?

++ sudo -H -u dpa bash -c 'cd; . .profile; rtool | grep -E SMPP_A/ProtocolVersion'
++ SMPP_A/OriginatingAddressTon
test.sh: line 1: SMPP_A/OriginatingAddressTon: No such file or directory
.
.


Tks,
Joao
0
woolmilkporcCommented:
Put the grep pattern (the one with the pipe symbols) between double quotation marks instead of single ones, i .e. from
"SMPP_A/ProtocolVersion|.... up to ...|SMSC_CONNECTION_1/OTASMOriginatingAddressNPI".

Is this really all in one line? You can continue commands on the next line(s) with "\"!

The awk error is due to a missing "-v" on the command line:

awk -v W1=30 -v W2=15 -v W3=60 -v W4=40 -v W5=10 -v LC="="

The "cut" command is not really needed since later you're instructing "awk" to print the first five fields only.

Finally: It's not clear to me what this "first text file" is and what it should be good for!
0
woolmilkporcCommented:
If you don't need the variable "rtool_smsc" for later use you don't need to create it.

Just try it like this (continuation applied, "cut" left in place):

sudo -H -u dpa bash -c 'cd; . .profile; rtool |\
grep -E "SMPP_A/ProtocolVersion|\
SMPP_A/OriginatingAddressTon|\
SMPP_A/OriginatingAddressNpi|\
SMPP_A/DestinationAddressTon|\
SMPP_A/DestinationAddressNpi|\
SMPP_A/SystemType|\
SMPP_A/SystemId|\
SMPP_A/Password|\
SMPP_A/BindTransceiver|\
SMSC_CONNECTION_1/SEND/TCPIP/StartConnection|\
SMSC_CONNECTION_1/ProtocolType|\
SMSC_CONNECTION_1/ChannelType|\
SMSC_CONNECTION_1/Protocol_ID|\
SMSC_CONNECTION_1Text/SMOriginatingAddress|\
SMSC_CONNECTION_1/OTASMOriginatingAddress|\
SMSC_CONNECTION_1/RECEIVE/TCPIP/Port|\
SMSC_CONNECTION_1/SEND/TCPIP/Port|\
SMSC_CONNECTION_1/SEND/TCPIP/SMSCTcpIpAddress|\
SMSC_CONNECTION_1/RECEIVE/TCPIP/SMSCTcpIpAddress|\
SMSC_CONNECTION_1/TextSMOriginatingAddressTON|\
SMSC_CONNECTION_1/TextSMOriginatingAddressNPI|\
SMSC_CONNECTION_1/OTASMOriginatingAddressTON|\
SMSC_CONNECTION_1/OTASMOriginatingAddressNPI" |\
cut -d " " -f 1,2,3,4,5' |\
awk -v W1=30 -v W2=15 -v W3=60 -v W4=40 -v W5=10 -v LC="=" '
BEGIN {FORMAT="%-"W1"s%-"W2"s%-"W3"s%-"W4"s%-"W5"s\n"; 
printf FORMAT,"Server","Instance","Parameter","Value","Type"; 
while (L++<W1+W2+W3+W4+W5) R=R LC; print R} 
{printf FORMAT,$1,$2,$3,$4,$5}'

Open in new window

I'm sure I never saw such a mighty grep pattern before.
It looks just as if a "grep -v"  to get rid of the few lines which you don't want to see would be shorter.

Note for "awk": The part "!/^Unable/ " is a remnant of the command I suggested in our very first thread.
You don't need it all the time - it is only required to filter out a line starting with "Unable" (Remember?)

To get rid of the quotation marks in the output change line 30 to:

{gsub("\"","",$0); printf FORMAT,$1,$2,$3,$4,$5}'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
woolmilkporcCommented:
Note for "grep":

You can put all patterns in a file instead of specifying them on the command line.

So if you create a file patternlist looking like this:

SMPP_A/ProtocolVersion
SMPP_A/OriginatingAddressTon
SMPP_A/OriginatingAddressNpi
SMPP_A/DestinationAddressTon
SMPP_A/DestinationAddressNpi
SMPP_A/SystemType
SMPP_A/SystemId
SMPP_A/Password
SMPP_A/BindTransceiver
SMSC_CONNECTION_1/SEND/TCPIP/StartConnection
SMSC_CONNECTION_1/ProtocolType
SMSC_CONNECTION_1/ChannelType
SMSC_CONNECTION_1/Protocol_ID
SMSC_CONNECTION_1Text/SMOriginatingAddress
SMSC_CONNECTION_1/OTASMOriginatingAddress
SMSC_CONNECTION_1/RECEIVE/TCPIP/Port
SMSC_CONNECTION_1/SEND/TCPIP/Port
SMSC_CONNECTION_1/SEND/TCPIP/SMSCTcpIpAddress
SMSC_CONNECTION_1/RECEIVE/TCPIP/SMSCTcpIpAddress
SMSC_CONNECTION_1/TextSMOriginatingAddressTON
SMSC_CONNECTION_1/TextSMOriginatingAddressNPI
SMSC_CONNECTION_1/OTASMOriginatingAddressTON
SMSC_CONNECTION_1/OTASMOriginatingAddressNPI

Open in new window

all which has to remain is this:

sudo -H -u dpa bash -c 'cd; . .profile; rtool |\
grep -f patternlist' |\
awk -v W1=30 -v W2=15 -v W3=60 -v W4=40 -v W5=10 -v LC="=" '
BEGIN {FORMAT="%-"W1"s%-"W2"s%-"W3"s%-"W4"s%-"W5"s\n"; 
printf FORMAT,"Server","Instance","Parameter","Value","Type"; 
while (L++<W1+W2+W3+W4+W5) R=R LC; print R} 
{gsub("\"","",$0); printf FORMAT,$1,$2,$3,$4,$5}'

Open in new window

0
woolmilkporcCommented:
Just for completeness, here is a solution requiring only a minimal change:

"grep" does not have to run with "sudo", so you can well move the terminating single quote from the end (after "cut -d " " -f 1,2,3,4,5") to a place near the start, right after "rtool", but before the pipe symbol. That's all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.