How to other write file using gawk script? - batch-file

I have written one code for remove duplicates from CSV file & now i want to save file with its original name. I dont want to save as with different name. Below is the batch script :
Code :
#echo off
C:\sw\awk\bin\gawk.exe "!x[$0]++" *.csv > "{print FILENAME, $0 > FILENAME ".csv"}" file*
My Objective is : I want to build dynamic batch script which will run on any CSV file. There should not be any type of dependency(file name).
Error is :
The filename, directory name or volume label syntax is incorrect.
Please help me with this.
Thanks.

This batch file is trying to redirect output from gawk into a file called {print FILENAME, $0 > FILENAME, which is not a valid filename.
At present, the gawk code produces a single output that omits all duplicate lines in all of the CSV files in the directory.
If you want to omit duplicate lines from each CSV file individually and write each file out individually, you will need a loop in the batch file to present each CSV file to the gawk script individually and output each modified file individually. You can do that as follows (presuming tmp.tmp is not an existing file in the folder):
for %%f in (*.csv) do (
gawk.exe "!x[$0]++" "%%f" >tmp.tmp
copy tmp.tmp "%%f"
)
del tmp.tmp
As a cautionary note, on DOS or Windows if you write through standard output to a file that you are reading, you can overwrite the file that you are reading before you've finished reading it. That is why the code above writes to a temporary file and then copies the temporary file to the original file.

There are a number of problems with your attempt. Firstly, you cannot write to the same file that you're reading from (at least, not while you're reading from it). Secondly, you're using the awk special variable FILENAME outside of the awk script, where it doesn't exist.
The following may work for a single file at a time. It reads the entire file in, using the line as the key of an associative array and using the line number as the value. Then in the END block, it prints the array out in order of the values, writing to the file it just read.
gawk "!($0 in a) {a[$0] = NR} END {PROCINFO[\"sorted_in\"]=\"#val_num_asc\"; for(x in a) print x >FILENAME}"

Related

What does ECHO .> mean in batch file?

I have a batch file with the following code within:
ECHO .> C:\file.txt
I read about ECHO and understand what it is used for, but what I do not know is what are the characters used for after the word echo (.>) and what is the use of the path of file after that.
It's used to truncate, or create if necessary, the file. echo . outputs a single line which is redirected to the file, effectively truncating it.
To obtain an absolutely empty file I often use cd . > filename. I don't know where I picked that one up but it's been around for a long time on UNIX systems.
> redirects the output of the command before.
echo .>c:\file.txtprints a dot to the file c:\file.txt, overwriting its contents (so it will contain a dot only afterwards)
I think, you got it wrong. Usually echo.>file.txt is used to create a empty file (or delete the contents, if the file exists). (Note the missing space)

Iterate through list of file in a list for a directory in a batch file?

I need to create CMD batch file which should have a predefined list of files, we are keeping eye on certain files, for a particualr directory. I have to iterate through this list and have to produce a result may be in a form of text file that only gives files sizes greater than 1MB.
So for instance if there are files called (a.txt,b.txt,c.txt,d.txt) and have respective lengths 900k,1.1mb,500kb and 1.5MB, then my outout file should look like
length of file b.txt > 1MB = 1.1 MB
length of file d.txt > 1MB = 1.5MB.
I need help in initializing and storing the file list in an array ,and how can i iterate through the fil list and spit out the result in a txt file.
You can pass your list of files as parameters to the script, end then use SHIFT to iterate.
call batch.cmd a.txt b.txt c.txt
a skeleton of batch.cmd :
:START
IF "%1"=="" GOTO END
REM -- CHECK FILE SIZE HERE
DO STUFF
SHIFT
GOTO START
:END

Compare output of "DOS" command to something

(by DOS I mean windows cmd.exe - I don't want to enforce powershell or similar on the end user)
I want to run a command line file that prints output to CON / the screen.
I want to capture that output and compare it to an expected output.
... in a .bat / .cmd file?
Specifically, the identify command of ImageMagick, and I want to run this over +- 300 files and compare the actual sizes to expected sizes.
example output:
$ identify rose.jpg
rose.jpg JPEG 640x480 sRGB 87kb 0.050u 0:01
If I understand the question correctly, you want to run the identify command on all the jpg files in a directory and capture the output of that command into a text file for later comparison. The comparison however is not part of the spec?
Something like the line below should do that job. Just run it from the folder the jpg files are located:
for /R %%X in (*.jpg) do identify %%X >> PicInfo.txt
This will capture the rose.jpg JPEG ... line for every .jpg file you have in the directory (and subdirectories thanks to '/R') that you run the command in and append it to the file PicInfo.txt.
You can call your identify program with a symbol that redirects console output to a file, which is the > character. Something like:
identify rose.jpg > myoutput.txt
Additionally, the >> will append output to what is already in the file. So using
identify rose.jpg >> myoutput.txt
...should create one file with all your output.
You can then use the DOS COMP command, which compares the contents of two files. The syntax is:
COMP [data1] [data2] [/D] [/A] [/L] [/N=number] [/C] [/OFF[LINE]]
Which you could also redirect to an output file using the > symbol.

DOS Batch Creating files with subfolders after reading from a list

I have a txt file which has a list of filenames with directory structure. Example below (w/o the blank lines in between):
C:\createdocs\1.txt
C:\createdocs\2.txt
C:\createdocs\mydocs\3.txt
C:\createdocs\mysubdocs\4.txt
C:\createdocs\5.txt
I want to create a batch file which will read from this file one by one and will create the file with some dummy value ("this is a test file") at the path provided at each line. If the directory doesn't exist, create the directory as well. Is it possible using the batch scripts?
In a cmd shell, you can use for /f %var in (file.txt) to process each line of the file sequentially. Then use the stuff detailed in this answer to process each line you've retrieved to extract the path. Then it's a simple matter of doing a mkdir followed by echo 'this is a dummy file' > thefile

How might I use a batch file to read the contents of one file and save it into another file?

I have created a file, file1.txt, the content of this file is like "abcdef".
I want to read the content of this file and wish to store content in another file "output.txt" using a batch file.
Please let me know how to do it from batch file.
Your batch file could simply copy the file to the new filename.
copy c:\file1.txt c:\output.txt
It sounds like you just want to copy the file, in which case you can use the following:
COPY "C:\FILE1.TXT" "C:\OUTPUT.TXT"
If that's not what you had in mind, I suggest you clarify the question or dig through the excellent reference here.
You can do this to overwrite the existing file:
type file1.txt > output.txt
You can do this to append to the existing file:
type file1.txt >> output.txt
Ruchi, your question sounds like you simply want to copy the contents of the file from 'FILE1.TXT' to 'OUTPUT.TXT', is that right? You do not want to change the file in any way? If so, there are lots of ways to do this:
#ECHO OFF
COPY C:\FILE1.TXT C:\OUTPUT.TXT
or
#ECHO OFF
TYPE C:\FILE1.TXT > C:\OUTPUT.TXT
for example.
#echo off
rem -- output file1 to file2 --
type %1 > %2
To use it, say the batch file is named output.bat, use command:
output.bat input.txt output.txt

Resources