Ammastaro
Ammastaro

Reputation: 193

Remove first character of a text file from shell

I have a text file and I would like to only delete the first character of the text file, is there a way to do this in shell script?

I'm new to writing scripts so I really don't know where to start. I understand that the main command most people use is "sed" but I can only find how to use that as a find and replace tool.

All help is appreciated.

Upvotes: 8

Views: 24095

Answers (6)

Toby Speight
Toby Speight

Reputation: 30831

dd allows you to specify an offset at which to start reading:

dd ibs=1 seek=1 if="$input" of="$output"

(where the variables are set to point to your input and output files, respectively)

Upvotes: 2

Denis Marques
Denis Marques

Reputation: 120

I used to use cut command to do this.

For example:

cat file|cut -c2-80

Will show characters from column 2 to 80 only.

In your case you can use:

cat file|cut -c2-10000 > newfile

I hope this help you.

[]s

Upvotes: 4

glenn jackman
glenn jackman

Reputation: 246807

A few other ideas:

awk '{print (NR == 1 ? substr($0,2) : $0)}' file

perl -0777 -pe 's/.//' file

perl -pe 's/.// unless $done; $done = 1' file

ed file <<END
1s/.//
w
q
END

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84561

You can also use the 0,addr2 address-range to limit replacements to the first substitution, e.g.

sed '0,/./s/^.//' file

That will remove the 1st character of the file and the sed expression will be at the end of its range -- effectively replacing only the 1st occurrence.

To edit the file in place, use the -i option, e.g.

sed -i '0,/./s/^.//' file

or simply redirect the output to a new file:

sed '0,/./s/^.//' file > newfile

Upvotes: 2

ihatecsv
ihatecsv

Reputation: 542

You can use sed

sed '1s/^.//' startfile > endfile
  • 1s means match line 1, in substitution mode (s)
  • ^. means at the beginning of the line (^), match any character (.)
  • There's nothing between the last slashes, which means substitute with nothing (remove)

Upvotes: 10

Barmar
Barmar

Reputation: 781004

You can use the tail command, telling it to start from character 2:

tail -c +2 infile > outfile

Upvotes: 18

Related Questions