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

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/bash

STR="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:

1
12345
123456789
6789
9

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

To read a file, fetch the first 8 characters repeatedly for each line, and print them to the terminal, use a while loop like this:

while read LINE
    do echo "${STD:0:8}"
done < "/path/to/the/text_file"

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


With cut:

$ cut -c-22 file
0000000000011999980001
0000000000021999980001
0000000000031999980001
0000000000041999980001
0000000000051999980001
0000000000061999980001

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/' file
0000000000 011999980001
0000000000 021999980001
0000000000 031999980001
0000000000 041999980001
0000000000 051999980001
0000000000 061999980001

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

awk 'BEGIN{FIELDWIDTHS="22"} {print $1}' file

echo "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

Tags:

Linux

Unix

Awk

Cut