Get the last 4 characters of output from standard out
How about tail
, with the -c
switch. For example, to get the last 4 characters of "hello":
echo "hello" | tail -c 5
ello
Note that I used 5 (4+1) because a newline character is added by echo
. As suggested by Brad Koch below, use echo -n
to prevent the newline character from being added.
How to get the last character of a string in a shell?
That's one of the reasons why you need to quote your variables:
echo "${str:$i:1}"
Otherwise, bash expands the variable and in this case does globbing before printing out. It is also better to quote the parameter to the script (in case you have a matching filename):
sh lash_ch.sh 'abcde*'
Also see the order of expansions in the bash reference manual. Variables are expanded before the filename expansion.
To get the last character you should just use -1
as the index since the negative indices count from the end of the string:
echo "${str: -1}"
The space after the colon (:
) is REQUIRED.
This approach will not work without the space.
Getting last char sent to std::cout
It is similar answer to this given by @Kevin. However I believe it is better for your needs. Instead of using some your stream in place of cout - you can replace streambuf from std::cout with your own:
int main() {
std::streambuf* cbuf = std::cout.rdbuf(); // back up cout's streambuf
std::cout.flush();
keep_last_char_outbuf keep_last_buf(cbuf);
std::cout.rdbuf(&keep_last_buf); // assign your streambuf to cout
std::cout << "ala ma kota\n";
char last_char = keep_last_buf.get_last_char();
if (last_char == '\r' || last_char == '\n')
std::cout << "\nLast char was newline: " << int(last_char) << "\n";
else
std::cout << "\nLast char: '" << last_char << "'\n";
std::cout << "ala ma kota";
last_char = keep_last_buf.get_last_char();
if (last_char == '\r' || last_char == '\n')
std::cout << "\nLast char was newline: " << int(last_char) << "\n";
else
std::cout << "\nLast char: '" << last_char << "'\n";
std::cout.rdbuf(cbuf); // restore cout's original streambuf
}
And expected output:
ala ma kota
Last char was newline: 10
ala ma kota
Last char: 'a'
A task to write such class keep_last_char_outbuf
is not very easy, Look for decorator pattern and streambuf interface.
If you don't have time for playing with this - look at my proposal ideone link
class keep_last_char_outbuf : public std::streambuf {
public:
keep_last_char_outbuf(std::streambuf* buf) : buf(buf), last_char(traits_type::eof()) {
// no buffering, overflow on every char
setp(0, 0);
}
char get_last_char() const { return last_char; }
virtual int_type overflow(int_type c) {
buf->sputc(c);
last_char = c;
return c;
}
private:
std::streambuf* buf;
char last_char;
};
How to get string without the last N characters?
Sample:
INPUT="This is my string."
echo $INPUT |sed 's/.$//' # removes last character
INPUT="This is my stringoi"
echo $INPUT |sed 's/..$//' # removes last two character
Get the last two letters of each line in a file using script shell
You may use a single command awk
:
awk 'substr($0, length()-1) != "AA"{exit 1}' file && echo "GOOD" || echo "BAD"
substr($0, length()-1)
will extract last 2 characters of every line. awk command will exit with 1
if we don't fine AA
in any line.
Get the characters after the last index of a substring from a string
try this:
your cmd...|sed 's/.*\. //'
this works no matter how many "dot" or "dot and space" do you have in your input. it takes the string after the last "dot and space"
Don't print the last character: print only after the next input
The buffering of your system is set to be line buffered, characters are transmitted from the buffer as a block when a new-line character is encountered. Using \n
is perfectly valid, but it has the side effect of also printing a newline, there are other options, namely:
Using
fflush(stdout)
after theprintf
will flush the buffer regarless, you won't need\n
.You can change your buffering mode to have no buffering, each output is written as soon as possible. Again, no
\n
will be needed.setvbuf(stdout, NULL, _IONBF, 0);
last character of character array is getting excluded
You allocated n+1
characters for your array, but then you told getline
that there were only n
characters available. It should be like this:
int n;
cin>>n;
cin.ignore();
char arr[n+1];
cin.getline(arr,n+1); // change here
cin.ignore();
cout<<arr;
Trim last 3 characters of a line WITHOUT using sed, or perl, etc
Assuming all data is formatted like your example, use 'cut' to get the first column only.
cat $file | cut -d ' ' -f 1
or to get the first 10 chars.
cat $file | cut -c 1-10
How to remove unnecessary characters from stdout in python?
You can use the function strip to get copy of the array with leading and trailing spaces removed. If you are sure only trailing spaces have to be removed use rstrip
Note b'Hello World' is an array of byte, not a string, if you want to convert it to a string you can use decode("utf-8")
pi@raspberrypi:/tmp $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b"Hello World\r\n".strip()
b'Hello World'
>>> b"Hello World\r\n".strip().decode("utf-8")
'Hello World'
>>>
Related Topics
Using Bash Environment Variables from Within a Perl Script
Distinguish .Shstrtab and .Strtab in Elf File
Install Marklogic Centos Virtualbox Vm
Chrome Certificate Selection Appears Multiple Times
Printing an Integer with X86 32-Bit Linux Sys_Write (Nasm)
Aslr Bits of Entropy of Mmap()
Different Results Between Ps Aux and 'Ps Aux' Inside a Script
In Bash, Why 'X=100 Echo $X' Doesn't Print Anything
Join on First Column of Two Files
Sed: Insert a Line in a Certain Position
Shell Script Current Directory
How to Break Up an Extremely Long String Literal in Bash
How Does Ngrok Work Behind a Firewall
Limit the Memory and CPU Available for a User in Linux
How to Display Modified Date Time with 'Find' Command