How can I strip first X characters from string using sed?

BashShellSed

Bash Problem Overview


I am writing shell script for embedded Linux in a small industrial box. I have a variable containing the text pid: 1234 and I want to strip first X characters from the line, so only 1234 stays. I have more variables I need to "clean", so I need to cut away X first characters and ${string:5} doesn't work for some reason in my system.

The only thing the box seems to have is sed.

I am trying to make the following to work:

result=$(echo "$pid" | sed 's/^.\{4\}//g')

Any ideas?

Bash Solutions


Solution 1 - Bash

The following should work:

var="pid: 1234"
var=${var:5}

Are you sure bash is the shell executing your script?

Even the POSIX-compliant

var=${var#?????}

would be preferable to using an external process, although this requires you to hard-code the 5 in the form of a fixed-length pattern.

Solution 2 - Bash

Here's a concise method to cut the first X characters using cut(1). This example removes the first 4 characters by cutting a substring starting with 5th character.

echo "$pid" | cut -c 5-

Solution 3 - Bash

Use the -r option ("use extended regular expressions in the script") to sed in order to use the {n} syntax:

$ echo 'pid: 1234'| sed -r 's/^.{5}//'
1234

Solution 4 - Bash

Cut first two characters from string:

$ string="1234567890"; echo "${string:2}"
34567890

Solution 5 - Bash

pipe it through awk '{print substr($0,42)}' where 42 is one more than the number of characters to drop. For example:

$ echo abcde| awk '{print substr($0,2)}'
bcde
$

Solution 6 - Bash

Chances are, you'll have cut as well. If so:

[me@home]$ echo "pid: 1234" | cut -d" " -f2
1234

Solution 7 - Bash

Well, there have been solutions here with sed, awk, cut and using bash syntax. I just want to throw in another POSIX conform variant:

$ echo "pid: 1234" | tail -c +6
1234

-c tells tail at which byte offset to start, counting from the end of the input data, yet if the the number starts with a + sign, it is from the beginning of the input data to the end.

Solution 8 - Bash

Another way, using cut instead of sed.

result=`echo $pid | cut -c 5-`

Solution 9 - Bash

I found the answer in pure sed supplied by this question (admittedly, posted after this question was posted). This does exactly what you asked, solely in sed:

result=\`echo "$pid" | sed '/./ { s/pid:\ //g; }'\``

The dot in sed '/./) is whatever you want to match. Your question is exactly what I was attempting to, except in my case I wanted to match a specific line in a file and then uncomment it. In my case it was:

# Uncomment a line (edit the file in-place):
sed -i '/#\ COMMENTED_LINE_TO_MATCH/ { s/#\ //g; }' /path/to/target/file

The -i after sed is to edit the file in place (remove this switch if you want to test your matching expression prior to editing the file).

(I posted this because I wanted to do this entirely with sed as this question asked and none of the previous answered solved that problem.)

Solution 10 - Bash

Rather than removing n characters from the start, perhaps you could just extract the digits directly. Like so...

$ echo "pid: 1234" | grep -Po "\d+"

This may be a more robust solution, and seems more intuitive.

Solution 11 - Bash

This will do the job too:

echo "$pid"|awk '{print $2}'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKokeshView Question on Stackoverflow
Solution 1 - BashchepnerView Answer on Stackoverflow
Solution 2 - BashRandy the DevView Answer on Stackoverflow
Solution 3 - BashMark LongairView Answer on Stackoverflow
Solution 4 - Bashdtp70View Answer on Stackoverflow
Solution 5 - BashBenView Answer on Stackoverflow
Solution 6 - BashShawn ChinView Answer on Stackoverflow
Solution 7 - BashMeckiView Answer on Stackoverflow
Solution 8 - BashEvgenyView Answer on Stackoverflow
Solution 9 - BashtreeheadView Answer on Stackoverflow
Solution 10 - Bashuser1751825View Answer on Stackoverflow
Solution 11 - BashArnaud F.View Answer on Stackoverflow