Questions tagged [string]
String manipulation: extracting a part of a string, text replacement, formatting to a given width, etc.
809 questions
-4
votes
5
answers
121
views
Linux: how to remove starting from chars?
I have those lines
mycommand --option1
mycommand --option2
mycommand --option3
mycommand --option4
mycommand -h
mycommand -i
mycommand -z
mycommand -1
I want to remove all until - or -- appear
so I ...
2
votes
1
answer
83
views
Want to search for a every paragraph that has a certain string and return some string from that paragraph
/* ----------------- WANT TO RETURN1 ----------------- */
machine: some value
server: WANT TO RETURN1
owner: some value
notification: some value
informatica: some value
sap:...
1
vote
6
answers
734
views
Change a double quote into a single quote with gnu sed when the quotes come before and after a parenthesis?
I'm using Debian 12 bookworm, gnu sed. I have this
"Hello1"
"('41-Z', 5001, 'A6')"
"'Hello2'"
"('42-Z', 5002, 'A7')"
'Hello3'
"('43-Z', 5003, 'A8')"
...
6
votes
2
answers
519
views
Can I use grep or strings to find the previous atime of a file still present on my btrfs?
The metadata of this file which resides on my HDD is written by CoW, therefore can I look for it just by using grep or strings, and the filename?
0
votes
3
answers
126
views
Making each word in a text file an item in a bash array
I have a string of text and spaces which looks like this:
macOS windows arch-linux ubuntu_linux
I want to append each item (with whitespace denoting a break between items) to a bash array. How do I ...
2
votes
1
answer
489
views
How can I take a sub-array in bash of the first N elements of a string array with elements containing spaces?
This question is similar to this one but it differs from it:
Consider this array with string elements which may contain spaces:
a@W:$ arr=("eins" "zwei" "eins plus zwei" &...
-1
votes
2
answers
94
views
Split string with 0-2 / (or determine there's none) (Bash)
Update: Up to 2 "/" in the string.
String structure is either:
Character set name/LF
Character set name/CRLF
Character set name/CRLF/(unknown purpose, likely a number)
Character set name
...
0
votes
2
answers
256
views
How to check value inside a file using bash?
I'm trying to check if the value inside a file is "0".
COUNT_EXECUTION=$(< /tmp/count)
if [ "$COUNT_EXECUTION" == "0" ]; then
echo "Try restart service."
...
0
votes
3
answers
336
views
How to edit a string matching a pattern in a specific field on the condition another string is not present on the same line
I need to edit the string "NA" to "Na" only if it is in the 6th field of a file. I can currently achieve this with:
awk '{gsub("NA","Na",$6)}1' $filename
...
0
votes
3
answers
436
views
Convert json data to comma separated string
I have a json object that has an unknown number of variables, like this:
{
"var1": 123,
"var2": 456,
"var3": 789
}
How can I automatically convert it to form ...
0
votes
1
answer
119
views
Problem matching new terminal window ID with xdotool in Bash
I have a script that kicks off a new terminal window that I want to move to a specific position on the left terminal using xdotool.
After kicking off the terminal I run
xdotool search --class gnome-...
2
votes
1
answer
356
views
Joining 'fish shell' arguments into a single string with spaces
Sorry, this question is already answered for 'bash' here:
Joining bash arguments into single string with spaces.
in Fish, using "'$*'" leads to this error:
$* is not supported. In fish, ...
0
votes
1
answer
68
views
What does the line ($eachJOBID = $eachScriptNoPath) =~ s/\.csh// ; do?
This line I have in my code cuts the .csh from a string and returns the rest of it. Can someone explain what each part of it does?
($eachJOBID = $eachScriptNoPath) =~ s/\.csh// ;
2
votes
5
answers
611
views
How to remove duplicate slashes from path to a file?
I have a path to a file that has duplicate slashes which would like to simplify.
For example, if I have the following:
/opt//bin//executable
I would like to get:
/opt/bin/executable
-1
votes
1
answer
66
views
I can't grep some inputrc string
bind -p |grep -E "\\e.\":" work
but
bind -p |grep -E "\\e\\C-.\":" don't work
I tried a lot of combination
1
vote
1
answer
108
views
GNU parallel: how to call exported bash function with empty string argument?
Scenario:
$ process(){ echo "[$1] [$2] [$3]" ; } ; export -f process
$ process "x" "" "a.txt"
[x] [] [a.txt]
Here we see that the 2nd argument is empty string ...
-1
votes
5
answers
229
views
add empty line before every line that contains certain characters
I have a lot of markdown files that contains something like this:
* header A
- item 1
- item 2
** sub-header A1
** sub-header A2
* header B
- item 1
- item 2
** sub-header B1
** sub-...
0
votes
0
answers
63
views
Why isn't passed quoted $@ a single argument? [duplicate]
Why isn't passed quoted $@ a single argument?
f2()
{
echo "f2: $1"
}
f1()
{
local x=("$@")
f2 "${x[@]}"
}
f1 x y
Invocation:
$ bash t537.sh
f2: ...
-1
votes
1
answer
166
views
Checking if I'm correct about the order of operations in Bash
Guten Tag! I'm a rookie in everything Bash-related. I'm familiarizing with the syntax and wanted to know if the order of operations in this command is as I thought or not.
The command:
echo 2 * 3 > ...
2
votes
1
answer
191
views
How to check what character a variable contains and compare it with a number? [closed]
I have an hour record in which subsequent hours are written from 1 to 9 as numbers and then as subsequent alphabetic characters. I would like to save the time in a normal format, so I need to convert ...
0
votes
1
answer
70
views
Retrieve the Following Occurrence of the String 'PWD' Once the Given String is Located
Below is my sample test.log file
export SQRDIR=/v/orahome/Middleware/Oracle/bin64
export OID=ap0092
export PWD=pass1
export FDPWD=pass1
export AP0085_PWD=pass1
export SVR=AFFPROD
export ...
1
vote
3
answers
404
views
awk print between lines when "/" is part of the name
I need to print lines between those that contain a "/" in the name.
I tried with:
awk '/+SOLUTION/ESTIMATES/,/-SOLUTION/ESTIMATES/' $F > fil$F
and
awk '/+SOLUTION"/"ESTIMATES/,/...
-1
votes
2
answers
7k
views
How does the `tr` command work?
I was playing around with tr and got some unexpected results. What is happening in these situations? I don't understand what is happening under the hood, or perhaps I'm not using this command ...
1
vote
1
answer
328
views
How to convert all newlines to "\n" in POSIX sh strings
I have a string that contains newline characters. I want to escape all newlines in that string by replacing all newline characters with a string of two characters: "\n". How can I do this in ...
0
votes
1
answer
1k
views
How to remove characters from end of a line till a particular character in perl
my string is like
$varin="wer.try.iuy.oiu.qert"
i wanted to remove characters starting from end of the string till "." is encountered.
expected output:
wer.try.iuy.oiu
have tried ...
0
votes
0
answers
167
views
Bash reverse search inserting characters into command after hitting tab
SYSTEM:
Ubuntu: 20.04
Kernel: 5.4.0-162-generic
bash: 5.0.17(1)
I found a strange problem that when I use bash's reverse search with CTRL+r, I will find a command I want to run. I select TAB to ...
0
votes
1
answer
35
views
Finding on each folder if a subfolder respect the convention name according to the folder name?
i have a "sites" folder with a number of site folder named:
bu.my-url.com
dud.myurl.com
[must-be-indentical_string].myurl.com
etc
On each site folder, I'd like to check if the /themes/amu_[...
0
votes
1
answer
675
views
bash: string variable contains asterisk. how to use this variable for searching etc with grep, sed?
Have a nice day
I have got text file (zz.txt):
Chemical name
3-Aminopropane-1-sulphonic acid
Synonym(s)
--
Homotaurine * Tramiprosate
--
Chemical name
Common name and synonyms
...
I have variable
s=&...
1
vote
2
answers
183
views
String Length Always Returns 2 in ZSH Function
I'm trying to bowdlerize email addresses in a fixed length text file by generating a random string the same length as the input. I'm passing the string as a backreference in sed.
To simplify, this ...
0
votes
2
answers
613
views
Use SED to replace part of a current variable with user input variable
I'm trying to replace only part of the existing variable with a new user input variable as below:
#Current Variable:
gdbName=Test.MY.DOMAIN.COM <--I need to replace the "Test" (This can ...
2
votes
2
answers
428
views
extract 2 strings from a log file
I have a log file that gets continuously populated; let's state its name is logfile.txt.
In this log file I want to continuously capture sub strings from a single line that contains other strings on ...
2
votes
2
answers
2k
views
Tool to flatten yaml
Is there a tool to flatten yaml structure like this:
foo:
bar:
baz: true
into this:
foo.bar.baz = true
not sure what is this syntax name
context: I need this for hcl, setting a lot of values ...
-4
votes
2
answers
512
views
Is it possible for any shell to interpret a string with decimal point as a number (int, float)?
This idea came to me with recent updates to Visual Studio Code, in which I usually shellcheck my code automatically as well. This is what I see, so you can get the idea:
This question is very simple ...
0
votes
1
answer
257
views
Error in chaining multiple conditions inside a single if statement
I am working on a shell script and a part of the script should check if a file doesn't exist or if the filename provided is null (i.e. no filename has been provided).
Here is my code -
if ! [[ -e &...
0
votes
0
answers
32
views
How can place a newline before EVERY individual character in a file? [duplicate]
Ok, I may just be having a dumbass moment. If so, I apologize.
It sounds like a fairly simple task, but I can't get this working:
I'd just like to be able to input a string, and place each individual ...
0
votes
4
answers
284
views
How to remove the superfluous "./" from a path?
Consider the following script compare_times.sh (thx to http://superuser.com/a/1780500):
#!/bin/bash
# Syntax: compare_times.sh directory_1 directory_2
# Semantics: for pairs of files with the same ...
0
votes
0
answers
58
views
Comparing a text file's unique content to expected string not registered as equal
I wrote a shell script to check which ".err" text files are empty. Some files have a specific repeated phrase, like this example file fake_error.err (blank lines intentional):
WARNING: ...
0
votes
4
answers
930
views
how to print first word from a string with multiple words without space?
I have a shell script to print appimage filenames inside a folder like this
#! /bin/bash
Dir="$HOME/Applications/"
Dir2="$HOME/Downloads/"
cd -P "$Dir"
for f in *....
1
vote
1
answer
50
views
Concatenating string passed into a control sequence
I have this echo invocation that prints a blue bar in a Bash terminal:
echo $'\e[48;2;0;0;255m \e[0m'
I would like to pass some variables to it, something like (this doesn't work):
...
3
votes
1
answer
2k
views
How to print leading zeros (padding) in awk?
I am trying to print 99.11111 as 099.11 in AWK.
I have tried the following variations without success.
$ awk '{printf ("%000.2f\n", $1);}' <<< 99.111111
99.11
$ awk '{printf ("%...
3
votes
1
answer
3k
views
Why does escaping double quotes from Python's run (with shell=True) is different than in Bash?
I need to produce JSON configuration files
with echo and tee called from my Python script.
By trial-and-error I've found out that I have to use single quotes.
Yet I don't understand all the behaviour ...
2
votes
9
answers
2k
views
Why grep does not work properly in this case?
Consider a file file2.txt having the following content:
P 89 24 -1.5388040474568784e+01 7.4421775186012660e+00 -1.3143195543234219e+03 1.3168884860257754e+03 8.0419002445999993e+01 44 0 0 -97 0
P 122 -...
1
vote
1
answer
60
views
How to extract the following strings from the file?
Consider the following data (say located in file.txt):
P 5 24 0 0 -9.0786328019999996e+02 9.1141809916739828e+02 8.0419002445999993e+01 22 0 0 -6 0
P 8 24 -3.9196518724924090e+00 2.0727804903086735e+...
3
votes
1
answer
1k
views
Why doesn't ampersand (&) work in string replacement without being escaped?
The following script is really simple, and replaces & with &:
string="Foo & Bar"
echo "${string//&/&}";
But the script does nothing. There are no ...
1
vote
1
answer
1k
views
Quoting a string in output from awk
I am fetching a string from a file using awk as shown below. Now I would like to double quote it. Any support would be highly appreciated.
awk -F',' '{print $(NF)}' sample.csv| tail +2
output:
...
3
votes
2
answers
437
views
Bash function colouring from string input matching regex
I have a multi-line string that I use for printing in a bash script.
docstring="
Headings
-H, -H CNT, -H=CNT, -HCNT, --heading CNT, --heading=CNT
Warnings
-W, -W CNT, -W=CNT, -WCNT, --...
1
vote
2
answers
138
views
Multi-line documentation for bash inspired by lisp
I want to print some usage information for some functions. I customarily use echo or printf for each line.
echo "-V, --version"
echo " Display the version number and copyrights ...
0
votes
0
answers
314
views
How does one store the evaluation of a big string with multiple env variables $VAR into another env variable?
Becuase I need to make sure I run authentication for my nohup commands I need the real command I want to run to be in a string in here:
nohup sh -c 'echo $SU_PASSWORD | /afs/cs/software/bin/reauth; ...
0
votes
1
answer
273
views
how to detect unbalanced special characters in string
I'm wondering what would be the best way (likely using grep or ack) to return lines containing unbalanced special character sets in a string? For example, if the string were:
bqM#+t1U"OyBGhk]ozVG[...
0
votes
0
answers
110
views
Creating a script which compares the return value from an AT command
I am trying to write a shell script that can save the output of a piped process to a variable. This variable is then compared to a known string in order to discern whether or not my AT modem is ...