Unix script / replace string in file from variable and move into another directory - linux

I'm trying to do a script which find specific string in .txt files and replace by variable. I don't want to change original files but do the copies and then move into another (specific) directory. Let's assume 3 .txt files:
files.txt,
file2.txt
file3.txt
First step is to find string "string1" and "string2" in all .txt files and do the copies (eg. tmp files).
Second is to replace a string by variable $1 and $2 (working on tmp files).
Then move all of them to 'directoryname' directory.
That's what I'v got:
#!/bin/bash
echo "$1 - first parameter"
echo "$2 - second"
configurer() {
for file in *.txt
do
echo "Processing file .... $file"
orig_file=$file
tmp_file=$orig_file.tmp
cp $orig_file $tmp_file
sed "s/string1/$1/g;s/string2/$2/g" $tmp_file
mv $tmp_file directorname/$orig_file
done
}
configurer
echo "Done ..."
It's almost correct, (correct move into another directory, do the tmp files), but sed function doesn't work as it should and I have no idea why. Could anyone take a look ?
Regards

try below sed, its always problem with sed with variable
sed -i -e 's/string1/'"$1"'/g' -e 's/string2/'"$2"'/g' $tmp_file
let me know if it works
for your piece of code
#!/bin/bash
echo "$1 - first parameter"
echo "$2 - second"
configurer() {
for file in *.txt
do
echo "Processing file .... $file"
orig_file=$file
tmp_file=$orig_file.tmp
cp $orig_file $tmp_file
sed -i -e 's/string1/'"$1"'/g' -e 's/string2/'"$2"'/g' $tmp_file
mv $tmp_file directorname/$orig_file
done }
configurer $1 $2
echo "Done ..."

Related

Need a solution for grep command for .gz files

I have file names starts with RACHEL_20180814_092356.csv.gz
and need to grep in format like RACHEL_20180814*.gz nd unzip it, but am unable too. Here is sample code I have been working on.need to also insert a date parameter, which changes with each day. tried using zgrep but I am out of luck! Any help please
Process GMRA file
echo "Starting file get for Rachel.gz files"
for SUBDIR in prices; do
set -A getlist `/bin/ls ${ENV_DIR_SCR}/bat/prices`
if [ ! -z ${getlist[0]} ]; then
for FILENAME in ${getlist[*]}; do
echo "Found File ${ENV_DIR_SCR}/bat/prices/${FILENAME}"
if [ `echo $FILENAME | grep "RACHEL*.gz"` ]; then
$FILENAME = gunzip $FILENAME
GETFILES="$GETFILES ${FILENAME}"
break
fi
done
fi
done echo "Completed file_get for RACHEL.gz files"

sed with variable as argument in bash script

