Showing posts with label cmd. Show all posts
Showing posts with label cmd. Show all posts

Saturday, November 29, 2014

Merge multiple lines into one based on the first field: AWK on Windows

When parsing text file, on some occasion you may need to merge different lines based on a field, in this example we will use the first field,column as the pattern or key. You can run:

sort input.txt | awk -F";" "NR!=1 && p1!=$1{print anterior;anterior=\"\"}{p1=$1;anterior=(anterior\"\")?anterior FS substr($0,index($0,$2)):$0}END{if(anterior\"\") print anterior}"

Assuming we have the next text file:
input.txt
F1;String1;String2;String3;String4
F1;String5;String6;String7;String8
F2;Text1;Text2;Text3;Text4;Text5;Text6
F2;Somethingelse
F1;Anotherthing
F2;Anotherone

In this example we want to merge lines based on the First Field of each line.
But we need to sort it first so it F1 and F2 lines are consecutive.


If your text is separated by a different character than semicolon separator replace -F";" by your separator or string. for example -F"," for a comma separated string.
It is case sensitive, if want it to be case insensitive add IGNORECASE = 1 but will not work well on some cases, if you need insenstivie case it would be better to up case or lower case all the text.


Example of merging lines based on first field, semicolon separated text

0 comments:

Post a Comment

Wednesday, November 5, 2014

Remotely obtain a list of Windows shares, physical Path and Share Permissions in CSV or Excel format

Have you ever needed to present a quick report of a Server's Shares, the Path of each share, and maybe the share permissions assigned to each share?
If that is the case you would probably needed to present it in Excel or similar format.

So Here's a way to do it, copy the next content on a .bat or .cmd:
=================================================================
del shares.txt
Set Servername=YOUR-SERVER-NAME
@ECHO OFF
for /f "Skip=4 Tokens=1 delims=:" %%p in ('rmtshare \\%servername%^|find /v /i "IPC$"^|find /v /i "The command completed successfully."^|sed "s/[ \t[a-z]]*$//I"') do (echo \\%servername%\%%p>>shares.txt)
sed -i "s/\s*[a-z]*$//I" shares.txt && del sed*.
del shares-with-quotes.txt
sed -i "s/&/^&/g" shares.txt
del sed*.
for /f "tokens=*" %%i in (SHARES.txt) do echo %%i | sed -e "s;\(.*\\\);\1"";" | sed "$s/.$/""/" >>shares-with-quotes.txt

REM lista share permissions
REM EL LOG TIENE QUE ESTAR FORMADO DE UNA FORMA ESPECIAL POR EJ \\SERVER\"SHARE"


REM ESTA LINEA SIRVE PARA MODELAR EL ARCHIVO INPUT:  for /f "tokens=*" %%i in (SHARES.txt) do echo %%i | sed -e "s;\(.*\\\);\1"";" | sed "$s/.$/""/" >>shares-with-quotes.txt

for /f "tokens=*" %%i in (shares-with-quotes.txt) do (
rmtshare %%i| sed -nE "s/^Share name *(\\\\.*)/\1/p;s/^Path *(.*)$/\1/p;/Permissions:/,/The command completed successfully/{/Permissions:\^^|The command completed successfully/!p}" | sed "{:q;N;s/\n/;/g;t q}"  | sed "s/;\\\\/;\n\\\\/g" | sed "s/;$//" >> rmtshares2.log  2>&1
)
sed -i "s/Permissions:;//g;s/ *.:. /:/g;s/        \\//g;s/;Permissions://g" rmtshares2.log
move rmtshares2.log LISTASHARE.TXT
del sed*.
=================================================================
The output as it can be see is going to be: LISTASHARE.TXT but rename it as .csv if you prefer.
It will get the list of shares from the server, and will obtain the physical path, and the permissions in CSV format so it is importable into excel and do a TEXT TO COLUMNS using semicolons. 

What executables or binaries do I need?
sed.exe (and its dependencies 3 .DLLs)
rmtshare.exe (Microsoft tool) 

You can get all together including the scripts and an additional script where you just need to fill in specifically a list of shares you want into a file named shares.txt then run otro.cmd
From here: Click here please  (it is a link from adfly, just click on the SKIP AD button which will appear after 5 Seconds). 
Then you can just click on the file which is hosted on mega.co.nz 
Important, the zip file has a password to make sure it was not edited or anything like that: 
Password: edu-naka.blogspot.com
In this file: shares-csv.zip you will find:
Content of the Shares-csv.zip

sed.exe and its dependencies 3 DLL files(unix port you may find it http://gnuwin32.sourceforge.net/packages/coreutils.htm) 
rmtshare.exe (alternative location you may find it ftp://ftp.microsoft.com/bussys/winnt/winnt-public/reskit/nt40/i386/ at least for now) 
and Two scripts: 
5.cmd: just edit the file and where it says  Set Servername=type your server name here and save it. For example  Set Servername=FileSERVER1
otro.cmd: for this all what you need to do is to fill in a text file named shares.txt with a list of share (do not leave any space that is not required for example "\\server1\share1 " it should read "\\server1\share1" (without the quotes)).

The result could look similar to this: (open or paste the content of Listashare.txt with excel). 


If you use it leave me a comment, tell me if you liked it. Try testing it as much as possible.

And thanks to my collegue friend Seba Bistakis (he gave me a piece of code with sed.exe to avoid using tr.exe).



0 comments:

Post a Comment