Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep the first 52000 characters of the first line in bash

Tags:

bash

sed

awk

I have a big file with many lines and a first line that has something like 100 000 characters.

I'm trying to keep the first 52000 characters from the first line and only the first line In Addtion to the rest of the file, which remains the same.

I've search for the net but I only found solutions where the removal of the first n-th characters was the norm.

I thought about cut -c 1-52000 but cut will remove every line and I want only the first line to keep up to 52000 characters.

I checked on sed but I did not find something useful.

I thought about this one

awk '{ NR==1 substr( 1, 52000) } { print }' infile

Do you think it would work?

Any tips are welcomed.

like image 250
Andy K Avatar asked Apr 03 '14 13:04

Andy K


2 Answers

What about this:

dd if=yourfile bs=52000 count=1 2>/dev/null | head -n 1
like image 156
Mark Setchell Avatar answered Sep 21 '22 17:09

Mark Setchell


If you're certain that the first line contains more than 52000 characters, you could use head. Saying:

head -c 52000 filename

would produce the first 52000 bytes from the specified file (note that the specified bytes aren't restricted to the first line).

From man head:

   -c, --bytes=[-]K
          print the first K bytes of each  file;  with  the  leading  `-',
          print all but the last K bytes of each file

Using head a variant that would produce the desired result (only from the first line) would be:

head -1 filename | head -c 52000
like image 20
devnull Avatar answered Sep 19 '22 17:09

devnull