Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut first column (variable length) of a string in shell

Tags:

How to cut first column (variable length) of a string in shell ?

ex of string :

23006 help.txt

I need 23006 as output

like image 444
Harsha K L Avatar asked Dec 27 '12 08:12

Harsha K L


People also ask

How do you cut a string in Unix shell script?

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.

How do you cut the first column in Unix?

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.

How do you find the length of a string in a shell script?

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.


1 Answers

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 
like image 195
anishsane Avatar answered Oct 23 '22 07:10

anishsane