Bash script to grep through one file for a list names, then grep through a second file to match those names to get a lookup value |
awk -v search="$search_string" '$0 ~ search { gsub(/"/, "", $5);
print $1" "$5; }' "$filename"
|
while read line
do
result=$(awk -v search="$line" '$0 ~ search { print $3; } '
"$lookup_file");
# Do "something" with $result
done
|
finding most recent file version from list of file path names with jumbled file names |
you can try:
find $WORK.../.history -type f -printf '%T@ %p
' | sort -nr | cut -f2- | xargs grep 'your_pattern'
Decomposed:
the find finds all plain files and prints their modification time and path
the sort sort sort them numerically - and reverse, so highest number comes
first (the latest modified)
the cut removes the time from each line
the xargs run its argument for each file what get to it input,
in this case will run the grep command, so
the 1st file what the grep find - was the lastest modified
The above not works when the filenames containing spaces, but hopefully
this is not your case... The -printf works only with GNU find.
For the repetative work, you can split the command to two parts:
find $WORK.../.history -type f -printf '%T@ %p
' | sort -nr | cut -f2- > /somewhe
|
linux Bash - read contents of file , store them in a variable and create network config file |
I tried yesterday and I am very close to the solution. Researching the
internet , I made a perl script and was able to create multiple interface
by extracting data from csv file.
But there is small issue I see and not able to figure out why an extra
character is coming in the file.
Following is the code:
#!/usr/bin/perl
use strict;
use warnings;
sub main
{
# Note: this could be a full file path
my $filename = "file1.csv";
open(INPUT, $filename) or die "Cannot open $filename";
# Read the header line.
#my $line = <INPUT>;
my $line;
# Read the lines one by one.
while($line = <INPUT>)
{
chomp($line);
# Display the header, just to check things are working.
|
Bash shell read names of files from folder and create an output file for each input file with the same name: |
The underscore is a valid character in variable names.
Do:
./"$f" > "${f}_july_15.txt"
Your code was trying to use a variable named f_july_15. You need to use
braces to delimit the variable name when it's followed by a character that
can be part of a variable name.
|
bash script to write content into file. File content requires bash variables. How to do that? |
You're actually deliberately turning off parameter subsitution by enclosing
'ENDFILECONTENT' in quotes. See this excerpt from example 19-7 of the
advanced Bash scripting guide on Heredocs, slightly reformatted:
# No parameter substitution when the "limit string" is quoted or escaped.
# Either of the following at the head of the here document would have
# the same effect.
#
# cat <<"Endofmessage"
# cat <<Endofmessage
Remove the single quotes around 'ENDFILECONTENT' and BASH will replace the
variables as expected.
|
Allowing punctuation characters in directory and file names in bash |
Always quote your variable substitutions. I.e. not cp $source $target, but
cp "$source" "$target". This way they won't be subject to word splitting
and pathname expansion.
Specify "--" before positional arguments to file operation commands. I.e.
not cp "$source" "$target", but cp -- "$source" "$target". This prevents
interpreting file names starting with dash as options.
And yes, "/" is not a valid character for file/directory names.
|
Bash script to list all IPs in prefix |
I think this little script I hacked together does the trick. If not, it's
definitely a starting point! Good luck.
#!/bin/bash
############################
## Methods
|
Bash / Regex Parsing only Domain Names In File containing both Domans and Email Addresses |
if your file contains only domain and email addr, try this line:
grep -v '@' file
test with your example:
kent$ echo "john3525@aol.com
brenda392@yahoo.com
xyx.net
boxers.on.ca
helloworlds.co.uk"|grep -v '@'
xyx.net
boxers.on.ca
helloworlds.co.uk
|
Create output file names based on input file name with autonumbers shell script linux |
VAR="INPUTFILENAME"
# One solution this does not use the VAR:
touch INPUTFILENAME{1,2,3,4,5,6,7,8,9,10}
# Another
for i in `seq 1 20` ; do
touch "${VAR}${i}"
done
And there are several other ways.
|
Whats wrong with this list of servers in my bash script? |
I don't know about the syntax error, but this should work:
SERVERS="blah1.example.com blah2.example.com blah3.example.com
blah4.example.com"
for server in $SERVERS
do
echo $server
done
EDIT: As noted by Jonathan Leffler in a comment, maybe you are not running
the script with bash. Other shells, such as dash, may not recognize the
array syntax. If that's the case, you can do:
SERVERS=(blah1.example.com blah2.example.com blah3.example.com
blah4.example.com)
for i in $(seq 0 3)
do
echo ${SERVERS[$i]}
done
But if you just want to loop through the names and run an SSH command (ie
if having an array won't provide useful functionality), the first method is
more straightforward.
|
Configuring config/database.php file using php script |
An approach would be to have 'template file' in your installation folder
like:
ex. database.php
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '<?php echo $hostname;?>';
$db['default']['username'] = '<?php echo $username;?>';
$db['default']['password'] = '<?php echo $password;?>';
$db['default']['database'] = '<?php echo $database;?>';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '<?php echo $prefix;?>';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']
|
How to write a bash script that can parse through an XML file as input and remove certain elements from the file |
Perhaps you could use php like this: running php script (php function) in
linux bash
And then you something like
domdocument(http://php.net/manual/en/class.domdocument.php) to read and the
right the xml.
Of course this is assumes you have php installed.
|
Pass content of file in variable into bash script - without writing the file down |
You need one more <:
./script -a <<<"$CONTENT"
<<< is called a herestring, and takes the following string and
passes it as the standard input.
|
How to use external file as a source of variables for Bash script file? |
Save this to a file e.g. script.sh.
#!/bin/bash
declare -A A
while read -ra __; do
A[${__[0]}]=${__[1]}
done < "$2"
while read -r __; do
for I in "${!A[@]}"; do
[[ $__ == *"$I"* ]] && {
V=${A[$I}
__=${__//"$I"/"$V"}
}
done
echo "$__"
done < "$1"
Then run bash script.sh aaa.txt bbb.txt.
To get the output run it as something like bash script.sh aaa.txt bbb.txt
> out.txt
I prefer to use Bash this time since you have to quote non-literals in Awk.
Another solution through Ruby:
ruby -e 'f = File.open(ARGV[0]).read; File.open(ARGV[1]).readlines.map{|l|
l.split}.each{|a| f.gsub!(a[0], a[1])}; puts f' aaa.txt bbb.txt
Another way to modify file directly:
#!/bin/bash
A=$(<"$1")
while read -ra B; do
A=${A//"${B[
|
Using shell script to pull strings out of a config file |
# with /bin/bash
. <(awk '/define/ {print $2"="$4}' FS="'" foo.php)
# with /bin/sh
declare `awk '/define/ {print $2"="$4}' FS="'" foo.php`
Result
echo $DB_SERVER # localhost
Example
|
Mac launch daemon keeps loading the firewall shell script |
Use LaunchOnlyOnce key in plist file.
LaunchOnlyOnce :
This optional key specifies whether the job can only be run once and
only once. In other words, if the
job cannot be safely respawned without a full machine reboot, then
set this key to be true.
|
difference between executing script using bash file.sh and ./file.sh |
If "file.sh" is not executable then ./file.sh will not work but bash
file.sh will.
If "file.sh" does not start with the line #!/bin/bash (or another path to a
valid bash interpreter) then ./file.sh will not work but bash file.sh will.
Basically, in order for a script to look like an executable file it must:
Have execute permission.
Start with the line #!/path/to/interpreter.
|
zsh: how to autocomplete file names after python script name? |
Honestly, IMO something in your shell configuration is breaking things up.
Try doing this to verify it:
zsh -f # starts a new shell ignoring your configuration
autoload compinit
compinit
./my-shell-script [TAB]
it completes with files. That is the default.
FWIW, if you want to bind a particular completer to a command/alias etc,
you can do
compdef _jstack jstack
# simple _files completion
compdef _files my-local-python-script
# restrict to some file extensions
compdef '_files -g "*.(eps|ps|pdf)"' okular
|
Bash: How to use a secret file for a script? |
When i had to do something like that, i used expect and a wrapper script
that would fetch a password from a file.
I.e. in my password file i'd have something like
root@192.168.1.10 ThisIsMyPass
user@localhost thisIsMyOtherPass
and then have the wrapper script get (it could be simple as grep
"root@192.168.1.10" ~/.passwords | cut -d ' ' -f2)
Im guessing there are more appropriate methods, but with this one you only
need to keep your wrapper and password file protected and you can make your
setup script public.
|
parsing an .kml file in bash script |
With bash if your lines are in order:
while read WHEN && read GXCOORD; do
echo "$WHEN : $GXCOORD"
done < test.kml
Or perhaps do it this way:
while read WHEN && read GXCOORD; do
echo "$WHEN : $GXCOORD"
done < <(exec grep -Fe "<when>" -e "<gx:coord>" test.kml)
And also perhaps trim out the tags:
while read WHEN && read GXCOORD; do
WHEN=${WHEN##*'<when>'} WHEN=${WHEN%%'</when>'*}
GXCOORD=${GXCOORD##*'<gx:coord>'}
GXCOORD=${GXCOORD%%'</gx:coord>'*}
echo "$WHEN : $GXCOORD"
done < <(exec grep -Fe "<when>" -e "<gx:coord>" test.kml)
|
Crontab Python Script Execution (Can't find settings config file) |
Your code is looking for settings.cfg in its current working directory.
This working directory will not be the same when cron executes the job,
hence the error
You have two "easy" solutions:
Use an absolute path to the config file in your script
(/home/tomi/amaer/config.cfg)
CD to the appropriate directory first in your crontab (cd /home/tomi/amaer/
&& python /home/tomi/amaer/controller.py)
The "right" solution, though, would be to pass your script a parameter (or
environment variable) that tells it where to look for the config file.
It's not exactly good practice to assume your config file will always be
lying just next to your script.
You might want to have alook at this question:
http://unix.stackexchange.com/questions/38951/what-is-the-working-directory-when-cron-exe
|
How can I write into a particular line of a file in a bash script? |
You could modify the line with sed.
catcenter.dat
sed -i "/CenterListName/ s/=$/= $(cat center.dat)/"
AnalyzeClusterParameterFile$c
Read the value first and add it to the second file with sed.
|
linux - bash script - test a file |
Not precisely. -N does a comparison between the file's atime and mtime,
which is not accurate on e.g. ext3 filesystems that are mounted relatime.
You should either use the OS's file monitoring facilities or compare the
mtime of the file directly.
|
How to run a command within a bash script which gets it's arguments from a csv file |
while read -r account abelpass entapass
You are reading abelpass & entapass but passing password1 and password2
! Didn't you meant to use
while read -r account password1 password2
?
|
Javac in bash script returns no such file |
You are not passing the actual file to compile - instead you're passing the
.java file as the classpath. Your actual command looks something like
this:
javac -classpath /some/dir/Program.java
while it should be something like this:
javac -classpath /path/to/library.jar /some/dir/Program.java
I think you're missing the actual classpath somewhere. Most likely your
script needs to be similar to this:
#!/bin/bash
CWD=$(pwd)
CLSPATH=/path/to/library.jar
SOURCE="$CWD/Program.java"
COMMAND="javac -classpath $CLSPATH $SOURCE"
eval $COMMAND
java Program
Also, I'm not sure what the purpose of the eval approach is. Why not just
invoke javac? I would further modify the script to be:
#!/bin/bash
CWD=$(pwd)
CLSPATH=/path/to/library.jar
SOURCE="$CWD/Program.java"
javac -classpath $CLSPATH $
|
Bash script for regex file renaming |
Pure bash based solution:
for x in *.pickle; do
[[ $x =~ ^([^_]+_[^_]+_)([^_]+)(_.+)$ ]] && mv "$x"
"${BASH_REMATCH[1]}$((${BASH_REMATCH[2]} + 1))${BASH_REMATCH[3]}"
done
|
Create answer file for bash script |
This basic trick may work in your case:
echo -e "y
y
y
y
n" | ./script
or
echo -e "yes
yes
yes
yes
no" | ./script
but it depends on how your script handles the input stream...
|
Bash Script to copy contents from one file to another |
You can remove files from the argument list as you use them, with the shift
command:
if (( $# % 2 )); then
echo Please enter an even number of filenames
exit 1
fi
while (( $# )); do
src=$1
dst=$2
cp "$src" "$dst"
shift 2
done
|
How to paste many lines to a file using a bash script? |
I would use cat together with here-doc syntax for this:
cat <<EOF > /etc/nsswitch.conf
group: compat
shadow: compat
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
...
EOF
The statement above will overwrite or create the file with the contents
between the first line and EOF. In the form above even variables like
group: $group would be expanded by bash. If you don't want this, then use
<<'EOF' (note the single quotes ' around the EOF)
|
Edit a text file using a Bash script |
try this:
awk '/localhost> localhost>/{print $1
>"file2";next}{$2=$3=$NF=""}7' file
this will output text you want, and generate a "file2" for the
localhost> localhost> case.
didn't test, I hope there is no typoes.
|
Edit File in bash script by sed command |
Looks to me like those sed commands arguments are not on the same line and
line shift is not escaped. Try:
sed -e "s/$SEEDVALUE/$RANDOMVAR/"
-e "s/$EXPIREMENT/$QUAN/"
-e "s/$DISTANCE/$DIST/"
-e "s/$OUTPUTFILE/$outputText/"
< $TEMPLATEFILE > $outputFile
Also, to get more debugging information, it's often useful to use the
following as interpreter:
#!/bin/bash -x
|
How to solve "bash: script/extension: No such file or directory" |
It's because this plugin is for old version of spree which was for Rails
2.x and this command syntax is deprecated by now. I think it would be
better for you to use something more up-to-date, like
https://github.com/nebulab/spree-subscriptions.
|
Read txt file and parse the values to bash script |
The following would give you a list of packages for which you want the
reports:
grep "^packages" config.txt | cut -d= -f2 | tr ',' ' '
Based on this, you can loop for values in the list:
filename="config.txt"
for i in $(grep "^packages" $filename | cut -d= -f2 | tr ',' ' '); do
for file in $(find /home/user/ftpuser -maxdepth 1 -name "*.[ew]ar" -type
f); do
echo /apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar
../windup-cli-0.6.8/windup-cli.jar -javaPkgs ${i} - input ../ftpuser/
-output ../reports/ "${file}"
cp "${file}" /home/user/ftpuser/scanned/
done
done
|
bash script to get modified file on remote server |
Change:
if ["$(( $(date +"%s") - $(stat -c "%Y" $i) ))" -gt "259200" ]; then
to:
if [ "$(( $(date +"%s") - $(stat -c "%Y" $i) ))" -gt "259200" ]; then
Spaces around the [ are required.
|
bash script to search the words from a file into the whole server |
If you have saved your patterns in the file /root/my-wordfile, then you can
use the following to find "all" the files under /home
grep -rf /root/my-wordfile /home
-r would search it recursively and -f would say the patterns are in
/root/my-wordfile(separated by newline)
Or if you want to search files with specific extension or names, you could
use find as below:
For example if you want to search all *.c files
find /home -name *.c -exec grep -Hnf /root/my-wordfile {} ;
Here -H would list the file name, -n would display line number.
|
Validating Empty column of .csv file in bash script |
If the echo prints ${Upper} blank, probably it contains only a space or a
tab, change the if this way
if [[ "${Upper}" =~ '^[[:blank:]]*$' ]]; then
echo "VAR is empty"
fi
|
Bash script for file listing (html output) |
Are you looking for this?:
$ ls -1
INFO1 - INFO2 - INFO3.doc
INFO1 - INFO4 - INFO6.doc
l.sh
$ ./l.sh
<li>
<h5>INFO1</h5>
<p>INFO2</p>
</li>
<li>
<h5>INFO1</h5>
<p>INFO4</p>
</li>
The script:
#!/bin/bash
for i in *.doc
do
# split file-name into array
IFS='- ' read -a array <<< "$i"
echo "<li>"
echo "<h5>${array[0]}</h5>"
echo "<p>${array[1]}</p>"
echo "</li>"
done
update
If filename looks like this:
First name blabla - Second part bla boa - third part, unimportant.doc
Change the IFS-parameter to a single - instead.
Make this change:
IFS='-' read -a array <<< "$i"
output:
$ ./l.sh
<li>
<h5>First name blabla &l
|
Hazel bash script or Applescript to sum numbers in txt file |
this awk one-liner should do :
awk -F'$' '{x+=$2+0}7;END{print "Total: "x}' file
it prints the whole file, and add a line
Total: 98
at the end.
|
Pause for password sftp bash script file |
After months of looking for an answer I have finally found the solution. It
was in a comment on an answer in some other thread I can't even remember.
Hope this can help others out there.
Your bash script should look like this and will connect to the sftp server,
prompt the user for the password, and then execute the remaining commands.
#!/bin/bash
sftp user@server <<!
cd /the/remote/directory
lcd /your/local/directory
put/get some.file
!
|
Using variables in bash script to set ini-file values while executing |
Not quite sure whether this helps or not:
This calls the program script five times:
for n in 1 2 3 4 5
do
./program $n input.ini
done
Then in program, refer to the first parameter $n as $1.
The second parameter input.ini is $2.
|