Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 06-11-2010, 08:52 AM   #1
LQ Newbie
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
Manipulate text file to allow import to excel

Dear All,

Have managed to create a simple script to poll the various printers we have at our company for a page count via SNMP. At the moment the script echos the printer name to a text file and then the snmp output is appended to the next line. In my rush I thought by using sed to add a comma to the end of each line would mean it would import in to excel nicely. So rather than having:

Printer 1,
Printer 2,

I need:

Printer 1, 45536,
Printer 2, 28765,

Is there a way to do this easily via sed? Or is there a way to get the command line to add the echo and the command output on to the same line of the text file?

currently the script looks like this:

echo Accounts > printers.txt
snmpget -v 1 -c public **.**.**.** SNMPv2-SMI::mib- >> printers.txt
echo Accounts Colour >> printers.txt
snmpget -v 1 -c public **.**.**.** SNMPv2-SMI::mib- >> printers.txt
Thanks in advance!
Old 06-11-2010, 10:46 AM   #2
Registered: Aug 2008
Location: Denmark
Distribution: Fedora, Ubuntu, Solaris
Posts: 35

Rep: Reputation: 19

You can try adding -n option to echo.

or to fix using sed

sed 'N;s/\n/ /' printers.txt
Both tested on Fedora 13. This is assuming a format off:

Printer 1,
Printer 2,
I do not know where your text off "Accounts" and "Accounts Colour" would appear in that file, but it will break that sed option. I'm sure that someone will post a better sed option that does some check on actual text and not just blindly join 2 lines.

If I have more time later I'm try and find a better solution.

Old 06-11-2010, 03:22 PM   #3
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 129Reputation: 129
More complex than sed, but a solution would be to use a counter and the modulo of the current iteration to add ,\n

something like this:

RESULT=$RESULT`snmpget -v 1 -c public **.**.**.** SNMPv2-SMI::mib-`
RESULT=$RESULT"Accounts Colour"
RESULT=$RESULT`snmpget -v 1 -c public **.**.**.** SNMPv2-SMI::mib-`

for LINE in $RESULT;do
   if [ `expr $ITERATOR % 2` -gt 0 ]; then

echo -e"$OUTPUT" > printers.csv
It will take a little time to debug before it work, but the principals should work. You may have to split both sections (Accounts and color) to make it work fine, or add an extra empty line after section title
Old 06-14-2010, 08:50 AM   #4
LQ Newbie
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks for that. Your command IW2B works like a charm. unfortunately not all my snmp commands have been answered so there is no output in the text file. this makes all the lines get muddled up! Think I am going to have to go and learn some if - for -else scripting and try and fiddle a null input for no response etc. knew it would turn out more complicated then i thought it would be originally.


manipulation, scripting, text

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Way to manipulate a text file.. php ? 8rucech85 Programming 2 01-30-2008 05:35 AM
how to import users in qmail from text file Braytac Linux - Software 5 12-24-2006 10:43 AM
convert text file to binary excel file ust Linux - General 2 11-23-2004 03:33 AM
how to import MS-Excel sheet data using PHP dileepkk Programming 1 07-28-2004 03:23 PM
Editing text file for import to a spread/database Franklin Programming 7 03-31-2004 07:31 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:04 PM.

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