How to remove first character from first line using sed

17,760

Solution 1

Try this instead:

sed -e '1s/^.//' input_file > output_file

Or if you'd like to edit the files in-place:

sed -ie '1s/^.//' input_file

(Edited) Apparently s/^.// doesn't quote do it, updated.

Solution 2

try:

sed -i '1s/^.\(.*\)/\1/' file

this should remove the first character from the first line. (try it without the -i argument first to make sure)

edit: i originally posted the following, which would delete the first character from every line. upon re-reading the question i realized that isn't quite what was wanted.

sed -i 's/^.\(.*\)/\1/' file

Solution 3

Remove the first character on the first line inplace:

sed -i  '1s/^.//' file
Share:
17,760
octopusgrabbus
Author by

octopusgrabbus

I was a software engineer working for a Greater Boston municipality. Wrote a lightweight Water Meter Data Management System in Python and using a MySQL database that maintains water meter and endpoint configuration and daily water meter reads. In addition, was responsible for the migration of a a municipal tax collection/reporting system written in Informix 4GL, C, and Perl. Additional components written in C# and Clojure to a commercial product. Wrote small Windows applications in C# and F# as needed for various departments. Twitter: @octopusgrabbus https://octopusgrabbus.wordpress.com/

Updated on June 18, 2022

Comments

  • octopusgrabbus
    octopusgrabbus almost 2 years

    I have three .csv files that are output from saving a query in MS SQLServer. I need to load these files into an Informix database, which requires that tacking on of a trailing delimiter. That's easy to do using sed

    s/$/,/g

    However, each of these files also contains (as displayed by vim, but not ed or sed) an at the first character position of the first line.

    I need to get rid of this character. It deletes as one character using vim's x command. How can I describe this character using sed so I can delete it without removing the line.

    I've tried 1s/^.//g, but that is not working.

  • Chris Seymour
    Chris Seymour over 11 years
    ^\. will only match a literal . at the start of the line, the g flag is redundant as is the redirection and most importantly this will perform a substitution on all matching lines not just the first line.
  • octopusgrabbus
    octopusgrabbus over 11 years
    This still puzzles me. What is the difference (if any) between -e "<sed-commands>" and -e '<sed-commands>' because yours worked.
  • sampson-chen
    sampson-chen over 11 years
    @sudo_O fixed it =) not sure why I put it in the first place.
  • octopusgrabbus
    octopusgrabbus over 11 years
    Thanks, but it's the first line I want. That byte alignment data is just 1st line/1st char.
  • Chris Seymour
    Chris Seymour over 11 years
    Here quoting doesn't make a difference so I'm not sure what you did, quoting is important to the shell for things like variable expansion and globbing.