This code will give the first part, but how can I remove it and get the whole string without the first part?
echo "first second third etc"|cut -d " " -f1
Using the parameter expansion syntax To remove the first and last character of a string, we can use the parameter expansion syntax ${str:1:-1} in the bash shell.
Example-2: Trim string data using `sed` commandUse sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]]. $ echo "$Var are very popular now."
Since we're dealing with known fixed length strings ( prefix and suffix ) we can use a bash substring to obtain the desired result with a single operation. Plan: bash substring syntax: ${string:<start>:<length>} skipping over prefix="hell" means our <start> will be 4.
You can use substring removal for that. There isn't any need for external tools:
$ foo="a b c d"
$ echo "${foo#* }"
b c d
You should have a look at info cut
, which will explain what f1
means.
Actually we just need fields after(and) the second field. -f
tells the command to search by field, and 2-
means the second and following fields.
echo "first second third etc" | cut -d " " -f2-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With