I am trying to write a bash script to scan for authorized_keys files and remove the keys of a couple previous employees if found. I am having one heck of a time figuring out the escaping for the sed command at the end. I am using commas instead of / since / can show up in the ssh-key. Any help would be appreciated
#!/bin/bash
declare -A keys
keys["employee1"]='AAAAB3NzaC1yc2EAAAABJQAAAIEAxoZ7ZdpJkL98n8cSTkFBwaAeSNK0m/tOWtF1mu5NAzMM/+1SDO6rJH/ruyyqBJo9s+AHWZLGRHfXT2XBg2SRaUnubAKp0w6qNIbej0MsA/ifAs8AfVGdj0pUPLtKpo6XVZkB8vEZSIQ+xNk1n5HJrGJnFGWKWeY3z1/KOLxcLHU='
keys["employee2"]='AAAAB3NzaC1yc2EAAAABIwAAAQEAwHYNAVhb319OBVXPhYF8cSTkFBwaAekr7UcKjfLPCHMpz19W0L/C0g+75Hn8COxOQILDUhIPhYHXOduQjGD/6NXgJDWxgyT00Azg5BREUnBd58WqZPlEvTZYlAgmdMIbnWPPGdJwzqKH/k7/STK6vTKxL6rxBo4lSNK0m/tOWtF1mu5NAzMM/+1SDO6rJH/ruyyqBJo9s+NIbej0MsA/ifAs8AfAkfO2JjgeQpJMyZ7B02XVN5iSLAyC3Cb5FXIjJuk4LPhcApuVyszH2lgve0r5bt/nFgVujJTvJTHPlGrqkYDcDJVUtfbjoLqGPrnpijp6rGIC7aFDDe7bk0ygHYMXDFWcjJBerfLGUWTYWFFLY3bfiO/h/9oEycmQHyB2co4a0IyyDnaYn9OY6xsRRATVlk4Q=='
files=`find / -name authorized_keys`
echo "Checking Authorized_Keys files on: " `hostname`
echo ""
echo "Located files: "
for file in $files; do
echo " $file"
done
echo""
for file in $files; do
for key in "${!keys[#]}"; do
if grep -q ${keys[$key]} $file; then
echo " *** Removing $key from $file"
sed "s,${keys[$key]},d" $file
fi
done
done
You've made it a bit complicated I think.
You can do this using grep -vf and process substitution:
# array to hold the value you want to remove
keys=(
'AAAAB3NzaC1yc2EAAAABJQAAAIEAxoZ7ZdpJkL98n8cSTkFBwaAeSNK0m/tOWtF1mu5NAzMM/+1SDO6rJH/ruyyqBJo9s+AHWZLGRHfXT2XBg2SRaUnubAKp0w6qNIbej0MsA/ifAs8AfVGdj0pUPLtKpo6XVZkB8vEZSIQ+xNk1n5HJrGJnFGWKWeY3z1/KOLxcLHU=',
'AAAAB3NzaC1yc2EAAAABIwAAAQEAwHYNAVhb319OBVXPhYF8cSTkFBwaAekr7UcKjfLPCHMpz19W0L/C0g+75Hn8COxOQILDUhIPhYHXOduQjGD/6NXgJDWxgyT00Azg5BREUnBd58WqZPlEvTZYlAgmdMIbnWPPGdJwzqKH/k7/STK6vTKxL6rxBo4lSNK0m/tOWtF1mu5NAzMM/+1SDO6rJH/ruyyqBJo9s+NIbej0MsA/ifAs8AfAkfO2JjgeQpJMyZ7B02XVN5iSLAyC3Cb5FXIjJuk4LPhcApuVyszH2lgve0r5bt/nFgVujJTvJTHPlGrqkYDcDJVUtfbjoLqGPrnpijp6rGIC7aFDDe7bk0ygHYMXDFWcjJBerfLGUWTYWFFLY3bfiO/h/9oEycmQHyB2co4a0IyyDnaYn9OY6xsRRATVlk4Q=='
)
while IFS= read -d '' -r file; do
grep -vf <(printf "%s\n" "${keys[#]}") "$file" > "$file.tmp"
mv "$file.tmp" "$file"
done < <(find / -name authorized_keys -print0)
In your case, it's easy, just need to use a sign which not contained in base64 code as the delimiter, eg |:
sed "\|${keys[$key]}|d" $file
Explanation in the sed manual:
\%regexp%
(The % may be replaced by any other single character.)
This also matches the regular expression regexp, but allows one to use a different delimiter than /.

Parsing ls, not recommended

I received a advice for do not parse ls, like describes in this website: Don't parse ls.
I was looking for DAILY files in my directory so that's what I did then:
for f in *.DA*; do
[[ -e $f ]] || continue
for file in $f; do
echo "The file that you are working on: "$file
archiveContent=$( sed -n -e 1p $file )
echo $archiveContent
done
done
Ok, that's works well, I've two files A.DAILY and B.DAILY, with the both archives I can get what is inside it, but when I changed a little bit the loop, it doesn't iterated with all files with .DAILY extension in my directory.
for f in *.DA*; do
[[ -e $f ]] || continue
for file in $f; do
echo "The file that you are working on: "$file
archiveContent=$( sed -n -e 1p $file )
echo $archiveContent
COMPRESS $archiveContent;
done
done
when I called a function inside the loop, the loop just does for the first file, but not to the second.
Since the outer loop sets f to each file in turn, your inner loop doesn't seem to serve any purpose.
for f in *.DA*; do
[[ -e $f ]] || continue
echo "The file that you are working on: $f"
archiveContent=$( sed -n -e 1p "$f" )
echo "$archiveContent"
COMPRESS "$archiveContent"
done

