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
-4
votes
2answers
123 views

random output from input

Related question : how to create a random output from users input I was thinking to take an input(only one argument) from user just like Hello. And, I want to give output like olleho llohe he llo lo ...
1
vote
1answer
55 views

How to get string between starting with X to ending with Y using Shell

I have dynamic string which are mentioned below, where I want to get ABCXXX.XXX.XXX string till , character. Starting character is fix (ABC)CAPITAL ONLY and ending also fix com or Xcom. String can be ...
0
votes
1answer
24 views

How remove from first till a certain string in array

How do we remove from first till a certain string in array just like that of in plain string by ${..#..} e.g. ${s#abc} $ s=(e u i o foo a) $ t=(${s[@]#*foo}) $ echo "${t[@]}" e u i o a ...
2
votes
2answers
173 views

Vendor from MAC address (first three parts)

How you can get the first three parts from MAC address? $ mac=11:22:33:44:55:66 $ vendor=${${mac//:/}:0:6} bash: ${${mac//:/}:0:6}: bad substitution ${mac//:/} removes : and :0:6 should get the first ...
6
votes
1answer
386 views

How to check if variable contains valid UTF-8 string without any control characters?

How can I check in bash whether a variable contains a valid UTF-8 string without any special control characters (such as newline or backspace or carriage return etc.)?
0
votes
1answer
22 views

Why xargs -0 adds an extra blank line?

In the example, why xargs -0 adds an extra blank line, and how to avoid it? $ touch a b c $ find . -print0 ../a./c./b $ find . -print0 | sort --zero-terminated | xargs -0 . ./a ./b ./c $ find . -...
1
vote
2answers
41 views

Grep opposite option of “-o” (Not -v)

I'd like to print only the part of string that doesn't match. Is there an opposite option of grep -o? Not grep -v because it ignores the string containing pattern completely. Lets say I have: setenv ...
5
votes
2answers
546 views

Bash: value too great for base when using a date as array key

I have read about specifying "10#", but I don't think it's my case as I am not doing number comparison. I am trying to create an associative array in Bash, and the code worked fine until ...
3
votes
3answers
102 views

When piping from another command, limit the original pattern space to the matched string

I have a text file where I want to replace all spaces inside [[ and ]] with hyphens (brackets are never nested and always matched). The following is an example: $ cat test.txt abc [[foo]] xyz abc [[...
0
votes
0answers
27 views

String comparison in Bash scripting [duplicate]

Are the following conditional expressions / commands do same in bash scripting for string comparison? if [[ "str1" == "str2" ]] if [[ "str1" = "str2" ]] if [ &...
2
votes
1answer
208 views

Bash removes first character in for loop

I have the following code snippet: #!/bin/bash IFS='\r\n' vconsole="pts" for entry in $(last -n 10) do if [ -z $entry ] then continue fi if [ &...
1
vote
2answers
298 views

Split string on newline and write it into array using read

Given string foo, I want to store each of its field seperated by \n into elements of array bar. And I want to do this using read command, or any other command that is compatible with older versions of ...
-2
votes
2answers
53 views

Bash: How to extract portion from the end of a line that's in a variable?

In the context of a bash script, how do I extract the uniqueID from the uniqueID_db.dat part of URLs such as these, where the uniqueID can be anything, for example: https://cdn.somedomain.com/fetch/...
0
votes
3answers
94 views

How do i use variable's value as a dynamic variable name?

i've been looking around for this all over, but either i don't know how to ask the question correctly, or it's not a common enough issue... In PHP this is (almost) trivial: function getSite(string $...
-1
votes
2answers
63 views

How to replace duplicate occurrences of a variable in a string? [closed]

I have a block of code that looks like this: DIV='_' STRING=’1__2__3__4__5' echo $STRING The result is: 1__2__3__4__5 I would like the result to look like this: 1_2_3_4_5 Since this is a snippet ...
0
votes
0answers
24 views

More efficient method to grab one word from string based on an index?

I'm trying to write a Bash completion script which doesn't break in the presence of colons. To do this, I'm using COMP_LINE to determine the string for the command, and COMP_POINT as the index of ...
0
votes
0answers
26 views

Let shell/terminal paste by replacing newline with space

How do I paste into a shell or terminal and simultaneously replace every newline with a space?
0
votes
1answer
29 views

find cannot do on having space within string variable for -path or -iregex or etc

Cannot have space within string variable for -path or -iregex find's option. This input '/home/demo.*/\.config/File System/.*t$' is fed on: read i m="-iregex $i" find ~ $m won't work as ...
1
vote
1answer
35 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
226 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
86 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
2k 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
13 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
64 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
205 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
41 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
67 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
210 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
1answer
39 views

Use string substitution to combine array? [duplicate]

I'm trying to use string substitution to combine an array. Output: a,b,c The below works, but isn't there a way to do with without piping? #!/bin/bash words=(a b c) echo "${words[@]}" | ...
0
votes
3answers
172 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
341 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
349 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
88 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
124 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
33 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
55 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
183 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
1k 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
39 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
57 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
31 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 ...
1
vote
1answer
2k 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
38 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
537 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
829 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
61 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
623 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 ...

1
2 3 4 5
14