How to get first n characters of each line in unix data file How to get first n characters of each line in unix data file unix unix

How to get first n characters of each line in unix data file


With cut:

$ cut -c-22 file000000000001199998000100000000000219999800010000000000031999980001000000000004199998000100000000000519999800010000000000061999980001

If I understand the second requirement you want to split the first 22 characters into two columns of length 10 and 12. sed is the best choice for this:

$ sed -r 's/(.{10})(.{12}).*/\1 \2/' file0000000000 0119999800010000000000 0219999800010000000000 0319999800010000000000 0419999800010000000000 0519999800010000000000 061999980001


sudo_O has provided nice cut and sed solution, I just added an awk one-liner:

awk 'BEGIN{FIELDWIDTHS="22"} {print $1}' fileecho "000000000001199998000180000     DUMMY RAG"|awk 'BEGIN{FIELDWIDTHS="22"} {print $1}'0000000000011999980001

with empty char (it depends on your requirement, you want to skip the spaces or you want to include and count them in your output)

if blank spaces should be counted and displayed in output as well: (you don't have to change the cmd above)

echo "0 0 0 0 00000001199998000180000"|awk 'BEGIN{FIELDWIDTHS="22"} {print $1}'                                                                         0 0 0 0 00000001199998

if you want to skip those spaces: (quick and dirty)

echo "0 0 0 0 00000001199998000180000"|sed 's/ //g'|awk 'BEGIN{FIELDWIDTHS="22"} {print $1}'                                                            0000000000011999980001


This can actually be done in Bash without using any external programs (scripts using this must start with #!/bin/bash instead of #!/bin/sh and will not be POSIX shell compliant) using the expression ${VARIABLE:offset:length} (where :length is optional):

#!/bin/bashSTR="123456789"echo ${STR:0:1}echo ${STR:0:5}echo ${STR:0:10}echo ${STR:5:10}echo ${STR:8:10}

will have this output:

11234512345678967899

Note that the start offset begins at zero and the length must be at least one. You can also offset from the right side of the string using a negative offset in parentheses:

echo ${STR:(-5):4}5678

An extremely useful resource for all you'll need to know about Bash string manipulation is here: https://tldp.org/LDP/abs/html/string-manipulation.html