I have a text file in which the first character - I want to delete, but I am not getting either its a CR or LF character, how Can I delete the file character from the entire file.
Asked
Active
Viewed 4,815 times
-1
-
Do you want to delete it in python or bash? – cs95 Jul 06 '17 at 02:01
-
using bash is easy - so bash – hassan Jul 06 '17 at 02:03
-
Deleted python tag. – cs95 Jul 06 '17 at 02:04
-
Possible duplicate of [Delete the first five characters on any line of a text file in Linux with sed](https://stackoverflow.com/q/3795512/608639) – jww Jun 04 '19 at 23:54
3 Answers
3
Use the tail
command:
tail -c +2 file.txt > newfile.txt
The -c
option says to count by characters rather than lines, and +2
means to output the tail starting from character 2.

Barmar
- 741,623
- 53
- 500
- 612
-
-
How else do you expect to do it? Unix doesn't provide any way to shift the bytes of a file directly. – Barmar Jul 06 '17 at 02:05
-
-
It's quite curious. Python provides a utility called `fileinput` that entertains inline edits to a file. I thought there must be a bash utility similar to that, maybe with sed or cut. – cs95 Jul 06 '17 at 02:13
-
@hassan: In situations like this, I find it's usually instructive to view the file in a hex editor. Most likely, there's an unprintable character present, and the hex editor will allow you to see which one. – khampson Jul 06 '17 at 02:17
-
@cᴏʟᴅsᴘᴇᴇᴅ You can use `ed` from a shell script. But it still has to do all the same rewriting -- it reads the whole file into memory, you do your edits, then it writes the memory back to the file. – Barmar Jul 06 '17 at 02:21
-
2The tricky thing is that tools like `sed` and `ed` operate on lines, not characters. So if the first character in the file is a newline, it's more complicated to delete it with these tools. – Barmar Jul 06 '17 at 02:22
-
And the `fileinput` utility has to do this as well. There simply isn't a way on Unix to directly modify a file's contents, it's all done by reading and writing. – Barmar Jul 06 '17 at 02:23
-
one caveat is that it only does it for the first line of the txt file – Mona Jalal Apr 05 '22 at 22:18
-
2
0
in python, you could open the file as a string, remove the first character, and wirte the file again.
myfile = open("yourfilehere", "r")
mylist = myfile.readlines()
mytext = ""
for items in mylist:
mytext = mytext + items +"\n"
myfile.close()
mytext = mytext[1:]
myfile = open("yourfilehere", "w")
myfile.write(mytext)

PMiner
- 1
- 4
0
If the first character that you want to delete is on the first line only, use the tail
command. tail -c +2
will do the job.
<oldFile tail -c +2 >newFile
If the first character that you want to delete is on every line of the file, use the sed
command. sed -e 's/^.//'
will do the job.
<oldFile sed -e 's/^.//' >newFile
If the first character that you want to delete is on some lines of the file, and it is a specific character, again use the sed
command. For example, you might want to remove a leading space from each line that has leading space.
<oldFile sed -e 's/^ //' >newFile

Christian Hujer
- 17,035
- 5
- 40
- 47