This question already has answers here:
How to extract values from a java properties files in a bash/shell script and store in a variable and cd to that variable
(2 answers)
Code for parsing a key/value in in file from shell script
(6 answers)
Closed 5 years ago.
How can I append a string variable which I got through cut with next variable? In the file application.properties, I have value: myValue=/tmp/user/
I tried:
file=myfile.properties
path=$(cat application.properties | grep myValue=)
path2=$(echo $path | cut -d'=' -f 2- )
pathToFile=$path2$file
But output is only: myfile.properties. I need /tmp/user/myfile.properties
Thank you for your help.
Don't use cut for this at all: The shell's built-in string manipulation can do this trimming for you. (And don't use cat: It's more efficient to have grep read straight from the file than to have it reading from a separate process that then writes to a FIFO).
file=myfile.properies
path=$(grep myValue= <application.properties)
pathToFile=${path#*=}${file}
Related
This question already has answers here:
Rename files using regular expression in linux
(10 answers)
Closed 2 years ago.
I have files:
alpha_123_abc_file.txt
beta_456_def_file.txt
gamma_789_ghi_file.txt
Is there a way to rename all to cut the parts after the first _ character? To become:
123_abc_file.txt
456_def_file.txt
789_ghi_file.txt
I've looked into the perl tool but I an unsure if it has the capability to search out a pattern like that.
for file in *_*; do echo mv -- "$file" "${file#*_}"; done
Remove the echo when you're done testing and ready to actually do the mv.
This question already has answers here:
Bash function to find newest file matching pattern
(9 answers)
Closed 2 years ago.
I'm trying to do a bash one liner to get the latest logfile to cat and/or tail:
for i in /mnt/usbdrive/backup/filelog_*.log; do ls -t $i | head -n1 ; done
But get all of the matching files:
/mnt/usbdrive/backup/filelog_2020-06-03-09:00:01:345123169.log
/mnt/usbdrive/backup/filelog_2020-06-04-09:00:01:370667894.log
/mnt/usbdrive/backup/filelog_2020-06-04-19:15:27:274135912.log
/mnt/usbdrive/backup/filelog_2020-06-05-09:00:02:020131150.log
/mnt/usbdrive/backup/filelog_2020-06-06-09:00:02:238963148.log
Where am I going wrong?
Also, if I wanted to tail (or cat) that, would I have to declare another variable and tail -f that $variable ?
I'm trying to do a bash one liner to get the latest logfile
You could use
latestfile=$(/bin/ls -t /mnt/usbdrive/backup/filelog_*.log | /bin/tail -1)
assuming you don't have spaces (or semicolons, etc...) in your file names
See ls(1), tail(1) and carefully read the documentation of GNU bash.
You'll better write your script in some other language (e.g. GNU guile, Python, Lua). See the shebang handling of execve(2).
You might also use stat(1) and/or gawk(1) and/or find(1). See glob(7) and path_resolution(7).
You could be interested by logrotate(8) and crontab(5) and inotify(7).
This question already has answers here:
Rename multiple files, but only rename part of the filename in Bash
(6 answers)
Closed 4 years ago.
I have the following file names where I am trying to relabel v5.4b to v5.7:
v5.4b_lvl-1.e8974326
v5.4b_lvl-1.o8974326
v5.4b_lvl-1.pe8974326
v5.4b_lvl-1.po8974326
v5.4b_lvl-2.1.e8974303
v5.4b_lvl-2.1.o8974303
v5.4b_lvl-2.1.pe8974303
v5.4b_lvl-2.1.po8974303
v5.4b_lvl-2.2.e8974304
v5.4b_lvl-2.2.o8974304
v5.4b_lvl-2.2.pe8974304
v5.4b_lvl-2.2.po8974304
v5.4b_lvl-3.1.e8974305
v5.4b_lvl-3.1.o8974305
v5.4b_lvl-3.1.pe8974305
v5.4b_lvl-3.1.po8974305
v5.4b_lvl-4.1.e8974327
v5.4b_lvl-4.1.o8974327
v5.4b_lvl-4.1.pe8974327
v5.4b_lvl-4.1.po8974327
I can't do mv v5.4b_* v5.7_* because it thinks v5.7_* is a directory so I am trying a for-loop but I can't get it to work
I am trying the recommended answer from this SO post How to set a variable to the output of a command in Bash? but getting a bunch of empty lines.
What am I doing incorrectly? How can I save the output of cut to SUFFIX so I can mv $i v5.7_$SUFFIX?
-bash-4.1$ for i in v5.4b*; do echo $i | SUFFIX=`cut -f2 -d'_'`; echo ${SUFFIX}; done
You've got echo $i in the wrong place. The output of that command needs to be piped to cut for it to read anything, then the result is assigned to SUFFIX:
for i in v5.4b*
do
SUFFIX=`echo $i | cut -f2 -d'_'`
echo ${SUFFIX}
done
If you rename utility then just do:
rename -n 's/v5\4.b/v5.7/' v5.4b*
PS: -n is for dry-run. You may remove it later for real renaming.
If rename is not available then use:
for i in v5.4b*; do
echo mv "$i" "${i/v5.4b/v5.7}"
done
Remove 'echo` if you're satisfied with the output.
This question already has answers here:
Grep : get all file that doesn't have a line that matches [closed]
(3 answers)
Closed 4 years ago.
I am looking for a command in bash that lists the files in which a keyword is not present. for listing files with the keyword I do
fgrep KEYWORD .
I was thinking I could feed vimdiff with two files with the lists, something like this
diff `fgrep KEYWORD .` `ls .` (THIS IS NOT CORRECT)
but I would not like to create two new files at hoc.
How about using simple grep option.
grep -L "foo" *
You could use --files-without-match option too with it.
This question already has answers here:
Bash input for multiple file
(5 answers)
Closed 8 years ago.
I have thousands of two set of files, one with name.ext and another set for the same name ending with name.new.psl. So for every name.ext there is a name.new.psl. Now I have to pass this as arguments to a script such as customise.pl name.ext name.new.psl
Any ideas for a loop in bash? The first name is common for each name.ext and name.new.psl like:
perl customise.pl name.ext name.new.psl
for f in *.ext ; do
perl customise.pl "${f}" "${f/%.txt/.new.psl}"
done
Will do it for you in the current working directory.
for fname in *.ext
do
perl customise.pl "$fname" "${fname%.ext}.new.psl"
done
The above does not require any special bash features. So, it is compatible with, for example, dash which is the default shell (/bin/sh) on debian-derived distributions.
The trick above is that ${fname%.ext} tells the shell to remove the text .ext from the end of $fname, leaving just the "name" part. Thus, "${fname%.ext}.new.psl" removes .ext adds the .new.psl extension.
The file names in the code above are in double-quotes. This is so that this script will work even if the file names have spaces in them.
for i in `ls *.ext`; do NAME=`echo $i | awk -F '.' '{print $1}'`; perl customise.pl $NAME.ext $NAME.new.psl; done