How to cut first column (variable length) of a string in shell ?
ex of string :
23006 help.txt
I need 23006 as output
The cut command in UNIX is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character and field. Basically the cut command slices a line and extracts the text.
To show you an example of the cut command with tab delimiter, we need to first change our delimiter from ":" to tab, for that we can use the sed command, which will replace all colon with \t or tab character. After that, we can use, and then we will apply the cut command of Linux to extract the first column.
Another way to count the length of a string is to use `expr` command with length keyword. The following commands will assign a value to the variable, $string, store the length value to the variable, $len and print the value of $len.
Many ways:
cut -d' ' -f1 <filename # If field separator is space cut -f1 <filename # If field separator is tab cut -d' ' -f1 <filename | cut -f1 # If field separator is space OR tab awk '{print $1}' filename while read x _ ; do echo $x ; done < filename
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With