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
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

Questions tagged [string]

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

0
votes
0answers
31 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
44 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
26 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/...
0
votes
1answer
22 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; ...
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 ...
-1
votes
1answer
23 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 ...
0
votes
1answer
30 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 ...
3
votes
4answers
171 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
0answers
42 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 ...
0
votes
1answer
36 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 ...
2
votes
2answers
179 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
58 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 ...
1
vote
1answer
32 views

Does one string contain another in fish shell?

I am working on my fish.config for using the fish shell. I am trying to compare strings using bash syntax but fish doesn't accept the syntax. There is clearly another way to do it. Any suggestions ...
30
votes
1answer
2k views

Why won't the strings command stop?

The strings command behaves weirdly, apparently it doesn't stop writing to a file even if drive run out of space. Or perhaps I'm missing something? I run the following: # strings /dev/urandom > ...
3
votes
4answers
276 views

How to remove trailing forward slash

For each line in my file, if the line ends with / I want to remove it. How to do this? My attempt: sed -e "s/$\/$//" myfile.txt > myfile_noslash.txt Did not work.
-1
votes
1answer
26 views

Search for a string in lots files in a directory, if found, save the file name and line

I have several files, with different names. no specific format for them. But all are .txt. For example: example.txt flower.txt abc.com.txt They are in the same directory. I want to search for a ...
0
votes
3answers
165 views

Modifying a shell variable with regex (bash)

I have a shell variable for example. a="big little man". How do I use regex in bash print out the variable with only the middle word capitalized? (big LITTLE man) I can do it by separating the ...
3
votes
3answers
111 views

How to search for a string from all the man pages

What command would show all lines containing the string “brace expansion” (ignoring case) in the man pages for Bash? I tried man -wK . | grep "[Bb]race [Ee]xpansion" I have got series of "couldn't ...
1
vote
2answers
27 views

sed: strip enclosing square brackets

I have this file: [default] region = us-east-1 output = json [profile automation] region = us-east-1 output = json [profile qa] region = us-east-1 output = json I need to extract only the profile ...
-4
votes
2answers
50 views

Finding specific string in XML file and storing in another file [closed]

Text in input file is like this <title> <band height="21" isSplitAllowed="true" > <staticText> <reportElement x="1" y="1" ...
0
votes
3answers
47 views

How can a conditional statement be used in a piped command? [closed]

The data being processed will follow specific patterns. In this case I'd like to isolate all characters found in the third column and return any characters found after the "-" but only if that ...
0
votes
2answers
27 views

How can I add incremental integer row in a text file using grep or awk command in linux terminal?

I have a text file which contains 10 million+ lines. The file looks like as bellow: 1902400023 1902400029 1902400031 1902400032 I wanted to add a heading, incremental ID number and a constant ...
-2
votes
1answer
34 views

Unable to search a string using more pager

I opened a file using the more pager,i was trying to find a string in this file using / as shown in the figure below,but it is saying that pattern not found even though the string i have searched is ...
0
votes
1answer
32 views

korn shell Unable to substitute ' ' (2 quotes) with ' (single quote) in a string variable

I am trying to substitute ''2019-01-14'' to '2019-01-14' in a varible $STG. I tried various techniques echo ${STG//''/'} echo $STG | sed -e s/'\'\''/'\''/g many more none of the equations are ...
2
votes
1answer
526 views

How to grep '$$$$$'? [duplicate]

I want to check the absence of the following sequence of characters $$$$$ (i.e., 5 dollar signs) in a json file using grep as it has been used instead of comma to separate fields and I need to make ...
4
votes
3answers
80 views

Replacing all instances of string containing hyphen

Running the following command: echo "hello-world" | tr "hello-world" "Hello World" Returns Heddd-ddddd. I want it to return Hello World. I believe this is because tr is interpreting the SET1 ...
0
votes
1answer
420 views

Bash: Set variable equal to a string substitution [duplicate]

I'm trying to replace a substring in $1 with another, AND set it to a variable. (saved as testStringReplacement file) #!/bin/bash secondString="Sara" myString= "${1/Suzi/$secondString}" echo "...
4
votes
2answers
121 views

What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?

I have encountered comparisions of variables to string literals multiple times over the years which had one character prefixing the variable and the literal, e.g. if [ "x$A" = "xtrue" ]; then in ...
1
vote
1answer
616 views

How do I split a string into two strings?

I already know how to put h4 h7 h8 h9 h7 into an array with space as delimiter, but after that I am trying to separate the h from the numbers to compare the numbers to each other. How can I do split ...
0
votes
1answer
31 views

Finding and replacing text when there are custom elements within [duplicate]

I have about 450 html files I need to edit. I have the directory they all are in, so that narrows things down from the perspective of searching. What needs to be changed is a bit of known text, some ...
0
votes
1answer
25 views

bash string increase from vda to vdb in a loop

I would like to increase a string by one, for example, from vda to vdb, from vdb to vdc and so on. I was able to do it in one of the languages in the past by doing something like: $c="vdb"+1 and c ...
0
votes
1answer
200 views

dollar sign inside eval string in bash

I have hundreds of sub-directories which contain a dollar sign (i.e., $) as the 1st character in their names, each of which needs to be visited. These sub-directories can not be renamed. The bash ...
2
votes
2answers
570 views

How to filter a string against the characters from a variable?

I am trying to remove special characters (by passing them as a variable) from a string variable below command able to remove single characters not multiple? string=#@$AAA%* a=#$@%* #...
5
votes
4answers
455 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 }') = ...
2
votes
3answers
189 views

