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
0answers
23 views

How do we have correct separator IFS on a condition?

Is there any way or workaround resorts to correctly have separator IFS on a condition that it's a certain character only if not preceded by another certain character ? Let e.g. separator must be ; not ...
1
vote
1answer
31 views

Match contents of first file with second to generate third file

Sample data of my first file first.txt is below: 10.0.0.1,Web Server,comments1,jboss is older version,myappcode1 10.0.0.3,Web Server,comments4,httpd is latest version,myappcode15 10.0.0.7,Web Server,...
3
votes
3answers
203 views

Set IFS to two bytes value in Bash

How to set IFS to two bytes value in Bash ? IFS=',;' will delimit each part of which resides between two simple , and/or simple ; instead of ,; as a delimiter How is emulated/workaround way, so that ...
0
votes
2answers
58 views

bash: extract only the part of the string following the dot

Say I have a string such as ="/home/Rob/.SpaceVim" and I want to extract the string following the dot. How do I do this? I know $ is also used to represent the last character, and \ can be ...
4
votes
1answer
145 views

How to split a string by ':' character in bash/zsh?

I am searching for a way to split a string (variable) by : character, in a compatible way between bash and zsh. From different sources, I found the following command: str="part1=part2=part3" ...
0
votes
0answers
9 views

How to substitute a string on the same line of the regex searched string in Ansible

