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
1answer
12 views

Zsh, splitting the expansion of %~

I'm at my wits end with this. I'm using zsh and trying to simply split the expansion of %3~ on the / character. This works fine with the expansion of $(pwd) and $PWD - both of which are also path-like ...
0
votes
2answers
37 views

How to safely escape a variable string (user input) in bash?

I have a very simple chat-like tool that runs within a GNU screen session. The screen window is split, the top part is running tail -f file.txt and the bottom part is running a script with the ...
0
votes
2answers
28 views

Grep F Parameter Not Complete Match

I'm trying to use the -F parameter on a grep to match a PHP variable. When I run: grep -F "$_SESSION['index']" -ri . though it is only matching: ['index'] How can I get it to also match the $...
0
votes
3answers
24 views

Replace a substring with another value in a main string

I have a string with a below pattern SrcWorkspaceName=abc_1234;SrcEndVer=1409;Lang=ENU,FRA,NLD I need the SrcEndVer value to be replaced from 1409 with other number. Here the number is stored in a ...
0
votes
3answers
49 views

Split a string by multiple delimeters in bash

There's a string: onetwothree.file.001.txt ; threefourfive.file.0.98.txt ; fivefoursix.file.14.txt I want to split it by . and ; removing the prefix before the filename, so that it looks like this: ...
1
vote
2answers
28 views

Cropping long strings in zsh

I would like to set my prompt to something like %1~ \#, which would show the current directory, without the full path (followed by % or #). But, if the name of the directory is long, I don't want all ...
0
votes
1answer
56 views

How To extract the whole string using awk?

Hi I have some directories and it looks like below: TBS890 |___________ A.ctab TBS345A |___________ A.ctab TBS567C |___________ A.ctab I'm interested in extracting some data from the A.ctab ...
1
vote
1answer
50 views

How to check that a variable has new lines characters only

How do I check the variable $last_cmd has new lines only and nothing else in Zsh? Ideally done via substitution, as this is part of a prompt script, so needs to be really fast. The following gives ...
1
vote
0answers
38 views

How do I write a function that has a double colon in a string without my current directory showing up in the string? [duplicate]

function awsl { echo "arg: $1" prfx='arn:aws:sts::' pstfx="$1:assumed-role/FOO/justin@bar.com" role="$prfx$pstfx" echo "role $role" saml2aws login --force --profile=default --role=$role } ...
2
votes
2answers
38 views

Search for a String in a file and save the string after it to a variable

Example: Lets say this is in a file: AB: Test CD: Testing EF: Rest I want a script to search for CD and save "Testing" into a variable. I've tried something with awk but somehow I'm not making any ...
0
votes
1answer
34 views

pass multiple argumens as single string to external program

I want to pass multiple arguments to a bash script as a single string parameter to an external executable (especially git) I found several answers that suggest something like this: "'$*'" "'$@'" ...
-1
votes
1answer
17 views

Space character within conversion specifier

NOW=$(date +%Y_%m_%d.%H:%M:%S) echo "NOW: $NOW" The goal is to replace the . between the d and % character with a space. How is this performed? Replacing the . with the space character produces and ...
2
votes
1answer
130 views

How to create sub-directories without HTTP or HTTPS from string parameter

I have ksh script, which is running the below command: mkdir -p down/$JENKINS_URL the thing is that some $JENKINS_URL (string) includes HTTP:// and in some HTTPS://, which is causing the creation of ...
0
votes
1answer
20 views

PHP: capture a multiline substring

I have a long multiline string (its in a file that I can load into a string), lets take this as example: <response status="success" code="19"> <result total-count="1" count="1"> <...
1
vote
1answer
23 views

String Manipulation using scripts missing 0 in truncation and substitution

