Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [string]

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

1
vote
4answers
892 views

How to deal with multiline strings and string interpolation?

Say I want to have a template somewhere which is multiline string: I have some text with ${placeholders} in this and some ${different} ones ${here} and ${there} What would be my best way of ...
0
votes
0answers
16 views

remove pattern from string using sed and a variable [duplicate]

I want to remove a pattern from a string (using sed and pipes), but this pattern is not always the same and is defined previously on a variable. this example mimics the desired behavior toremove="...
2
votes
3answers
44 views

How prepend appropriate amount of 0's to numbers contained in image names?

I've got a folder /User/me/Desktop/folder/images with files like key 1--name.jpg, key 10--name1.jpg, etc. The files are sorted incorrectly when fed to my site so I get an arrangement like key 84--name....
6
votes
4answers
472 views

Write a function that checks if a string starts with or contains something

I want to write a function that checks if a given variable, say, var, starts with any of the words in a given list of strings. This list won't change. To instantiate, let's pretend that I want to ...
1
vote
2answers
40 views

How to split string by string (multi-character) separator into an array?

Basically I'd like to split a string into an array by a string separator like sep. for example. Is there native bash way to do this without using tools like sed, awk, etc? I know I can split a string ...
5
votes
4answers
495 views

How to treat a command output as text

I need to compare a command output with a string. This is the scenario: pvs_var=$(pvs | grep "sdb1") so pvs var is: /dev/sdb1 vg_name lvm2 a-- 100.00g 0 if [[ $($pvs_var | awk '{ print $2 }') = ...
12
votes
4answers
878 views

How to split a string in two substrings of same length using bash?

I would like to split a string into two halves and print them sequentially. For example: abcdef into abc def Is there a simple way to do it, or it needs some string processing?
6
votes
7answers
26k views

How to split a string into an array in bash

I have a problem with the output of a program. I need to launch a command in bash and take its output (a string) and split it to add new lines in certain places. The string looks like this: battery....
0
votes
2answers
316 views

Searching for URLs with subdomains using grep

I have a file with content similar to: mail.google.com srv1.mail.google.com google.com facebook.com yahoo.com tt.twitter.com I want to search for lines with subdomains. I used this command: grep -e ...
0
votes
0answers
21 views

How can I escape braces in string to be fed to sub process [migrated]

import subprocess profile_val = 'ishan' cmd = 'instaloader --post-metadata-txt="{likes} likes, {comments} comments, {caption}" --login=blabla_id --fast-update' + ' ' + profile_val) ...
186
votes
21answers
160k views

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 ...
1
vote
2answers
27 views

Bash Split Variable into 3 Variables based off Delimiters?

Trying to split a string var with the following pattern: TestString="Artical Title Here (ClassYear) [Author]" I was able to get the title into a Var using cut as such: ATH="$(cut -d'(' -f1 <&...
1
vote
4answers
57 views

Replace string in multiple files (same directory) with condition

Example: File1: Name: abc Number: 325 Code: 0 File2: Name: xyz Number: 578 Code: 0 File3: Name: ttt Number: 356 Code: 0 File4: Name: nnn Number: 228 Code: 0 File5: Name: bnb Number: 434 Code:...
7
votes
3answers
4k views

List variables with prefix where the prefix is stored in another variable

I'm trying to list all the variables with a certain prefix, but that prefix is dynamically determined. For example: prefix=apple_ apple_one=a1 apple_two=a2 If I simply do: echo ${!apple_@} I can ...
2
votes
5answers
710 views

Grep specific data from a string [duplicate]

Here is the data: Valid from: Fri Nov 24 12:11:28 EST 2017 until: Sun Nov 24 12:11:28 EST 2019 Valid from: Fri Feb 24 13:21:19 EST 2017 until: Thu Feb 24 13:31:19 EST 2022 Valid from: Tue Dec 20 12:...
0
votes
3answers
40 views

How to find and replace seemingly a tab char using sed?

I have a line looks like this: some.com *.some.com The space is seemingly a tab space because when I move the keyboard arrow, one move will move me to the end of it (it does not seem multiple ...
0
votes
2answers
39 views

How to find more than one space and replace it with one space?

I want to find repeated spaces (more than one) and replace them with one space. I tried: sed -e 's/ []+/ /g' myfile.txt > myfile2.txt This resulted in an empty file. Why? and how to achieve my ...
16
votes
6answers
75k views

How to capture error message from executed command?

I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and ...
0
votes
1answer
29 views

How to search for a newline that is not preceeded by } and remove it

I want to use sed to search for line breaks that are not preceded by }. The regex for identifying theses cases is: [^}]$ . What I want to say: If the line ended without } then remove the new line. ...
0
votes
3answers
55 views

