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

LinuxUnixAwkCut

Linux Problem Overview


I am trying to get the first 22 characters from a unix data file.Here is my data looks as below.

First 12 characters is column 1 and next 10 characters is 2nd column.

000000000001199998000180000     DUMMY RAG #         MFR NOT ST            1999980    ZZ-            0        0              0ZZ-
000000000002199998000180000     DUMMY RAG #         MFR NOT ST            1999980    ZZ-            0        0              0ZZ-
000000000003199998000180000     DUMMY RAG #         MFR NOT ST            1999980    ZZ-            0        0              0ZZ-
000000000004199998000180000     DUMMY RAG #         MFR NOT ST            1999980    ZZ-            0        0              0ZZ-
000000000005199998000180000     DUMMY RAG #         MFR NOT ST            1999980    ZZ-            0        0              0ZZ-
000000000006199998000180000     DUMMY RAG #         MFR NOT ST            1999980    ZZ-            0        0              0ZZ-

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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

Solution 3 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTejaView Question on Stackoverflow
Solution 1 - LinuxChris SeymourView Answer on Stackoverflow
Solution 2 - LinuxKentView Answer on Stackoverflow
Solution 3 - LinuxJody BruchonView Answer on Stackoverflow