'rename' with expression|replacement with a leading '-' (hyphen|minus)

I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried: rename . -bar. xyz_* which resulted in: rename: ...
0
votes
1answer
27 views

find and replace a substring and change to uppercase

I think it's best done with an example Contents of my file name="apple" parameter1="value1" parameter2="value2"; name="orange" parameter1="value1" parameter2="value2"; name="tea" parameter1="value1" ...
10
votes
3answers
752 views

How to POSIX-ly count the number of lines in a string variable?

I know I can do this in Bash: wc -l <<< "${string_variable}" Basically, everything I found involved <<< Bash operator. But in POSIX shell, <<< is undefined, and I have ...
3
votes
1answer
171 views

Does Bash imitate Locale-Specific Translation from C?

Bash manual mentions that 3.1.2.5 Locale-Specific Translation A double-quoted string preceded by a dollar sign (‘$’) will cause the string to be translated according to the current locale. ...
3
votes
2answers
74 views

Prepare arguments containing quoted string in variable

In a Bash script, I call a program like this in several places: numfmt --suffix=" B" --grouping 231210893 Where the number is different every time, but the other parameters stay the same. I would ...
0
votes
1answer
319 views

Get string between two strings

I need to get inner text from this string [%L10n.msg('foo')%] so text between [%L10n.msg(' and ')%] so in this example I should get foo. I found lot of examples but no one of them worked, I tried ...
1
vote
2answers
86 views

Append strings to multiple space-separated values in a variable

I have a variable VAR="XYZ YZA ZAB" declared and I would like to append some strings next to all the words in that variable. For example: I want to append .file1 to each space-separate substring in ...
0
votes
2answers
973 views

exact string matching for multiple strings with grep

I am looking for exact string matches for multiple strings using grep. Lets say I want to find the following three strings in a large data file that has about 10 Million rows and 10 columns. I ...
0
votes
3answers
51 views

determine if output is string or integer

I am trying to write a script which connects to a database then run a query that returns count of rows in a table. sometimes database or table have problems and instead of an integer they return an ...
0
votes
3answers
119 views

Cut path to first four directories

I have several projects where I have to use the same command. The projects always are in /home/[username]/public_html/[prjectname]/ I'd like to run this command from every directory of my projects, ...
1
vote
4answers
68 views

RegEx for matching strings in 2nd and 5th columns using grep

I have a file (test.txt) that contains string such as: name1,6.5.8.12,specs1,version1,['66.55.89.12'] name2,19.13.13.159,specs2,version2,['13.13.173.59'; '14.19.232.42'] name3,141.101.196.40,specs3,...
0
votes
2answers
49 views

How to delete text from variable after matching some pattern?

I wish to delete some text before some pattern. For example: VAR="This is a test script text and real script text." PATTERN="test " And the desired output that I want is: NEW_VAR="script text and ...
0
votes
3answers
111 views

sed command to replace and write

I am trying to replace the following string with another one from a file which contains few lines. "schema" : "AAAAA", TO "schema" : "BBBBB", Currently, I am doing the following command to get the ...
0
votes
1answer
290 views

Extract substring after delimiter using GNU awk or awk [duplicate]

I would like to extract substring after colon delimiter for my practical needs using GNU awk utility. For example I can extract the temperature of hdd value using cut as follows hddtemp /dev/sda | ...