basically I want to run the following command on every text-file automatically:
awk -f myScript.awk file1.txt > new\file1.txt
awk -f myScript.awk file2.txt > new\file2.txt
...
Then move the processed files to the folder \old.
move *.txt \old
should work for that part.
How do I create the correct for-loop, so that the output of the awk program has the same name as the input, just in the new folder?
OK, try this:
for %%i in (*.txt) do awk -f myScript.awk "%%~fi" > "new\%%~nxi"
Related
I have the following problem:
I'm trying to write a script where two files (file1.txt and file2.txt) should be combined into 1 file with a text passage in between. The output should be written in another file (e.g. output.txt).
The output.txt file should be:
[content of file1.txt]
text passage
[content of file2.txt]
After some research on the internet I found the following and it works fine in the terminal:
cat file1.txt <(echo "text passage") file2.txt > output.txt
However, it does not work in my script:
#!/bin/sh
cat file1.txt <(echo "text passage") file2.txt > output.txt
If I execute the script nothing happens (the output.txt isn't written).
Why doesn't this line work in a script and what can I do to make it work?
Thank you for your help!
Stephan
You can just do this:
cat file1.txt > output.txt
echo "Text message" >> output.txt
cat file2.txt >> output.txt
the >> operator means add it to the end of the file, rather than overwriting the contents.
You can also group the commands using brackets:
(cat file1.txt
echo "Text message"
cat file2.txt) > output.txt
Please feel free to rename the question to something more appropriate.
How would I mimic the below zsh using bash instead?
mkdir folder1
mkdir folder2
mkdir folder3
# zsh
folders=(folder*) | print $folders
#folder1 folder2 folder3
# bash
folders=(folder*/) | echo $folders
#folder1
As you can see, this only outputs the first element.
Any pointers would be appreciated, thanks.
Try changing it to:
folders=(folder*); echo "${folders[#]}"
folders[#] gives all the elements in the array
${} expands the output from above command to bash.
If lets say, you have multiple .txt file in some Directory and you want to get/display those folders . you can try something like this:
declare -a folder_arr
i=0
for dir in *.txt; do
folder_arr[i]=$dir
i=$((i+1))
done
for j in $(seq 0 $((i-1)))
do
echo ${folder_arr[$j]}
done
I excuted the above file and was able to get the expected reult.
/temps$ ./dirrr.sh
z.txt
I would like to use a batch file to put them into default folder, but the account name is in the middle of the folder. Have any script I can use in dos command prompt?
888123AA.pdf
888123BB.pdf
888123CC.pdf
777456AA.pdf
777456BB.pdf
777456CC.pdf
Default folder:
999-888123-03
666-777456-01
#echo off
setlocal EnableDelayedExpansion
rem Process all .pdf files
for %%a in (*.pdf) do (
rem Get just the file name, ie: "888123AA"
set fileName=%%~Na
rem Using the file name minus two last chars, ie: "888123"
rem get the default folder with that name
for /D %%b in (*-!fileName:~0,-2!-*) do (
rem And copy the file to that folder
copy "%%a" "%%b"
)
)
I don't remember any apparent way to do it other than a UNIX shell... Maybe get MSYS and use that (outdated) bash to help?
Here is a bash script that can use after you installed bash from MSYS (or you can sort it with a Linux box - Ubuntu is no bigger than 800MB and can run as LiveCD without interfering your current Windows system, and the LiveCD can double as a system saver when needed. :-)
#!/bin/bash
for each in ./*; do
if [ -d $each ]; then # Only folders are minded.
# Extract the second part of the folder name.
ACCOUNT_NAME=`echo $each | sed "s/\\-/\n/" | head -n 2 | tail -n 1`
cp -v ./$ACCOUNT_NAME*.pdf $each
fi
done
I want to take all of the files in /media/mdrive/dump/:
1COD-234355.jpg
MAK-LXT218.jpg
ZIR-CON145.jpg
And create and sort them into the following directories:
/media/mdrive/dump/1/1COD-234355.jpg
/media/mdrive/dump/M/MAK-LXT218.jpg
/media/mdrive/dump/Z/ZIR-CON145.jpg
How would I do that?
This script takes a directory as the first argument and performs what you need:
#!/bin/bash
DIR="$1"
if [ -z "$DIR" ]; then
echo >&2 "Syntax: $0 <directory>"
exit 1
fi
if [ ! -d "$DIR" ]; then
echo >&2 "\"$DIR\" is not a directory"
exit 1
fi
cd "$DIR"
for file in *.jpg *.JPG; do
first=${file::1}
mkdir -p $first && mv $file $first/;
done
head -c xx will return the first xx characters of its input (here, the filename). mkdir -p will skip directory creation if it already exists.
to make two directories you could try something like
dir "/media/mdrive/dump/1/" :: CD would also work here
mkdir folder 1
mkdir folder 2
from here I think you can continue with your IF statements and so forth.
all you need to do is set the dir commands with the Direct path takes the guess work out.
then to check each just do:
start explorer.exe "the folder's path here"
it should open the folder to view the files
I am trying to download a chunk of files from an application. The shell command for it is 'go filename download'.
I have a text file containing all the filenames I have to download. All I want to do is to run a script/command such that when the above command is executed
1. the filenames are picked up from the textfile & executed using the above command
2. existing files/unavailable files are skipped
3. the process then continues with the next files in the list
So far I have this idea of using an operator like go $ download & then feed the operator with the text file containing the filenames list. Thanks in advance.
For Windows, you can use for /f to process the file and create a command from it. The following script supergo.cmd shows how this can be done:
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "delims=" %%f in (list.txt) do (
echo go "%%f" download
)
endlocal
The following transcripts shows it in operation:
C:\Pax> type list.txt
file1.txt
file number 2.txt
another file.jpg
C:\Pax> supergo
go "file1.txt" download
go "file number 2.txt" download
go "another file.jpg" download
If you're using a shell like bash, you can use sed to create a temporary script from the input file then run that:
#!/bin/bash
sed -e "s/^/echo go '/" -e "s/$/' download/" list.txt >/tmp/tempexec.$$
chmod u+x /tmp/tempexec.$$
. /tmp/tempexec.$$
rm -rf /tmp/tempexec.$$
This puts an echo go ' at the start of each line, a ' download at the end, then marks it executable and executes it.
In both cases (Windows and bash), remove the echo to get it to do the real work.