Append text to a file with pattern matched name in bash [duplicate] - linux

This question already has an answer here:
In bash, how do I expand a wildcard while it's inside double quotes?
(1 answer)
Closed 4 years ago.
I am trying to add a line to a specific file which matches a pattern in its name.
e.g. I am trying to append text STATUS PASSED to a file whose name contains 2018_09_26_04_51_30.
date="2018_09_26_04_51_30"
echo "STATUS PASSED" >> "/test_dir/*$date*.txt"
Above said commands are creating new file named *2018_09_26_04_51_30*.txt which is not serving my purpose!
Let's assume that there are several other files with *.txt extension, but none of these files contains $date in their names.
The test_dir directory contains:
test1-2018_09_26_04_50_48.txt
test2-2018_09_26_04_50_56.txt
test3-2018_09_26_04_51_03.txt
test3-2018_09_26_04_51_30_51S.txt
So, here file test3-2018_09_26_04_51_30_51S.txt is unique.
P.S. I have to execute this script in both Linux and AIX.
Any help will be appreciated!

Try like this,
date="2018_09_26_04_51_30"
#cd /test_dir
echo "STATUS PASSED" >> $(ls /test_dir/*$date*.txt)

Related

rename file using a loop [duplicate]

This question already has answers here:
Rename multiple files based on pattern in Unix
(24 answers)
Closed 2 years ago.
I have a lot of file with the same format (???_ideal.sdf) name and I need to rename all (???.sdf) removing form the name "ideal". For example:
Files:
002_ideal.sdf
ERT_ideal.sdf
234_ideal.sdf
sCX_idel.sdf
New Files:
002.sdf
ERT.sdf
234.sdf
SCX.sdf
I thought to using a loop but I don’t know how to indicate that in the new file name should be removed "individual".
For example:
for file in ???_individual.sdf; mv $file what?
With your shown samples, could you please try following. This will only print the rename command on terminal(for safer side, check and make sure if commands are fine and looking ok to you first before actually renaming files), to perform actual rename remove echo from following.
for file in *_ideal.sdf
do
echo mv "$file" "${file/_ideal/}"
done
Based on this answer you can also write it as one line with:
rename 's/^(.*)_ideal.png/$1.png/s' **/**
First parameter replaces the the _ideal.png, second one means all files.

Bash script file as input [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
i am trying to give an file as input to me my shell script:
#!/bin/bash
file ="$1"
externalprogram "$file"
echo 'unixcommand file '
i am trying to give the path to my file but it says always
cannot open `=/home/username/documents/file' (No such file or directory)
my path is this /home/username/Documents/file
i do this in terminal : ./myscript.sh /home/username/Documents/file
can someone help me please?
When you say
file ="$1"
with a space after "file", you're running something called file with =$1 as an argument. There probably actually is a utility called file. If you want to assign $1 to a variable called file, you don't need the space:
file="$1"
there shouldn't be a space before = in the second line.
file=$1 should be good enough.
Check what shellcheck says about your code:
^-- SC1068: Don't put spaces around the = in assignments (or
quote to make it literal).
You can read more about SC1068 case on its Github
page.
#!/bin/bash
file=$1
code $file
echo "aberto o arquivo ${file} no vscode"
I made this code snippet to demonstrate, I pass a path and it opens the file in vscode

How to concatenate a string value at the head of a text file [duplicate]

This question already has answers here:
Unix command to prepend text to a file
(21 answers)
Closed 2 years ago.
Real nit picky Linux question.
I have a text file, call it userec. I also have a string variable 'var_a'.
I want to concatenate the string value, let just say it's 'howdy' to the top of the text file.
So something like
echo $var_a | cat usrec > file_out
where it pipes the output from the echo $var_a as a file and adds it to the top of file_out and then adds the rest of the usrec file.
So if the userec file contains just the line 'This is the second line' then the contents of file_out should be:
howdy
This is the second line.
problem is that's not what the command is doing and I do not want to create a variable to store var_a in. This is running from a script and I don't want to create any extra flack to have to clean up afterwards.
I've tried other variations and I'm comming up empty.
Can anyone help me?
If you give cat any file names then it does not automatically read its standard input. In that case, you must use the special argument - in the file list to tell it to read the standard input, and where to include it in the concatenated output. Since apparently you want it to go at the beginning, that would be:
echo $var_a | cat - usrec > file_out
I would simply do :
echo $var_a > file_out
cat usrec >> file_out

Evaluating variable - output json file contents [duplicate]

This question already has answers here:
File content into unix variable with newlines
(6 answers)
Closed 4 years ago.
In the bash shell, I'm trying to read the json file and load to a variable
eri#xyz:~/Documents/inbound>e1=$(eval echo $(cat ./deploy/request.json))
Upon fetching the output of that variable, I'm seeing -bash - command not found along with the actual contents of the .json file
eri#xyz:~/Documents/inbound>"$e1"
-bash: { type:Pipeline, category:Software, risk:4, short_description:sample short description text, description:sample detailed description text, assignment_group: Services - Retail Services, cmdb_ci:Retail Service, u_version:1.0, start_date:2017-01-04 18:00:00, end_date:2017-01-04 19:00:00, backout_plan:see department for standard backout plan, implementation_plan:sample implementation plan, test_plan:sample text plan, production_system:false }: command not found
Is there a way to suppress the -bash - command not found in the output?.
No need for eval - just e1=$(< ./deploy/request.json) should do the trick. (Thanks to #shellter for the syntax — you don't even need to use cat!)
To show the variable, you want
echo "$e1"
instead of just "$e1". "$e1" by itself on the command line does not print out the value of $e1, unlike many programming-language REPLs. Instead, it tells bash to try to interpret the entire contents of $e1 as the name of a command. It isn't the name of a command, so bash tells you a command by that name cannot be found.

Extracting the file name from a full path string [duplicate]

This question already has answers here:
Get just the filename from a path in a Bash script [duplicate]
(6 answers)
Closed 4 years ago.
Let's say I have a string which represents the full path of a file:
full_path='./aa/bb/cc/tt.txt'.
How can I extract only the file name tt.txt?
Please don't tell me to use echo $full_path | cut -d'/' -f 5.
Because the file may be located in a deeper or shallower folder.
The number, 5, cannot be applied in all cases.
if you are comfortable with python then, you can use the following piece of code.
full_path = "path to your file name"
filename = full_path.split('/')[-1]
print(filename)
Use the parameter expansion functionality.
full_path='./aa/bb/cc/tt.txt'
echo ${full_path%/*}
That will give you the output
./aa/bb/cc
And will work any number of directory levels deep, as it will give you everything up to the final "/".
Edit: Parameter expansion is very useful, so here's a quick link for you that gives a good overview of what is possible: http://wiki.bash-hackers.org/syntax/pe
You can use this construct
echo "$full_path" | sed 's/\/.*\///'

Resources