Cut the first 100 characters of file

Hello all,

I have a file and would like to cut the first 100 characters of the first line. I tried it with the �cut�-command:

cut �c100- $file > $file.tmp

But this does not work, because it will cut the first 100 characters of each line. But I need to cut them only from the beginning of the file � so only from the first line.

Do you have a solution for this?

If you want only the first line then:

 head -1 file 

and only the first 100 char you could:

 head -1 file | cut -c1-100 

Oh, sorry for the misunderstanding.

When I will do it with the 'head'-command I would get this line cut of 100 characters. Thats for the line ok.

But my problem is that I have a file with a size of maybe 1GB. And it could be possible that the content is only 1 line. All what I want is to cut the first 100 characters of the file. This can I do with the command:

 
cut �c100- $file > $file.tmp

But I do not know if I have only 1 line in file. It could also have more lines in it. And then this command won't work anymore, because it will cut the first 100 characters of each line.

So, what should I do? Is there a command to do it for both situations? Or in minumum I would like to know how can I cut the first 100 characters of the first line of file if there are more lines?

I could do it in this way

 
line=$(head -1 $file)
sed '1d' $file > $file.body
echo $line > $file.head 
cat $file.head  $file.body > $file

But it there maybe another way, because it would be better to do it with 1 statement... :wall:

Try:

sed '1s/.\{100\}//' file

Not that this will cut off 100 characters of the first line only, not of the file. Is that what you are looking for?

head -c 100 filename

This will give the first 100 characters either single or multiple lines.

1 Like

Note: "head -c" cuts off bytes, not characters.

1 Like

Hello all,

thanks for you help - with these hints I could solve the problem.

@Scrutinizer: Thats exactly what I need. Strictly speaking I needed it for a file, but to redirect the output to a file is no problem:

sed '1s/.\{100\}//' file  > file.tmp

@Girish19: With this statement I have the first 100 characters, but my problem was to have a file without these 100 characters. But nevertheless this is a very useful hint to extract the first 100 characters. I could use it at another position in my script.