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.

0
votes
3answers
57 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
72 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
22 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
45 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
45 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
20 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
33 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 ...
-2
votes
0answers
17 views

Problem with GCC compiler [migrated]

I am using Arch Linux and when I am compiling and running the following C code, #include <stdio.h> #include<string.h> int main(void) { char s1[]="Hello"; char s2[]="World"; ...
0
votes
1answer
27 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
511 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
72 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
52 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
114 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
138 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
23 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
92 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
323 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
438 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
168 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
25 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
701 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
166 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
71 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
137 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
72 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
311 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
50 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
61 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
62 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
45 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
95 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
128 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 | ...
0
votes
2answers
43 views

How to check if first column's string of file1 exist in the file2?

I have two files, file1.txt contains strings separated by commas: 1.1.1.1,string1,comment1 7.7.7.7,string3,comment3 2.2.2.2,string2,comment2 88.88.88.88,string4,comment4 999.999,999,999,string5,...
9
votes
2answers
829 views

Build a command dynamically

I'm working on a script and I need to build the tar command dynamically. Here are two examples to illustrate what I'm trying to do : #!/bin/bash TAR_ME="/tmp" EXCLUDE=("/tmp/hello hello" "/tmp/...
1
vote
1answer
179 views

Grep global find replace [duplicate]

Is there a way to grep for a regular expression on an Ubuntu server and then find and replace it with another string? I am using the following command right now, but it doesn't account for actually ...
-1
votes
3answers
66 views

Remove a whole line beginning with a pattern occurring anywhere after another pattern

How can I remove any line beginning with --- occurring in any line number after a line containing # Match (the first occurrence in any line number)? EDIT: The relationship between #Match with the ...
3
votes
5answers
470 views

Finding numbers in a command output and save it as variables

In a bash script, I wish to get some numbers from the output of a command and store them in variables. An example of the command output: 25 results [22 valid, 2 invalid, 1 undefined] I would like to ...
4
votes
7answers
4k views

How to compare 2 strings in UNIX shell script? [duplicate]

I have a variable which stores a string, the output of a sed command. I want to execute a set of commands only if this string value matches either of the 2 other strings. I used the below code. #! /...
3
votes
4answers
323 views

Extract addresses separated by semicolon and print each address in a line

I have a file with the following input. The numbers separated by dots represents addresses. Any number in address can be one or more digits as follows: [112.112.112.112;3.3.3.3;44.44.44.44] [6.6.6.6;...
1
vote
4answers
52 views

How to process a column's multiple strings

I have a comma separated file that looks similar to his format: aa.com,1.21.3.4,string1 string2 K=12 K2=23 K3=45 K4=56 bb.com,5.6.7.8,string1 string2 K=66 K2=77 K3=88 K4=99 I want to take ...
2
votes
5answers
578 views

Grep specific data from 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
2answers
165 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 ...
6
votes
3answers
2k views

Search for special characters using grep

I want to search for the lines that contains any of the following characters: : / / ? # [ ] @ ! $ & ' ( ) * + , ; = % I am familiar with grep. But with this set of characters, it is a bit tricky ...
2
votes
2answers
280 views

How to execute grep -v -e 'expr1' and grep -e 'expr2' at the same command?

I have the following command to extract string that do not start with www. and do not contain /: grep -v -e '^www\.' -e '/' test2.txt But I want the above in addition to matching somestring....
0
votes
3answers
643 views

How to remove filename extension from a list of filenames in bash

I am trying to remove the filename extension from a list of files I've generated with the following bash script: #!/bin/bash file_list=$(find . -type f) #assuming that the files are stored in the ...
0
votes
2answers
43 views

re-write a string with text and numbers onto a newline and insert text

In Short, I want to split single line output string into multiple rows and label each raw with the hostname. Hostname ProcessName CPUCore output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host '...
1
vote
1answer
296 views

How to search for a pattern using grep?

I want to search a file to check for patterns like this: text1,text2,[1.2.3,3.4.5,6.7.8,etc] exist. The number of commas inside the square brackets is undefined. I tried the following command but it ...
3
votes
2answers
254 views

Using pipe STDOUT as a variable?

Fairly new to Bash, about a grueling week. I love it so far and really like long chaining pipes. What I'm noticing though is that I have to break the pipe if I need to use the STDOUT as a variable. ...
0
votes
4answers
35 views

How to replace the line with an exact matching of a string

I have a file with this content: dskldfsd.com domain domain.com sub.domain.com domain.main.com anythinghere I want to delete the line which contains the word domain. Using grep, this command ...