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

x
?
Solved

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

Posted on 2011-10-12
23
Medium Priority
?
2,735 Views
Last Modified: 2016-08-23
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
Comment
Question by:Simon336697
  • 5
  • 3
  • 3
  • +4
16 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36960030
This is like bat file or such?
Are you directing this to a file like >output.txt ?


Try

>output.txt 2>nul
0
 
LVL 1

Author Comment

by:Simon336697
ID: 36966362
Hi mplungjan.
I am not directing this to a file.
I tried your suggestion but that made no difference mplungjan.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36966433
What do you do with te output?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Author Comment

by:Simon336697
ID: 36989671
Hi mplungjan,

It just displays to the window.

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

0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 0 total points
ID: 37002442
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
 
LVL 86

Assisted Solution

by:oBdA
oBdA earned 0 total points
ID: 37002481
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
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 0 total points
ID: 37002630
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37002641
Hmm, similar to oBdA and bill there... called away before submitting, should have refreshed :-)

Steve
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 0 total points
ID: 37054350
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37054376
A rather lengthy way of doing a one line command as usual Paul...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37054471
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37054509
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
 
LVL 1

Author Comment

by:Simon336697
ID: 37073691
Guys thanks so much to all you brilliant people.
My apologies for the delay in getting back here.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37074514
Great but I surely did not give the actual answer, did I?
0
 

Expert Comment

by:Sudheer T
ID: 41766559
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 41766580
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

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Introduction to Processes
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

581 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