• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2805
  • Last Modified:

Removing first and last lines, and blank spaces at the start or each line.

Hi guys hope you are all well.

Guys im trying to work out how to remove from the output,
1)The first and last lines eg.samid and dsget succeeded
2)The blank spaces at the start of each line.

D:\>dsquery group "<Full DN" | dsget group -samid

Produces the following output..

  samid
  Member Servers - Printer Operators
  Notes Server - Restore Folder Full Control
  a_bcs
  a_helpdesk
  a_sas
  Accounts - Unlock users
dsget succeeded

So that the output looks like below..

Member Servers - Printer Operators
Notes Server - Restore Folder Full Control
a_bcs
a_helpdesk
a_sas
Accounts - Unlock users


Any help greatly appreciated.
0
Simon336697
Asked:
Simon336697
  • 5
  • 3
  • 3
  • +4
4 Solutions
 
Michel PlungjanIT ExpertCommented:
This is like bat file or such?
Are you directing this to a file like >output.txt ?


Try

>output.txt 2>nul
0
 
Simon336697Author Commented:
Hi mplungjan.
I am not directing this to a file.
I tried your suggestion but that made no difference mplungjan.
0
 
Michel PlungjanIT ExpertCommented:
What do you do with te output?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Simon336697Author Commented:
Hi mplungjan,

It just displays to the window.

dsquery group "<Full DN" | dsget group -samid

0
 
Bill PrewCommented:
Here's a BAT file you can try to see if it does what you want.  Try it as is, but you may need to change:

for /F "skip=1 tokens=*" %%A in ('dsquery group "<Full DN" ^| dsget group -samid') do (

to

for /F "skip=1 tokens=*" %%A in ('dsquery group "^<Full DN" ^| dsget group -samid') do (

to handle the special character.

@echo off
setlocal EnableDelayedExpansion

for /F "skip=1 tokens=*" %%A in ('dsquery group "<Full DN" ^| dsget group -samid') do (
  if /I "%%A" NEQ "dsget succeeded" (
    call :Trim %%A
    echo %T%
  )
)
goto :EOF

:Trim
  set T=%*
  exit /b

Open in new window

~bp
0
 
oBdACommented:
Command line:
for /f "tokens=1,2" %a in ('dsquery group "<Full DN" ^| dsget group -samid -L') do (if "%a"=="samid:" echo %%b)

Open in new window

Batch:
for /f "tokens=1,2" %%a in ('dsquery group "<Full DN" ^| dsget group -samid -L') do (if "%%a"=="samid:" echo %%b)

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
From a command line:

(for /f "skip=1 tokens=*" %a in ('dsquery group "Full DN" ^| dsget group -samid ^| find /i /v "dsget succeeded"') do echo %a) > output.txt

or double up the % to %% from a batch file.

Steve
0
 
Steve KnightIT ConsultancyCommented:
Hmm, similar to oBdA and bill there... called away before submitting, should have refreshed :-)

Steve
0
 
Paul TomasiCommented:
Perhaps I can assist!

Copy the code and save it as a batch file. When run, the batch file executes your dsquery command sending it's output to a temporary file.

The output is processed and the following is displayed on the screen.

Member Servers - Printer Operators
Notes Server - Restore Folder Full Control
a_bcs
a_helpdesk
a_sas
Accounts - Unlock users


Also, be aware that you can direct this output to another file if you so wish to do so as in the following:

   BATCHNAME >filename

Or as input to another command like this:

   BATCHNAME | command


See the code here:
@echo off
setlocal enabledelayedexpansion

dsquery group "<Full DN" | dsget group -samid >file.tmp

(for /f "tokens=1,2* delims=[] skip=2" %%a in ('find /v /n "" file.tmp') do (
  set count=%%a
  set line=%%b
  call :LTrim line
  echo.!line!
))>trim.tmp

for /f "tokens=1,2* delims=[] skip=2" %%a in ('find /v /n "" trim.tmp') do (
  if %%a gtr 1 if %%a lss !count! echo.%%b
)

del file.tmp trim.tmp 2>nul
exit /b


:LTrim
  set str=!%~1!
  if "!str!"=="" goto :eof

  :loop
    if not "!str:~0,1!"==" " (
      set %~1=!str!
      goto :eof
    ) else (
      set str=!str:~1!
    )
  goto :loop

Open in new window

~pt
0
 
Steve KnightIT ConsultancyCommented:
A rather lengthy way of doing a one line command as usual Paul...
0
 
Paul TomasiCommented:
Well spotted...

I didn't notice the sample data until I finished coding.

Not familiar with his command so hedged my bets and wrote output to file.

What was crucial to me during coding were:

   - spaces on the right are preserved (only LEFT spaces specified in question)

   - lines consisting wholly of spaces are trimmed to blank lines

   - blank lines are preserved

   - number of text lines in file are therefore, preserved

   - leading spaces trimmed from start of lines

   - first line removed

   - last line removed

So there's a real lot going on here... and well beyond the scope of a one-liner!
0
 
Steve KnightIT ConsultancyCommented:
Nope.  One line is fine.

It is a standard AD query output.

He just wants to get rid of the first and last lines and the spaces, like he said.... there won't be any other spaces in the output, or blank lines, and the first and last lines will be as shown.

So one line suffices.... or Bill Prew's, or oBdA's which do the same but with a wrapper around.

Bed calls.

Steve
0
 
Simon336697Author Commented:
Guys thanks so much to all you brilliant people.
My apologies for the delay in getting back here.
0
 
Michel PlungjanIT ExpertCommented:
Great but I surely did not give the actual answer, did I?
0
 
Sudheer TCommented:
Hi paultomasi,

@paultomasi , your code work fine but how to remove the trailing spaces from the output file. Seems like we have 3 spaces at the end. Also, request you to tell me how to output to a CSV file with a comma at the end to this output. Could you please help me in this aspect ASAP.

Current output :
Name1  
Name2  
Name3  
Desired output:
Name1,
Name2,
Name3,
0
 
Steve KnightIT ConsultancyCommented:
Picking a 5 year old question to add a comment to probably not best way to get an answer.  Suggest make a new question and add a link to this one.

Steve
0
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.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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