How to keep the content of a replaced string with sed

I am trying to use sed to replace a line of text that will keep the found text intact and add a colon to the end of it. specifically, in a file there are a bunch of lines in this format ...
4
votes
2answers
94 views

Extract Sub-Directory Path from Partially Known Directory

Lets say I've got the following directory structure: base/ | +-- app | | | +-- main | | | +-- sub | | | +-- first | | | | | +-- tib1.ear | ...
-1
votes
1answer
705 views

String comparison not working in bash

Simple string comparison is not working in bash. What's wrong with this : $ cat compare.sh function compare { BEFORE_STATUS=enabled AFTER_STATUS=disabled if [ $BEFORE_STATUS = $AFTER_STATUS ] then ...
1
vote
3answers
194 views

How to print each line of a file as many times as according to the number in the first column

Input_file: 1 string1 4 string2 2 string3 ... Output_file: 1 string1 4 string2 4 string2 4 string2 4 string2 2 string3 2 string3 ... Here's my code and it didn't work. #!/bin/bash N=0 cat ...
0
votes
2answers
45 views

Bash String Manipulation and Assignment

How can I manipulate a string and then assign it to a variable? This string manipulation works as I want: echo ${dir:2:5} | sed 's/[.]$//'; but: var x = ${dir:2:5} | sed 's/[.]$//'; and x = ${...
0
votes
4answers
96 views

Removing lines whose first column is duplicated

I have a file where strings are separated by a special string (not a comma or delimiter) e.g. <vvv>. I want to check if all strings in the first field are unique. If duplicate rows for the same ...
234
votes
8answers
459k views

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 ...
0
votes
1answer
41 views

How to capture two regex

In this command, I want to extract strings that are like *.com or *.net, where * is not a dot. I wrote this: grep -P -E '^[^.]+\.com$ |^[^.]+\.net$' example.csv I get this error: grep: conflicting ...
0
votes
0answers
37 views

sed command to insert between two strings that are returned from another sed command

I have a YAML file that's about 25K lines and I have about 450 instances where I need to insert between two specific lines. Here's an example of the file entries: - changeSet: id: ...
0
votes
0answers
34 views

How to change the newline position [duplicate]

I have a file that contains string in each line. I used this file as an input in a program that reads them automatically. The program seem to not be able to read the strings correctly. I tried to ...
1
vote
0answers
33 views

Is there a command specifically designed to manipulate file path strings, such as changing foo/bar/baz -> bar/baz?

I'm trying to modify the output of git diff --name-only from a subdirectory of a git project such that it only contains relative paths in the current directory. I know how to do this with sed/grep, ...
0
votes
1answer
45 views

Does sort -u convert case?

If I have a file with some words starting with Capital letters and some starts with Small letters and some start with numbers or special characters. Does using sort -u input > output convert the ...
0
votes
1answer
38 views

Convert a string to date format

In Unix i have a file which has information like below $ cat date.txt Mon Apr 8 21:00:42 I Need to convert this as 2019-04-09 00:29:22. Example: $ cat date.txt Mon Apr 8 21:00:42 -- expected o/...
3
votes
4answers
153 views

Variable output doesn't concatenate

I have a script that concatenates output from two different variables. The issue is that the output of both variables contains multiple rows. So, the output isn't what I expect. First variable and ...
32
votes
12answers
29k views

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....
0
votes
1answer
44 views

Extract lines from files containing a string but not another in that same line

I am using grep within a script to obtain a list of filenames of the files that contain a certain string. The basic instruction would be this one within a do loop: if grep -q "$str" Inputs/inp$n; ...
4
votes
5answers
6k views

Bash while loop read from colon-delimited list of paths using IFS

I am trying to write a bash function that behaves similarly to the where builtin in tcsh. In tcsh, where lists all the builtins, aliases, and the absolute paths to executables on the PATH with a given ...
0
votes
1answer
32 views

How can I generate random answer string like "yes,no,maybe maybe not?

I dont know how can I generate random answer. exemple result: user:~$ ./question.sh do you love me ? user:~$ yes or user:~$ ./question.sh do you hate me ? user:~$ maybe I try to put all the ...
34
votes
3answers
137k views

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 ...
-1
votes
1answer
26 views

How do we get the readable string from big endian hex string

How do we get the readable string from hex string 0049006e00740072006f of BE which means as normal word, ie. we dont have to swap the byte pair per word tried with xxd in vain, cause I dont know ...
2
votes
2answers
247 views

How can I properly use the 'sed' s command in this particular scenario

I have a names.txt file where each line is of the form: xxxxxx random_string_of_characters 2015 where xxxxxx is a 6-digit number, and random_string_of_characters can be anything. I want to use ...
3
votes
4answers
178 views

Replace four times with sed

I want to replace the second, third, fourth and fifth dots in this string 2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1 with a commas, to get this result: 2019-03-...
0
votes
1answer
39 views

Preserve presence of last newline when assigning to variable [duplicate]

In bash, when an output is captured into variable using $(...), an annoying newline is appended. However, I have an output that sometimes ends in a newline, and sometimes not. I want the output to be ...
6
votes
2answers
8k views

Checking if PATH contains $HOME/mydir and adding it if not (all in a script) [duplicate]

Possible Duplicate: keep duplicates out of $PATH on source Some of my scripts are located in $HOME/mydir. Unfortunately, they are not accessible from elsewhere if this dir is not part of PATH. I ...
0
votes
1answer
37 views

How to extract the first column in a file with multi-character delimeter?

I have a file in which I use a string like <xxxxx> to separate the fields. This was a choice to avoid problems with the comma separated fields and other types of single characters due to the ...
0
votes
0answers
56 views

Escaping a string for a parameter inside a script [duplicate]

I have a shell script inside of which I call a command with several arguments. When I call the command from the terminal, the proper syntax is: command --foo='bar baz' Inside my script, I want to ...
2
votes
2answers
232 views

Bash replace string at multiple places in a file from command line

I have a file with the below content. I want to replace chrome with chromeBackup but not in all places. Name=chrome XXXXXXXXXXX Run=bin -app "chrome/chrome.sh" XXXXXXXXXXX Vol.apps/chrome=chrome ...
1
vote
1answer
61 views

Bash displaying gibberish after specific incoming request

I set up a new, basic Linux server (CentOS in this case) just for testing purposes. In there, without any firewalls and all that I'm running a Python web application. Basically I type python run.py ...
111
votes
5answers
194k views

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 ...
0
votes
2answers
54 views

I need to find a value in file between >STRING<

I have a file: example here: <hello>TestFile</hello> <test>2018-04-30</test> <greetings>mycomputer</greetings> **<Iwanthis>6.1.0</Iwanthis&...
1
vote
1answer
3k views

Using sed to replace a string with special chars with another string with special characters

I'm trying to automate switching out a bash prompt for another in .bashrc Original String: PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' ...