I have the following shell commands and want to ensure the TEST_02 is printed as an output myVer=TEST_03 n=${myVer##*[!0-9]}; p=${myVer%%$n} lastver=$p$((n-1)) echo $lastver This gives an output ...
0
votes
1answer
43 views

How to pass a multiword string as argument in Linux [duplicate]

I am trying to pass a multiword argument in a function an echo the result in a simple fashion. Currently I am doing this: function myFunction { multiWordString="" for ((i=3; i<=$#; i++...
9
votes
4answers
699 views

Command to keep only a portion of JSON data from each line?

I have test.json file with different lengths of rows. Some fictitious example: { "a" : 123, "b": "sd", "c": 45, "d": 1, "e": "" } { "a": 5, "b": "bfgg", "c": "x4c", "d": 31, "e": "" } I want to just ...
4
votes
2answers
225 views

Universal sed command for finding and replacing values

Is there a way to use sed to substitute any string for any other string? Because sed uses a delimiter character for the find and replace expression, I can't figure out a way to write a sed command ...
1
vote
2answers
67 views

Replace multi line string with multi line string without escaping by hand

Say I have a text file text.txt and I want to replace a (multi-line) string that is contained in before.txt with another string that is contained in after.txt, how do I do that? (I dont want to use ...
0
votes
2answers
79 views

$STRING1 != “${STRING1/substr/}” in Linux

I have one string and I want to check if it contains the sub-string or not. So I am using this command. Below is the Linux shell script. I just want to know how the condition present in if statement ...
3
votes
2answers
153 views

Combinations of different letters in specific positions

In Unix, I am trying to create all possible combinations of these letter at 6 specific positions as follows: Position 1 - A or B or C Position 2 - A or C Position 3 - only C Position 4 - A or D ...
2
votes
2answers
735 views

Pattern matching in a zsh conditional expression

I am having trouble with pattern matching in zsh's hook function precmd. I have the following: precmd(){ local x='test' if [ $x = '*test*' ]; then echo 'hello' fi } which does not ...
3
votes
2answers
173 views

Why we can not compare . (dot) as a string value inside the round brackets of if statement?

Problem I have to compare a string "problem1.sh" with itself. It works fine in case of solution1.sh (given below) where I have used square brackets for comparison. However, it does not work in case ...
0
votes
1answer
37 views

More efficient zsh string parsing / array handling

Is there any more efficient way to do the following in zsh? I imagine that there might be ways to get rid of the intermediate array parameters a and/or b. The script gets some output from a command. ...
2
votes
2answers
122 views

Get the Unicode code point of a character in zsh

How can I get the Unicode code point of a character in zsh? e.g., if I have: s=abc How can I get the code point of s[2] (i.e. code point of "b" = 0x0062)?
0
votes
1answer
58 views

trying bash brace expansion (discrete numbers) using while read line and echo, doesn't expand

I'm going to give a short version of the kind of issue I'm facing. The real application I'm trying to write has many parts, so I've tried to isolate the part where I'm stuck with a small example. My ...
0
votes
0answers
8 views

Extracting two patterns from a file on multiple lines with output side by side

I have text report data formatted with an a identifier code on one line and then the account code associated with the identifier code on another line. Sometimes there are multiple account codes per ...
0
votes
1answer
64 views

How to mkdir or cp from Bash Script with Mixed Variable/String Literal Arguments?

I'm attempting to automate resetting a Minecraft world, and copying some custom functions back in... The reset just means deleting the world folder(s). Copying the custom functions to the right spot ...
2
votes
2answers
97 views

Easiest way to get `+` indented `tree` output

I need to replace indentation in tree output with + signs so that instead of this: $ tree --noreport dir dir ├── dir1 │ ├── file1 │ └── file2 └── dir2 ├── file1 └── file2 it'd print this:...
1
vote
2answers
72 views

Text processing of unix file

I have following data in a unix flatfile and I want to suppress duplicate values in first two columns and convert it as shown below: 1:x:4:3:2:y 1:x:7:9:l:z 1:0:3:j:k:m 2:9:r:s:6:u 2:m:y:5:7:9 2:u:7:...
-1
votes
3answers
41 views

Get last “block” (pattern) of log file

my logfile: ... ---------- text1 text2 ---------- text3 text4 ---------- And now I want to grep only the last block (between "----------") -> text3/text4 Thank you :-) Didn't find any solution... ...
0
votes
1answer
41 views

How to execute multiple commands from an input file and string it to a new output file

I want to execute multiple commands (one after the other) from an input.txt file and output it to a new .csv file. All the commands run successfully when executed individually, but nothing happens ...
-2
votes
2answers
56 views

How to duplicate a line and then comment the 1st occurrence

I would like to duplicate a line and comment one of the occurrences. This is more like keeping a copy before I make changes to the copied line (uncommented one). Input File : Hi , can you help me ...
0
votes
4answers
277 views

Find and replace after a string after a specific character

I have the following lines in my input file and I would like to replace all the text after "=" sign with a new string, say "good". Again, I would like to do this only for certain strings in the line....
1
vote
1answer
43 views

How to procedurally generate command in bash script using string interpolation with spaces?

I'm trying to create a simple script that uses a list of months like this: (Jan Feb) To generate and execute this command: python ExpenseManager.py -p Inputs/Jan\ 2019\ Debit.CSV Inputs/Jan\ ...
0
votes
1answer
18 views

How to grep/sed a defind part of string that has spaces

I have a log that contains records like: LineFFF 1234567 0002 @2018-06-12T17:02:50 (len:0) 99 OK : jAFoSSYut2iJLmrk6fdQPL4OMRgd1Ybzb6U9MG1GeWKWqXlYPyfzyMVb9zn3 LineFFF 1234568 0044 @2018-06-12T17:...
0
votes
3answers
49 views

Find a string in a file and then find the first line above containing another string with bash

How can I find a string in a file, then find the first instance of a line that contains 'srv' that precedes it. I have been looking at Tac and sed, but have been unsuccessful at getting it to work ...
7
votes
3answers
1k views

How to split a string by the third .(dot) delimiter

I did a few research, I can split the string by '.' but the only thing I want to replace is the third '.', and the version number is actually variable so how can I do the replace/split? version: 1.8....
0
votes
1answer
61 views

Extracting multiple strings, according to a pattern, in a bash script

I'm writing a shell script to generate a directory listing. as an input a receive a long html string : https://www.mycompany.com/posts/aureliaflore_china-seoul-startup-activity-6571925510337728512-...
0
votes
1answer
96 views

Cut string on every nth line of a very large document

I have document A here: @rand1 ABCDEFBHIJKLM + <<///// @rand2 NOPQRSTUVW + <<//<<< @anotherrand XYZABCDE + <<//<<< I would like to have an output that contains ...
1
vote
2answers
67 views

Convert string in #!/bin/sh

Hi I have output like this [4251][7c3c] I need to get 517C3C I've tried decodeSerial() { serial=$1 #serial=$serial | sed -r 's/(\[|\])//g' #serial=$serial | sed 's/]//' #serial=$...
1
vote
1answer
35 views

Bash replace special character '

All the questions and answers i already read are not my solution. So, all i want to do is to replace the special char ' in bash. This works: A="abc" B="${A//[b]/x}" But this does not work: A="a'b" ...
1
vote
1answer
37 views

Replacing lines in one file based of indices in a second file

I have two files: Ref.txt which contains: ABCDEFG,15147,1,2,6 HIJKLMN,500,3,4 and Seq.txt which contains: ABCXXXX ABCDEXX IIJKXXX HIHKXXX LMNOPQR BBCDEXX STUVWXY I would like to replace the ...
1
vote
1answer
377 views

Bash: How can i remove characters from a string until i hit a specific character

Basically, I'm trying to make a program that at one point needs to store coordinates. There is one part of the program where I need to separate one string into two so I an able to preform math on the ...
2
votes
3answers
55 views

Extract a part of a filename

I have the filename TRNPRD.STD.BSRE.BREX.J.20190520.V02.PRET on a Linux machine, from which I need to extract only TRNPRD.STD.BSRE. I tried the following command: echo TRNPRD.STD.BSRE.BREX.J....
0
votes
3answers
3k views

Parse a string in bash script

I reviewed other answers but couldn't get a proper explanation of how to do this. I have a string variable called id such that id='{"name":"john"}'. How can I get 2 variables out of this string like- ...
4
votes
3answers
84 views

How to find character locations of a string in a file?

I need to search for a string (a sequence of characters) in a file with a certain encoding, typically utf8, but return the character offsets (not byte offsets) of the results. So this is a search ...
0
votes
2answers
40 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
139 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
156 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 ...

1
2 3 4 5
13