Questions tagged [string]

String manipulation: extracting a part of a string, text replacement, formatting to a given width, etc.

Use this tag for questions about string manipulation, especially in but not limited to shell scripts (use or the specific tag for the shell or other language you're using as appropriate).

For questions about batch processing of text files, see . If the string is a file name or path, use in addition to or instead of . For questions about bypassing the interpretation of special characters, see .

781 questions
561
votes
25 answers

How to generate a random string?

I would like to generate a random string (e.g. passwords, user names, etc.). It should be possible to specify the needed length (e.g. 13 chars). What tools can I use? (For security and privacy reasons, it is preferable that strings are generated…
landroni
  • 10,288
  • 12
  • 30
  • 48
316
votes
12 answers

How can I test if a variable is empty or contains only spaces?

The following bash syntax verifies if param isn't empty: [[ ! -z $param ]] For example: param="" [[ ! -z $param ]] && echo "I am not zero" No output and its fine. But when param is empty except for one (or more) space characters, then the…
maihabunash
  • 6,973
  • 19
  • 64
  • 80
265
votes
13 answers

Delete the last character of a string using string manipulation in shell script

I would like to delete the last character of a string, I tried this little script : #! /bin/sh t="lkj" t=${t:-2} echo $t but it prints "lkj", what I am doing wrong?
user3581976
  • 3,095
  • 3
  • 18
  • 19
200
votes
9 answers

Split string by delimiter and get N-th element

I have a string: one_two_three_four_five I need to save in a variable A value two and in variable B value fourfrom the above string I am using ksh.
Alex
  • 2,255
  • 3
  • 17
  • 12
190
votes
5 answers

How to uppercase the command line argument?

I searched SO and found that to uppercase a string following would work str="Some string" echo ${str^^} But I tried to do a similar thing on a command-line argument, which gave me the following error Tried #!/bin/bash ## Output echo…
mtk
  • 26,802
  • 35
  • 91
  • 130
147
votes
13 answers

Replace space with new line

How can I replace spaces with new lines on an input like: /path/to/file /path/to/file2 /path/to/file3 /path/to/file4 /path/to/file5 etc... To obtain the following: /path/to/file /path/to/file2 /path/to/file3 /path/to/file4 /path/to/file5 Note I'm…
laconbass
  • 4,339
  • 4
  • 16
  • 20
137
votes
3 answers

grep on a variable

Let's say I have a variable line="This is where we select from a table." now I want to grep how many times does select occur in the sentence. grep -ci "select" $line I tried that, but it did not work. I also tried grep -ci "select" "$line" It…
gkmohit
  • 3,219
  • 8
  • 27
  • 30
119
votes
8 answers

How to get the first word of a string?

When I echo * I get the following output: file1 file2 file3 ... What I want is to pick out the first word. How can I proceed?
vdegenne
  • 1,676
  • 3
  • 16
  • 20
94
votes
5 answers

Splitting string by the first occurrence of a delimiter

I have a string in the next format id;some text here with possible ; inside and want to split it to 2 strings by first occurrence of the ;. So, it should be: id and some text here with possible ; inside I know how to split the string (for instance,…
gakhov
  • 1,095
  • 2
  • 8
  • 9
65
votes
14 answers

Add thousands separator in a number

In python re.sub(r"(?<=.)(?=(?:...)+$)", ",", stroke ) To split a number by triplets, e.g.: echo 123456789 | python -c 'import sys;import re; print re.sub(r"(?<=.)(?=(?:...)+$)", ",", sys.stdin.read());' 123,456,789 How to do the same with…
user2496
  • 1,141
  • 1
  • 9
  • 8
57
votes
5 answers

how to split the string after and before the space in shell script?

I am having a variable which shows on echo like this $ echo $var 129 148 I have to take only 129 as output. How will I split 129 and 148?
surbhi
  • 641
  • 2
  • 7
  • 5
50
votes
4 answers

Test if a string contains a substring

I have the code file="JetConst_reco_allconst_4j2t.png" if [[ $file == *_gen_* ]]; then echo "True" else echo "False" fi I test if file contains "gen". The output is "False". Nice! The problem is when I substitute "gen" with a variable…
Viesturs
  • 933
  • 3
  • 10
  • 15
50
votes
1 answer

Remove last character from string captured with awk

I need to remove the last character from a string in this command: sudo docker stats --no-stream 39858jf8 | awk '{if (NR!=1) {print $2}}' The result is 5.20% , I need remove the % at the end, giving 5.20. Is it possibile to do this in the same…
Steph
  • 547
  • 1
  • 4
  • 6
46
votes
9 answers

How to check if $PWD is a subdirectory of a given path

E.g. check if $PWD is a subdirectory of /home. In other words I'm searching for a bash string operation to check if one string starts with another.
Tobias Kienzler
  • 9,184
  • 13
  • 65
  • 106
1
2 3
52 53