Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to restrict length of string present in a line using linux

Tags:

linux

bash

ubuntu

I have data of the following form:

num1    This is a string
num2    This is another string

I want to limit length of all strings which are after the first tab..such that length(string)<4. Therefore, the output which I get is:

num1    This is a string
num2    This is another 

I can do this using python. But I am trying to find a linux equivalent in order to achieve the same.

like image 749
Jannat Arora Avatar asked Nov 08 '13 22:11

Jannat Arora


People also ask

How trim a string in Linux?

To cut by character use the -c option. This selects the characters given to the -c option. This can be a list of comma separated numbers, a range of numbers or a single number. Where your input stream is character based -c can be a better option than selecting by bytes as often characters are more than one byte.

How do I trim a string in bash?

Example-2: Trim string data using `sed` commandUse sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]]. $ echo "$Var are very popular now."

How do you find the length of a string in Linux?

We can use the # operator to get the length of the string in BASH, we need to enclose the variable name enclosed in “{ }” and inside of that, we use the # to get the length of the string variable. Thus, using the “#” operator in BASH, we can get the length of the string variable.

How long can a command line be Linux?

The maximum line length is 4096 chars (including the terminating newline character); lines longer than 4096 chars are truncated. After 4095 characters, input processing (e.g., ISIG and ECHO* processing) continues, but any input data after 4095 characters up to (but not including) any terminating newline is discarded.


1 Answers

In bash, you can use the following to limit the string, in this case, from index 0 to index 17.

$ var="this is a another string"

$ echo ${var:0:17}

this is a another
like image 130
jramirez Avatar answered Sep 21 '22 03:09

jramirez