Questions tagged [string]

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

Filter by
Sorted by
Tagged with
0
votes
2answers
35 views

Find string2 when exist string1 in file

I have a configuration file (from Nagios) with the following structure: define service{ use pruebaspre-service,srv-pnp host_name server1.es servicegroups ...
0
votes
1answer
40 views

Extract filename from path string

I have a string like so /path/to/some/file.txt I want to extract only the file's name, without extension: file The problem is that this path is chosen randomly and I can't hardcode it into a ...
0
votes
2answers
22 views

How to delete everything before the occurrence of a regex pattern per line in a bash variable

I've got a bash variable containing several lines of text, which includes IP addresses, and I need to remove 'everything' before the last IP address occurrence in the same line. This: 43.12.40.53 ...
0
votes
2answers
18 views

Concatenate strings passed as arguments with null character as delimiter

I'm looking for a CLI tool that takes an arbitrary number of arguments $a, $b, $c, $d, etc., and writes $a"\0"$b"\0"$c"\0"$d to stdout. Is there a standard tool that can do this that I can just use in ...
0
votes
1answer
25 views

group list by last string of each line

I'm having a list of strings (sorted IP address ranges) like this: 10.100.0.0-10.100.255.255 External: 2.2.2.2 10.120.0.0-10.255.255.255 External: 2.2.2.2 10.0.0.0-10.255.255.255 External: 3.3.3.3 ...
3
votes
2answers
29 views

Remove trailing whitespace in df command

I have the following df command: > df -h --output=used,avail,pcent / Used Avail Use% 5.9G 12G 34% Notice how there is a space before Used and 5.9GB So I did the following: > df -h --...
2
votes
2answers
110 views

Convert array type variable to string type with space delimiter

If i have an array like arr[0]=2019-06-26 arr[1]=15:21:54 How can i convert that into a string whose value is '2019-06-26 15:21:54'
0
votes
2answers
28 views

How to grep a string with any two numbers that match or that have the first number one less than the second number?

I have a tab delimited file with four columns. I would like to grep for the lines that have a specific pattern in column 1, where it says apple M of N.  I only want to extract the lines that have the ...
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="...
6
votes
4answers
640 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
48 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 ...
12
votes
4answers
989 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?
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
61 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:...
0
votes
3answers
56 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
56 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 ...
0
votes
1answer
30 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
56 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 ...
0
votes
2answers
57 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
1answer
44 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
4answers
117 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 ...
0
votes
0answers
40 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
35 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
46 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
46 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
54 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
36 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
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 ...
0
votes
1answer
38 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
182 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
71 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
40 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
262 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
62 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
133 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
412 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
34 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
568 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
174 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
79 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
55 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
50 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
41 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
37 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
45 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
559 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
131 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
1k 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 "...