How to read a file and copy from one file to another file in shell script

How to read a file and copy from one file to another file in shell script:
#!/bin/csh -f
echo ---file.txt---
cat file.txt
echo ######## file.text is opened ########
#set file_1="export/home/caratins/trial/file.txt"
while read line
do
echo "$line"
cp file.txt files
done<file.txt
Actually one folder trial is there, inside trial folder 4 text files are there. I want to open a file-'file.txt'. Inside file.txt 3 files names are there: test1.txt, test2.txt, test3.txt. My work is using file.txt file I have read all 3 files names and copy it to another folder. So for that I have to open file.txt, read the file and print 3 files and only copy these 3 files not full folder and copy these 3 files to another folder'files' which is in same directory.
if you want to copy entire file as it is then
cat filename >> newfilename
for three files
cat file1.txt file2.txt file3.txt >>file.txt
if you want to copy line by line then
while IFS= read -r line
do
echo "$line"
echo -e "$line\n" >>newfilename
done <"filename"
try this,
here test1 is you source folder, which will contail you files,
and test2 is destination folder where you will move your files after reading..
#!/bin/sh
cd test1;
echo "list of files:";
ls;
for filename in *;
do echo "file: ${filename}";
echo "reading..."
exec<${filename}
value=0
while read line
do
#value='expr ${value} +1';
echo ${line};
done
echo "read done for ${filename}";
cp ${filename} ../test2;
echo "file ${filename} moved to test2";
done
or you can try this...
ls;
echo "reading main file...";
filenames="filenames";
exec<${filenames}
while read name
do
echo "file: ${name}";
echo "reading..."
cd test1;
exec<${name}
value=0
while read line
do
#value='expr ${value} +1';
echo ${line};
done
echo "read done for ${name}";
cp ${name} ../test2;
cd ..;
echo "file ${file} moved to test2";
done
yo...

Editing every file in a directory after opening it bash

Looking around I didn't see exactly what I was looking for. Some similar stuff, but for some reason what I tried so far hasn't worked.
My main goals:
run script in my current directory
open the picture to see what it is
rename the picture i just viewed
repeat the process without running the script again
These were the sources I attempted to follow:
Bash Shell Loop Over Set of Files
Bash loop through directory and rename every file
How to do something to every file in a directory using bash?
==================================================================================
echo "Rename pictures. Path"
read path
for f in $path
do
eog $path
echo "new name"
read newname
mv $path $newname
cat $f
done
You should pass the script an argument rather than trying to make it interactive. You also have numerous quoting problems. Try something like this instead (untested):
#!/usr/bin/env bash
moveFile() {
local newName=
until [[ $newName ]]; do
printf '%s ' 'new name:'
read -er newName # -e implies Bash with readline
echo
done
mv -i "$1" "${1%/*}/${newName}"
}
if [[ ! -d $1 ]]; then
echo 'Must specify a path' >&2
exit 1
fi
for f in "$1"/*; do
eog "$f"
moveFile "$f"
done
You might want to try something like this:
for f in $*; do
eog $f
echo "new name:"
read newname
mv $f $newname
done
If you name the script, say, rename.sh, you can call
./rename.sh *gif
to review all files with extention 'gif'.
Using find command allows you to search for image files in the specified directory recursively.
echo -n "Rename pictures. Input image directory: "
read path
for f in `find $path -type f`
do
eog $f
echo -n "Enter new name: "
read newname
mv $f $newname
echo "Renamed $f to $newname."
done

Resources