Solved

Bash code too ugly to use

Posted on 2014-11-13
8
198 Views
Last Modified: 2014-11-18
I want to take commands that give multline output, add lines to them and bracket them with quotation marks as the commas.

if you look at the output of this code in excel you will see the number of lines  then the output of the mount command in each cell of an excel csv spreadsheet.

I would like to be able to make the contents of cell and then printf "%s" them

Here is my ugly and confusing attempt

#!/bin/bash


function yada {
echo "MountCount=`mount | wc -l`"
mount
}


printf "%s" "\"`yada`\"",
printf "%s" "\"`yada`\"",
0
Comment
Question by:TIMFOX123
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 40441662
Can you please give an example of your desired output?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40441730
printf '"%s",' "`yada`"
?
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40442441
Do you want a csv file of the 'mount' output?
0
 

Author Comment

by:TIMFOX123
ID: 40443737
Sorry for being so slow to respond, you folks are great

Here is the output and a screen shot of what I want it to look like in excel

The reason I want pretty code ( easy to follow) is I have to maintain it as well as I tend to use the same code many times

acceptance of confusing code by the team is not going to happen,
mud.csv
sshot.png
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40444212
Replace your function with this to get the required output:
echo \"Mountcount=$(mount | wc -l)\"
mount | sed 's/^/"/;s/$/"/'

Open in new window

0
 

Author Comment

by:TIMFOX123
ID: 40444686
I have to say that is  much more elegant solution however that is quoting every line.

I was hoping to have quotes only at the beginning and end.  

If you open this in excel you would likely see the difference.
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
ID: 40444785
I see, try last line like this:

echo \"  $(mount) \"

(mobile, not tested)

<edit>
This works:
echo \"Mountcount=$(mount | wc -l)\"
echo \"$(mount)\"

Open in new window


Or one one line:
echo \"Mountcount=$(mount | wc -l)\";echo \"$(mount)\"

Open in new window

0
 

Author Closing Comment

by:TIMFOX123
ID: 40450093
Great job,  

I had to do a little mod on this one so I figure I would post it

yada="\"$(echo -n "MountCount=";mount | wc -l; mount  )\","


it was still your solution, I just had to clean some up so excel was happy
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now