Remove all text from last dot in bash
With awk:
awk 'BEGIN{FS=OFS="."} NF--' file
In case there are no empty lines, this works. It sets input and output field separators to the dot .
. Then, decreases the number of fields in one, so that the last one is kept out. Then it performs the default awk
action: {print $0}
, that is, print the line.
With sed
:
sed 's/\.[^.]*$//' file
This catches the last block of .
+ text
+ end of line
and replaces it with nothing. That is, it removes it.
With rev
and cut
:
rev file | cut -d'.' -f2- | rev
rev
reverses the line, so that cut
can print from the 2nd word to the end. Then, rev
back to get the correct output.
With bash
:
while ISF= read -r line
do
echo "${line%.*}"
done < file
This perform a string operation consisting in replacing the shortest match of .*
from the end of the variable $line
content.
With grep
:
grep -Po '.*(?=\.)' file
Look-ahead to print just what is before the last dot.
All of them return:
abc.cde.ccd.eed.12345.5678
abcd.cdde.ccdd.eaed.12346.5688
aabc.cade.cacd.eaed.13345.5078
abzc.cdae.ccda.eaed.29345.1678
abac.cdae.cacd.eead.18145.2678
aabc.cdve.cncd.ened.19945.2345
How to remove the last dot from a string in bash?
Answering OP's issue's fix: Since you are reading input from a variable, then that's not the correct way to pass value from variable to sed
what you are using shown in your question(you must be getting No such file or directory
error while executing your code).
Try using:
echo "$domain" | sed 's/\.$//'
OR use:
sed 's/\.$//' <<<"$domain"
Ideal way to deal is could be: Try following if you are ok to use parameter expansion as shown by @anubhava sir in comments.
var="${domain%.}"
How can I remove last dot (period) from a string in bash
Parameter expansion has an operator %
to trim characters from the end of an expansion.
$ set test.
$ echo "$1"
test.
$ echo "${1%.}"
test
If there is no trailing dot, the expansion is left unchanged:
$ set test
$ echo "${1%.}"
test
How can I remove all text after a character in bash?
An example might have been useful, but if I understood you correctly, this would work:
echo "Hello: world" | cut -f1 -d":"
This will convert Hello: world
into Hello
.
How to remove last n characters from a string in Bash?
First, it's usually better to be explicit about your intent. So if you know the string ends in .rtf
, and you want to remove that .rtf
, you can just use var2=${var%.rtf}
. One potentially-useful aspect of this approach is that if the string doesn't end in .rtf
, it is not changed at all; var2
will contain an unmodified copy of var
.
If you want to remove a filename suffix but don't know or care exactly what it is, you can use var2=${var%.*}
to remove everything starting with the last .
. Or, if you only want to keep everything up to but not including the first .
, you can use var2=${var%%.*}
. Those options have the same result if there's only one .
, but if there might be more than one, you get to pick which end of the string to work from. On the other hand, if there's no .
in the string at all, var2
will again be an unchanged copy of var
.
If you really want to always remove a specific number of characters, here are some options.
You tagged this bash
specifically, so we'll start with bash builtins. The one which has worked the longest is the same suffix-removal syntax I used above: to remove four characters, use var2=${var%????}
. Or to remove four characters only if the first one is a dot, use var2=${var%.???}
, which is like var2=${var%.*}
but only removes the suffix if the part after the dot is exactly three characters. As you can see, to count characters this way, you need one question mark per unknown character removed, so this approach gets unwieldy for larger substring lengths.
An option in newer shell versions is substring extraction: var2=${var:0:${#var}-4}
. Here you can put any number in place of the 4
to remove a different number of characters. The ${#var}
is replaced by the length of the string, so this is actually asking to extract and keep (length - 4) characters starting with the first one (at index 0). With this approach, you lose the option to make the change only if the string matches a pattern; no matter what the actual value of the string is, the copy will include all but its last four characters.
You can leave the start index out; it defaults to 0, so you can shorten that to just var2=${var::${#var}-4}
. In fact, newer versions of bash (specifically 4+, which means the one that ships with MacOS won't work) recognize negative lengths as the index of the character to stop at, counting back from the end of the string. So in those versions you can get rid of the string-length expression, too: var2=${var::-4}
.
If you're not actually using bash but some other POSIX-type shell, the pattern-based suffix removal with %
will still work – even in plain old dash, where the index-based substring extraction won't. Ksh and zsh do both support substring extraction, but require the explicit 0 start index; zsh also supports the negative end index, while ksh requires the length expression. Note that zsh, which indexes arrays starting at 1, nonetheless indexes strings starting at 0 if you use this bash-compatible syntax. But zsh also allows you to treat scalar parameters as if they were arrays of characters, in which case the substring syntax uses a 1-based count and places the start and (inclusive) end positions in brackets separated by commas: var2=$var[1,-5]
.
Instead of using built-in shell parameter expansion, you can of course run some utility program to modify the string and capture its output with command substitution. There are several commands that will work; one is var2=$(sed 's/.\{4\}$//' <<<"$var")
.
Bash script to remove dot end of each line
Just use sed
:
sed 's/\.$//' yourfile
- Escape the special character
.
using\
. - Put an achor
$
to only remove it from the end. - To make infile changes use
-i
option ofsed
.
Delete everything before last / in a file path
awk '{print $NF}' FS=/ input-file
The 'print $NF' directs awk to print the last field of each line, and assigning FS=/ makes forward slash the field delimeter. In sed, you could do:
sed 's@.*/@@' input-file
which simply deletes everything up to and including the last /
.
How can I remove the last character of a file in unix?
A simpler approach (outputs to stdout, doesn't update the input file):
sed '$ s/.$//' somefile
$
is a Sed address that matches the last input line only, thus causing the following function call (s/.$//
) to be executed on the last line only.s/.$//
replaces the last character on the (in this case last) line with an empty string; i.e., effectively removes the last char. (before the newline) on the line..
matches any character on the line, and following it with$
anchors the match to the end of the line; note how the use of$
in this regular expression is conceptually related, but technically distinct from the previous use of$
as a Sed address.Example with stdin input (assumes Bash, Ksh, or Zsh):
$ sed '$ s/.$//' <<< $'line one\nline two'
line one
line tw
To update the input file too (do not use if the input file is a symlink):
sed -i '$ s/.$//' somefile
Note:
- On macOS, you'd have to use
-i ''
instead of just-i
; for an overview of the pitfalls associated with-i
, see the bottom half of this answer. - If you need to process very large input files and/or performance / disk usage are a concern and you're using GNU utilities (Linux), see ImHere's helpful answer.
Related Topics
Linux /Bin/Sh Check If String Contains X
Gdb Warning: Loadable Section Not Found in Added Symbol-File System-Supplied Dso at 0X7Ffff7Ffd000
Loop Over File Names from 'Find'
Analyzing CPU Registers During Kernel Crash Dump
How to List Recently Deleted Files from a Directory
Is It Good Practice to Use Mkdir as File-Based Locking on Linux
Gdb: Lx-Symbols Undefined Command
Configuring Oracle Listener to Listen Externally
How to Set Emacsclient Background as Emacs Background
How to Get Clearcase Stream Name by a Given View Path
How to Produce Stand Alone Haskell Executable
Rsync --Exclude Not Excluding Specific Files
How to Reserve Virtual Memory in Linux
Docker Container Accessible Only via Cloudflare Cdn (Selected Ip Ranges)
Unterminated Address Regex While Using Sed
Bash: How to Traverse Directory Structure and Execute Commands
Top Command First Iteration Always Returns the Same Result
Why Is Execution Time of a Process Shorter When Another Process Shares the Same Ht Core