Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

Pipe the output which has multiple line

Hi,
I try to figure out how to produce an file output which has multiple lines. Example the command IPCONFIG/ALL. I can use egrep to get the output after 'DNS Servers' onward, but once i pipe into a file and i open the file - the output is correct but one single long line.

How can i produce the exact layout as shown IPCONFIG/ALL on screen?
0
plusign
Asked:
plusign
  • 7
  • 4
  • 3
2 Solutions
 
t0t0Commented:
on the command line use,

   for /f "tokens=*" %a in ('ipconfig /all') do @echo %a >>file.txt

or the following will work in a batch file

   @for /f "tokens=*" %%a in ('ipconfig /all') do @echo %%a >>file.txt
0
 
plusignAuthor Commented:
That will be all the output of ipconfig /all. I would like only the portion of the output - from DNS Server to bottom, example:

 DNS Servers . . . . . . . . . . . : 10.1.1.1
                                     10.1.1.2
                                     10.1.1.3
 Primary WINS Server . . . . . . . : 10.2.2.2
 Secondary WINS Server . . . . . . : 10.2.2.3
 Lease Obtained. . . . . . . . . . : Thursday, 12 February 2009 7:18:06 PM

 Lease Expires . . . . . . . . . . : Sunday, 22 February 2009 7:18:06 PM
0
 
t0t0Commented:
give me a minute or two to sort it....
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
AmazingTechCommented:
Try this.

If you want to trim the leading spaces use this:

for /f "Tokens=1 delims=[]" %%a in ('ipconfig/all ^| find /n /i "DNS Servers"') do Set /a Line=%%a-1

for /f "skip=%Line% Tokens=*" %%a in ('ipconfig/all') do echo %%a
for /f "Tokens=1 delims=[]" %%a in ('ipconfig/all ^| find /n /i "DNS Servers"') do Set /a Line=%%a-1
 
for /f "skip=%Line% Tokens=* delims=" %%a in ('ipconfig/all') do echo %%a

Open in new window

0
 
AmazingTechCommented:
Oh. Right you want it in a file.

Leading spaces trimmed

for /f "Tokens=1 delims=[]" %%a in ('ipconfig/all ^| find /n /i "DNS Servers"') do Set /a Line=%%a-1

(for /f "skip=%Line% Tokens=*" %%a in ('ipconfig/all') do @echo %%a)>Output.txt
for /f "Tokens=1 delims=[]" %%a in ('ipconfig/all ^| find /n /i "DNS Servers"') do Set /a Line=%%a-1
 
(for /f "skip=%Line% Tokens=* delims=" %%a in ('ipconfig/all') do @echo %%a)>Output.txt

Open in new window

0
 
plusignAuthor Commented:
thanks, let me try it
0
 
plusignAuthor Commented:
ehmm...how it supposes to work? I believe that's one long script command, but i got the result = 25?

I miss something?
0
 
AmazingTechCommented:
Save the file to something like DNSinfo.bat and then run it from the command line. A output.txt file should be generated with your DNS info and below. This is what it looks like from my command line.
Can you post yours if there's still a problem change the IP addresses before you post though.

C:\AT>dnsinfo

C:\AT>for /F "Tokens=1 delims=[]" %a in ('ipconfig/all | find /n /i "DNS Servers
"') do Set /a Line=%a-1

C:\AT>Set /a Line=46-1

C:\AT>(for /F "skip=45 Tokens=* delims=" %a in ('ipconfig/all') do @echo %a ) 1>
Output.txt

C:\AT>type output.txt
        DNS Servers . . . . . . . . . . . : 12.7.23.86
                                            12.7.5.13
                                            12.7.11.21
        Primary WINS Server . . . . . . . : 12.7.23.160
        Secondary WINS Server . . . . . . : 12.7.5.12
        Lease Obtained. . . . . . . . . . : Tuesday, February 17, 2009 7:07:03 A
M
        Lease Expires . . . . . . . . . . : Tuesday, February 24, 2009 7:07:03 A
M
0
 
t0t0Commented:
Please try this....


@echo off
setlocal enabledelayedexpansion
set line=0
for /f "Tokens=1 delims=[]" %%a in ('ipconfig/all ^| find /n /i "DNS Servers"') do if !line!==0 Set /a Line=%%a-2
(for /f "skip=%Line% Tokens=* delims=" %%a in ('ipconfig/all') do echo %%a)>ipconfig.tmp
(for /f "tokens=* delims=" %%a in (ipconfig.tmp) do if not "%%a"=="ECHO is off." echo %%a)>ipconfig.txt
del ipconfig.tmp
0
 
t0t0Commented:
AmazingTech...

Would you agree his is a weird one?

The output of ipconfig inserts a carriage return (ascii 13) character just prior to the normal carriage return / linefeed conbination at the end of each line, including blank lines. I would guess this is a programmer's error on the part of Microsoft and one which has gone unnoticed.

This would explain why the author states the output of egrep is a single continueous text line.

In dos, it may be possible to use character substitution to delete the extra carriage return character using ALT-Number Pad combinations in a text file such as:

   ...
   set text=%%a
   set text=!text:<CR><CR><LF>=<CR><LF>!
   echo !text!>>ipconfig.txt
   ...

where <CR> and <LF> are ALT-13 and ALT-10 respectively typed on the numerical keypad. If EE displays this correctly, this would appear as:

   set text=!text:jjÙ=jÙ!

Notice the '0D 0D 0A' combinations in the attached image file.

This would also explain why you can't do the following:

   if not "%%a"=="" echo %%a

because %%a will contain at least a carriage return character however, the follwoing may work (I haven't tried it though):

   if not "%%a"=="j" echo %%a
0
 
t0t0Commented:
AmazingTech.....

Strange, in my previous post, EE has converted ascii 13 as 'j' and ascii 10 as U (with an accent)
0
 
t0t0Commented:
AmazingTech....

Sorry, I forgot to attach the image file...
ipconfig.txt.bmp
0
 
t0t0Commented:
plusign

As you can see from the above conversation with AmazingTech, there is no simple way to output ipconfig /all as easily as other commands. This is why AmazingTech has decided to omit all redundant white spsce.

There are basically 3 options here:

1) output contains no redundant white space however, text is not left-aligned correctly
2) output retains blank lines and also retains left-alignment
3) additional processing is required to filter the file to produce correct output

AmazingTech has chosen option 1 whereas, I have chosen option 2 with the following:

:
:
@echo off
setlocal enabledelayedexpansion
 
set line=0
for /f "Tokens=1 delims=[]" %%a in ('ipconfig/all ^| find /n /i "DNS Servers"') do if !line!==0 Set /a Line=%%a-2
(for /f "skip=%Line% Tokens=* delims=" %%a in ('ipconfig/all') do echo %%a)>ipconfig.tmp
(for /f "tokens=* delims=" %%a in (ipconfig.tmp) do if not "%%a"=="ECHO is off." echo %%a)>ipconfig.txt
del ipconfig.tmp
:
:

Open in new window

0
 
AmazingTechCommented:
Hmm... I'm not too sure what's going on.

I've tried your code and my code from the site and they both work for me.

I copied and pasted in Notepad saving as ANSI format.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now