5
sed 's/.\(.*\)/\1/' myfile

Say myfile contains:

abc
def
ghi

I want to remove the first character only from the first line but the above removes it from all the line.

11

Maybe sed '1s/.\(.*\)/\1/' myfile?

  • 4
    Capturing groups slows down the regular expressions. Better do it without them: sed '1s/.//' myfile. – manatwork Feb 24 '12 at 15:35
4

can also use awk: awk 'NR==1 {sub(/./,"")} 1'

2

tail can read from a byte offset, so if your file uses only single-byte characters (typically UTF-8 ASCII), this will work.

It depends on your situaton, whether it is improtant to you or not, but tail is faster than sed... probably becaue sed needs to process each line inndividually, ... But don't use it where there is any chance of the first character using more than one byte.

tail -c +2 "file"  

From man tail

-c, --bytes=N output the last N bytes; alternatively, use +N to output bytes starting with the Nth of each file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.