How replace string with regex in bash script? [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 days ago.
Improve this question
I have this working in terminal (it just remove the date using string replacement)
NAME="/home/me/a_path/2023-04-10 filename"
NEW_NAME=$(echo ${NAME//20[0-9][0-9]-[0-9][0-9]-[0-9][0-9] /})
echo ${NEW_NAME}
>>> Expected output : "/home/me/a_path/filename"
But this is not working (it output the non-modified string) in script, I can't understand why.
I tried different quotation marks positions and some other things that I found on SO, but nothing has worked for me yet.
I tried using sed it does not work better.
Edit: The example I gave is working, so probably a typo in my full script

Here is another approach without regex based on the inputs provided in the asked question.
#!/bin/bash
NAME="/home/me/a_path/2023-04-10 filename"
DIR=$(dirname "${NAME}")
FILE=$(basename "${NAME}" | awk '{print $NF}')
echo "${DIR}/${FILE}"
The output:
/home/me/a_path/filename

Related

Use echo to create a bash fils [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I wanted to make a one-liner that would create an executable. It's for a quick guide I' making for a friend, so that it would be an easy copy-paste job.
I have this:
export FILE=spotify; [ -f ~/.local/bin/$FILE ] && echo "File $FILE already exists. Couldn't create it..."; [ -f ~/.local/bin/$FILE ] || { echo $"#!/bin/sh\nflatpak run com.spotify.Client" > ~/.local/bin/$FILE; chmod +x ~/.local/bin/$FILE; echo "Successfully created $FILE" }
However I am stuck on echo "#!/bash/sh" which causes "event not found"...
Any ideas how to overcome that?
In a Bash interactive shell the exclamation mark is used for searching the Bash history. Specifically, it replaces the string after the exclamation mark with the first entry in the history which matches that string. If there is no such entry, you get the symptom you're seeing.
The solution is to use a single-quoted string.

How to remove characters from the beginning of txt file using bash? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have some txt file look like this:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAUAAtADASIAAhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAAAwQCBQEGBwgACf/EAFAQAAIBAwMCBAQEBAMHAQUBEQECAwAEEQUSIQYxEyJBUQcUYXEIMoGRFSNCoVKxwQACAwEBAQAAAAAAAAAAAAABAgADBAUGB//EADkRAAICAgIBAgUBBwIFBAMAAAABAhEDIRIxBCJBBRMyUWFxFCOBkaGx0TPBJEJS4fAGFTSSQ3KC/9oADAMBAAIRAxEAPwD8+4YcMMjIBoqXkcTEAkkcDB709Bp9tpQ/Y9xScplulJXkZwAO1bD/ALm3lvAJbiI/zAMKOcfpU
I wanna add to my script some command to remove:
data:image/jpeg;base64,
and get this effect:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAUAAtADASIAAhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAAAwQCBQEGBwgACf/EAFAQAAIBAwMCBAQEBAMHAQUBEQECAwAEEQUSIQYxEyJBUQcUYXEIMoGRFSNCoVKxwQACAwEBAQAAAAAAAAAAAAABAgADBAUGB//EADkRAAICAgIBAgUBBwIFBAMAAAABAhEDIRIxBCJBBRMyUWFxFCOBkaGx0TPBJEJS4fAGFTSSQ3KC/9oADAMBAAIRAxEAPwD8+4YcMMjIBoqXkcTEAkkcDB709Bp9tpQ/Y9xScplulJXkZwAO1bD/ALm3lvAJbiI/zAMKOcfpU
I try with some sed commands but don't work and i don't have more ideas.
You probably forgot to skip the backslash. Try again with:
sed 's/data:image\/jpeg;base64,//' file > newFile
If you are dealing with the text input which contains / then you can use different separator like # or # etc inside sed.
sed 's#data:image/jpeg;base64,##' file
or this for inplace replacement in the file.
sed -i 's#data:image/jpeg;base64,##' file

how do I basic script with linux? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
what does this command do?
!/bin/bash
My first script
clear
echo (I don't know what will come after echo , can you help me with that too?)
./hello.shell
#!/bin/bash is called the shebang (you missed the leading #).
It tells which program will execute your script.
clear is for clearing screen.
echo outputs following argument to the standard output (your terminal by default). But you must not surround your string with parenthesis as it's used for grouping command in a sub-shell. If you want to print (...), you'll have too use double quotes :
echo "(I don't know what will come after echo , can you help me with that too?)"
./hello.shell will execute your script after you gave it execute permissions with chmod +x hello.shell.
Note that commonly used extension for a shell script is .sh rather than .shell.
For more, try theses links :
http://mywiki.wooledge.org/BashGuide/
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
#!/bin/bash tells to the SO that this file is a script and that bash is the shell that must execute it. So you can found: #!/opt/bin/perl for perl scripts, #!/bin/csh for c-shell, #!/bin/zsh ...

Begining Bash Scripting in Linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
My teacher randomly dropped bash Script on us the last week of class with no previous use of anything except the bash shell commands on Linux.
I was wondering if someone could give me a small tutorial on how it works by over using the line notes.
the beginning problem is:
Write a bash script called getlines that can output the lines 'A' through 'B' of any given file.
example of the syntax
getline 5 17 "filename"
#!/bin/bash
#
a='cat $3 || wc -l'
b=$(($a-$1+1))
c=$(($a-$2+1))
for lines
do
'cat $3 || tail -$b || head -$c'
done
echo $lines
This is what I have so far, I know the math is off but I'll get to that when I can actually get the syntax up and running can someone just tell me some thing i could fix within the syntax.
Thanks for your help, and the nice lesson on how the site works ! Sorry if I came across like a lazy kid but I got it to work with:
#!/bin/bash
#
#
a='cat $3 | wc -l'
b=$(($a-$1+1))
c=$(($b-$2-1))
Lines='cat $3 | tail -$b | head -$c'
echo $lines
Thanks again guys ! You really did help guide me!

adding the same message for several files in linux [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I´ve written the line command this, but it doesn´t work
cat fich?.txt < hi, adding message for several files
I´ve got the following files
fich1.txt
fich2.txt
fich3.txt
fich4.txt
fich5.txt
fich6.txt
for f in fich?.txt; do
cat message.txt >>$f
done
This will add (append) the contents of the file message.txt to all files matching the shell globbing pattern fich?.txt. If you want to be more strict and only match the specific files that you mentioned, use the pattern fich[1-6].txt.
To add something to the beginning of each file, do this:
for f in fich?.txt; do
tmpfile=$( mktemp XXXXX )
cat message.txt $f >$tmpfile
mv $tmpfile $f
done
No catching of errors is happening here, so it's not fool-proof or super safe.
Different approach - inserts the new content at the beginning of the file
for f in fich*.txt; do
sed --in-place '1 ihi, adding message for several files' "$f";
done

Resources