Here is my Test String test.txt: hello user1 ALL=(ALL) NOPASSWD: /usr/bin/* /app/tomcat/tomcat*/webapps/*, /usr/bin/rm -rf /app/tomcat/tomcat*/webapps/*,/bin/rm -rf /app/tomcat/tomcat*/webapps/**.sh ,...
1
vote
1answer
52 views

Compare strings with different encodings

I'm trying to compare a string from a file that is encoded in UTF8 file /dev/eeprom: UTF-8 Unicode text, with very long lines with a serial number that is hardcoded into the script. When printing to ...
2
votes
3answers
81 views

Padding unicode strings with bash's printf

I tried padding a Unicode string with bash's printf, and saw that, while printf "%2s" a yields the expected " a", a Unicode variant printf "%2s" ä yields a ...
0
votes
1answer
32 views

convert directory names with german 'now' date to YYYY-MM-DD

I exported pictures from mac's iPhoto. The directories are named with the date in local naming ("Day. MONTH YYYY"): 8. Mai 2013/ 13. Juli 2013/ 31. Oktober 2014/ and some of them with GPS ...
0
votes
2answers
48 views

Unable to construct command from the output of ps

I wish to get the httpd process and then get the bin\apachectl command/file -v output Output: ps -eo cmd | grep httpd | grep -v grep /home/myapache/app/httpd/bin/httpd -k start /home/myapache/app/...
-1
votes
1answer
33 views

Replace certain strings

I've got a text file containing the following: yyyy-mm-dd, 12:34/s-show/... yyyy-mm-dd, 13:24/s-show/... yyyy-mm-dd, 14:32/s-show/... yyyy-mm-dd/s-show Now I'd like to replace the substrings before /...
2
votes
1answer
56 views

zsh: Read from stdin until a string delimiter

I want to read from stdin until a string delimiter MARKER=$'\0'"BRISH_MARKER" is encountered. I tried: ❯ unset br ; print -rn -- hi${MARKER}world | { IFS= read -d "$MARKER" -r ...
0
votes
3answers
73 views

How to convert a kubernetes style time elapsed into seconds so I can do some comparisons

From kubectl get pods -o wide The output looks similar to some-pod-name-with-numbers-123 1/1 Running 0 6d4h 192.168.0.23 node-name-abcdeft-host.domain <none> ...
2
votes
5answers
144 views

Using output of expression in bash string slicing

I want to extract a substring of a filename from a path of unknown length. I can do these two parts separately, but I wonder if there is a way to combine the two without a temporary variable? ...
6
votes
3answers
309 views

Most robust way to list every basename in a directory, sorted by modification date?

Given a directory containing: note 1.txt, last modified yesterday note 2.txt, last modified the day before yesterday note 3.txt, last modified today What is the best way to fetch the array note 3 ...
0
votes
4answers
51 views

how can extract single character before the number in a string

I have a list that contains the names of hosts available in our company. For example : gswast03 gkjbossp1 frdwop04 The last characters t, p, q before the number represent the environment of the host....
0
votes
1answer
51 views

How to espace sequence of characters in sed?

I am new to regex and running into some frustrations. I am trying to work with strings that express directories and am having the issue reproducible by running: echo "a" | sed "s/a/home/...
0
votes
1answer
30 views

Print Multiple Substrings Including an IP Address

Good day! I have a giant input file that displays a lot of garbage and I would like to export multiple substrings. The problem is that sometimes additional information is injected into the file making ...
1
vote
2answers
52 views

Using a variable within a string

I'm stuck on a problem. I am a biologist who uses linux for automation so I don't have the best programming background and would appreciate your help. I have a large text file, containing several ...
4
votes
2answers
153 views

Using sed to replace between a specific pattern

I've been trying with sed, and googling for the whole morning, and I can't seem to get this to work. Is it possible with sed to lowercase text between two specific characters: i.e. SOMENAME=WOODSTOCK, ...
0
votes
2answers
45 views

finding multiple strings in a line

I'm brand new to Unix and am using CygWIN64 simulator. I have a huge number of text files (tens of thousands) that I need to search for specific strings. I have had success teaching myself to search ...
0
votes
2answers
159 views

How to properly encode string based on json file?

I have the following basic.json file: { "user": "user", "pass": "password" } I'm trying to encode it in base64 like this: "Basic dXNlcjpwYXNzd29yZA==" I think I'm close: echo Basic $(echo ...
0
votes
2answers
32 views

Replace underline in multiple XML files

In a directory with several XML files (there may be hundreds of files) I have a field called <import_url>. In this field it can happen that a URL has an underscore. I would like this underscore ...
0
votes
2answers
47 views

Replace first word keeping file alignment

I am trying to write a Shell script to edit an input file. Data is structured in the input file as follow: 1000␣␣␣␣␣␣␣␣␣␣␣␣: final time 1000 : print time 0.1 : time step The ...
0
votes
1answer
28 views

Output file blows up when trying to find strings on bash on ubuntu on windows

I'm using Bash on Ubuntu on Windows to run linux commands on my windows system. It seems to work just like Ubuntu so for all intents and purposes I'm on ubuntu here I guess. I'm trying to use strings ...
0
votes
1answer
280 views

diff command to compare 2 strings?

According to https://www.computerhope.com/unix/udiff.htm The diff command analyzes two files and prints the lines that are different. Can I use the same diff command to compare two strings? ...
0
votes
2answers
34 views

Find capitalised lowercase string and put before uppercase string

I have a bash script reading names from a webpage as variables, which use the format LASTNAME Firstname: SANCHEZ Rick SMITH Morty VAN SOMETHING Halen However, I need to display each name as ...
0
votes
0answers
14 views

`zsh` and `bash` handle command output differently [duplicate]

In zsh: % RESPONSE=$(ping -c 1 raspberrypi4b.local) % echo $RESPONSE | grep "% packet loss" 1 packets transmitted, 1 packets received, 0.0% packet loss But in bash it's different: $ RESPONSE=$(...
0
votes
1answer
115 views

How to target and shorten a long string in bash with “…”

I'm doing some web development and I am printing out some object data in the console. I want to target one property/field denoted in output as dataURI: DFASDFKAJELKJDFSADFMLAKFJLSKDJFAJSDFL which is a ...
1
vote
3answers
185 views

String manipulation with curly brackets in bash

I have a string from a latex file that I would like to work on: \title{This is the title} I would like to save "This is the title" in a variable, but cannot manage to remove the substrings with the ...
1
vote
1answer
43 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
161 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
31 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
25 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
60 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
71 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
68 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
52 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
41 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
39 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
134 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
22 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
26 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
120 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
718 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
290 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
146 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
181 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 ...

1
2 3 4 5
14