How do I cut letter X out of a word?

孤者浪人 提交于 2019-12-02 11:24:56

This might work for you (GNU sed):

sed 's/^\([0-9]*\) \(.*\)/echo '\''\2'\''|sed '\''s\/.\/\/\1'\''/e' file

This uses the e flag of the s command to evaluate the RHS and runs a second sed invocation using the backreferences from the LHS. Perhaps easier on the eye is this:

sed -r 's/^([0-9]*) (.*)/echo "\2"|sed "s#.##\1"/e' file

You can use just bash and its parameter expansion:

while read n s ; do
    echo "${s:0:n-1}${s:n}"
done < input.txt

If you need one line, just remove the newlines and add a semicolon:

while read n s ; do echo "${s:0:n-1}${s:n}" ; done < input.txt

If you really need to use sed and cut, it's also doable, but a bit less readable:

cat -n input.txt \
| sed 's/\t\([0-9]\+\).*/s=\\(.\\{\1\\}\\).=\\1=/' \
| sed -f- <(sed 's/[0-9]*//' input.txt) \
| cut -c2-

Explanation:

  1. number the lines
  2. turn each line into a sed command that searches for the given number of characters and removes the one following them
  3. run the generated sed command on the original file with the numbers removed
  4. remove the extra leading space

you can use sed in this way:

sed -e '1s/\([a-z]\)\{1\}//' -e 's/^[0-9]\+\s\+\(.*\)/\1/g' file.txt

The first sed regular expresion works on the first line and replace the first character and the second regular expresion works with rest of text, 1 column: one number or more, 2 column; one space or more, after this I put the remaining test in one match \(.*\) and replaced all with this match.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!