Stack Exchange Network

Stack Exchange network consists of 174 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
1answer
25 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
0answers
32 views

Extracting a substring between quotes that also contains quotes?

I know that "get text between quotes" has been asked to death both here and on other related sites. This is however not one of those questions. This is a more specific sub-question which I have not ...
0
votes
1answer
27 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
20 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 ...
-1
votes
2answers
41 views

BASH Creating unique username [closed]

I want to create a unique username based on the following bash script. The variable "user" should change based on the following criteria below. How would you achieve this? tput cup 18 18 echo "...
0
votes
1answer
38 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
130 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
414 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
149 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
22 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
667 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
160 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
65 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
31 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 ...
0
votes
2answers
50 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
37 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
42 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
39 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
51 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
40 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
76 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
45 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
34 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
635 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
82 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
65 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
362 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
2k 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
295 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
51 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
492 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
67 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
1k 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
234 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
350 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
41 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
101 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 ...
2
votes
2answers
176 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
33 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 ...
2
votes
2answers
130 views

What is the concept of Shortest Sub-string Match in Unix Shell?

I'm using following script for Shortest Sub-string Match in String handling. filename="bash.string.txt" echo ${filename#*.} It gives following output. string.txt Here is an explanation of above ...
-1
votes
1answer
202 views

Empty string is a file? ( if [ ! -f “” ] )

The script is called isFile.sh and looks like this: #!/bin/sh echo $1 echo $2 if [ ! -f $1 ]; then echo "$1 (arg1) is not a file" fi if [ ! -f $2 ]; then echo "$2 (arg2) is not a file" fi ...
1
vote
1answer
177 views

Match wildcarded string in case statement using sh shell

I'm having trouble matching a wilcarded string in the sh shell. The code is part of a configure.ac so it usually gets the lamest shell available. I can't count on the Bash shell. The code first ...
1
vote
7answers
126 views

Create PHP array from letters

I would like to create a PHP array from the Serbian alphabet. Out of а б в г д ђ е ж з и ј к л љ м н њ о п р с т ћ у ф х ц ч џ ш a b c č ć d dž đ e f g h i j k l lj m n nj o p r s š t u v z ž ...
2
votes
4answers
218 views

How to extract columns before and after a string ocurrence

I have a text file. That looks like: www.ac.com has address 6.1.1.146 www.ac.com is an alias for ac.com. www.ac.com is an alias for ac.com. www.ba.net is an alias for www-bn.gs.ba.com. www-bn.gs.ba....
1
vote
4answers
95 views

Zsh equivalent of bash's `expr index “abcdefghijklmnopqrstuvwxyz” xyzd`

What would be Zsh equivalent of bash's expr index "abcdefghijklmnopqrstuvwxyz" xyzd I am trying to find index of substring.
0
votes
2answers
57 views

How to query all lines in a file that are NOT equal or suffix of any name in another file

I have two files file1 and file2. The two files contain names. One name in each line. I want to query all names in file2 that are not equal to or a suffix of name in file1. Note that the prefix is ...
-1
votes
2answers
576 views

grep to extract lines that contains full domain names from a file [closed]

I have a large file that contains domain names in the form of: domain.com sub.domain.com sub.domain.co.uk domain.co.uk I want to extract main domain names (no sub domains) with top level domain name ...
1
vote
3answers
45 views

Bash compound substring expansion [duplicate]

I'm aware I can use awk to parse on multiple delims, but that spawns subprocesses. I wanted to know if compound/nested bash parameter expansion is possible. I have PDFs in a directory named like "...
1
vote
4answers
62 views

Search and replace using a custom utility

I want to run a utility for replace values matching a regular expression. This means for every match of a regular expression, call the utility with the characters comprising the match. The output of ...
0
votes
1answer
46 views

Extracting lines that has match in the first column? [duplicate]

I have two files: file 1: a,txt1,v1 b,txt2,v2 c,txt3,v1 d,txt4,v2 file2: a,txt5,v2 b,txt6,v1 xc,txt7,v1 xd,txt8,v2 I want to refine the files. I only need the lines where the 1st column in file1 ...