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
1 vote
1 answer
29 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 ...
  • 9,885
0 votes
1 answer
51 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: ...
2 votes
2 answers
68 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, --...
  • 25
0 votes
2 answers
31 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 ...
  • 25
0 votes
0 answers
49 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
40 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
28 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 ...
1 vote
3 answers
66 views

get specific output in linux after string

Controller loading lists... ------------------------------------------------- command: select SERVICE_NAME from <table_name> ------------------------------------------------- ...
2 votes
2 answers
79 views

How do I convert the text to remove words with one letter?

How do I convert the text of the input stream in the following way: skip single letter words. If the word contains one number and one letter, it should also be omitted. I have a Text1.txt file and ...
  • 21
1 vote
1 answer
47 views

test returns wrong value (but no errors) depending on the presence of spaces around the operator

If there are spaces around the operator, test returns the correct value of the expression. But if there are no spaces, it doesn't throw any syntax error and always return true. $ test "A" == ...
0 votes
1 answer
28 views

How can I append a specific text at the end of each line in a file?

I have a file called my_file.txt. Its content is as follows: 7rrmC 7rztC 7s63D 7scfA 7sciA 7seuA 7sh3B 7shgA 7sj9N 7sp5A 7sr2B 7stdC 7sv6A 7sybA 7t2yB 7t30B 7t3bE 7t4rA 7t71A 7t8wD 7tacA 7tbsA 7tfmA ...
1 vote
3 answers
47 views

Extract file length from output of 'ls -l' passed back from sftp session

From my local server I only have access to stfp to a remote server where I want to check the size of my backup file as it's being created. So I've been able to write a bit of bash that logs in and ...
1 vote
1 answer
76 views

How to delete a string in my files?

So I want to match all below then remove those lines of string. [MTT-5634](https://my.atlassian.net/browse/MTT-5634) [MCC-123](https://my.atlassian.net/browse/MCC-123) [MTT-7965]: https://my.atlassian....
0 votes
1 answer
1k views

Convert comma separated string into json array

I have the following string in bash "3.8,3.9,3.10" Is there a way using shell to convert it into a json array, i.e. ["3.8", "3.9", "3.10"]
  • 2,393
0 votes
2 answers
66 views

Extract text from first and last strings

I want to extract text from a string matching the first and last word string is a path : /path/to/the/file/app.apk(randomcharacters) and I want to extract text like /path/to/the/file/app.apk using ...
2 votes
1 answer
84 views

Extracting json fields in a log line using awk or some other cli tool

Here's the log format in the system that we are currently using and right now it's not easy to change the logs format through the entire system. And I need a way to extract the json object in the logs ...
0 votes
1 answer
35 views

uncomment the line upon search string while ignoring case in a file

Below is my cron file entries: #Ansible: test2 */15 * * * * ansible-playbook /web/playbooks/automation/detect401MORTEN/va_action.yml | tee -a /web/playbooks/automation/detect401MORTEN/cron.out # #...
  • 417
-2 votes
1 answer
62 views

Why is my file with 10 random bytes larger than 10 bytes?

So I have generated a random string of 10 bytes using the command openssl rand -hex 10 > result. My question is why does the resulting file have a size of 21 bytes? I haven't encrypted so I am not ...
  • 7
0 votes
2 answers
189 views

How do I camel case a snake-case string except for the first letter?

I'm using bash shell. I have used this to convert a string from snake case to camel case ... $ echo "this_is_the_string" | sed -r 's/(^|_)([a-z])/\U\2/g' ThisIsTheString However, what if I ...
  • 2,102
0 votes
0 answers
17 views

How to returns the correct character after manipulating the output of ps ax | grep [duplicate]

I would like to have some help writing a script (which should works on freebsd where I have sh as default) that should grab a precise character when I do a "ps ax | grep" command from the ...
  • 471
0 votes
1 answer
37 views

How to get one line above the enabled crontab entry

I have the below crontab entries $ crontab -l #Cron to auto restart app1 #Ansible: test1 #*/15 * * * * ansible-playbook /web/playbooks/automation/va_action.yml #Cron to auto restart app7 #Ansible: ...
  • 417
-1 votes
2 answers
64 views

How to use the string returned by $() to be used as more than one argument in another command?

My use case: I need to remove all dev packages listed in composer.json file. Suppose I have two packages: projectx/package-nice and projecty/package-good. To remove them I need to run: $ composer ...
  • 309
2 votes
3 answers
128 views

Why do I need eval in this example?

I'm exploring the notion of having a shell script input and output two dimensional structures of strings, with the limitation that the strings do not contain newlines. They can contain spaces. The way ...
  • 2,135
0 votes
1 answer
56 views

Parsing text using Python

I have this text; {bc}having or marked by great {a_link|volume} or bulk {bc}{sx|large I was trying to get something like this; having or marked by great volume or bulk I tried doing this with ...
  • 246
0 votes
1 answer
80 views

Facing difficulties sending bytes containing white spaces python irc bot

Trying to make a basic irc bot in python (3.9.2) that can response specific commands when invoked. It works although when a response contains whitespace or a space, only the first word gets displayed ...
  • 246
0 votes
2 answers
225 views

Linux search file with given name containing string recursively

From Linux shell, Let's say I'm in directory /dir and I want to find, recursively in all subfolders, all the files which contain in the name the string name_string and, inside, the string ...
0 votes
1 answer
49 views

Why does cat randomFile.txt mess up with my terminal? [duplicate]

I am simply generating a pseudo-random content file with. dd if=/dev/urandom bs=1M count=1 of=./filetone.txt To then cat it cat ./fileone.txt The issue is that upon completing with this command it ...
1 vote
1 answer
85 views

IP address string manipulation problem

I am trying to build three octets 10.AB.C9 from a 5 digit number: 12ABC: 12 = first octet AB = Second octet C = Third octet There are two scenarios with my existing code that can cause an in correct ...
  • 406
0 votes
2 answers
255 views

Awk substr index 0

I just discovered that substr() in awk accepts either 0 or 1 as the initial index in a string. I tested this in Gawk 5.1.0 and MacOS awk 20070501. awk 'BEGIN {print substr("abcd", 0, 1)}' ...
0 votes
3 answers
91 views

Better way to convert IP?

I am trying to convert 10.AB.C9.XYZ to 10.AB.C2.252. Right now I am extracting each character and piecing them together. ip_main=10.AB.C9.XYZ A_char=${ip_main:3:1} B_char=${ip_main:4:1} C_char=${...
  • 406
0 votes
2 answers
88 views

How to correctly line break and concatenate a string in a bash script

I have a Bash script with a function to log messages with time and status, and I want to keep my script 80 column only: #!/bin/bash log_message() { echo "[$(date)]-[$1] : $2" >> log....
  • 752
0 votes
1 answer
93 views

Remove certain words and set word at end of string in files

I have a bunch of files that contain strings similar to: Get<1>(abc) Get<2>(xyz) How can I recursively search for all files with the above pattern and remove everything before the first '(...
  • 101
0 votes
1 answer
24 views

Loop that replaces a string only once each time after reading a different string

I'm trying to edit a mod that changes regularly (I'm trying to turn all characters into adults), and in some of the files I need to run a script that finds a specific line like "character = yes&...
  • 1
0 votes
1 answer
346 views

Run cargo command in subdirectories with user-specified arguments

I've a directory structure like: rust/ ├── dir1/ │ └── Cargo.toml └── dir2/ └── Cargo.toml I want to create a zsh script that'll run from the rust directory, and for each subdirectory with a ...
0 votes
1 answer
33 views

String compare negating in bash doesn't behave as it should be?

I have this little bash script: readonly imagick_extension=$(cat "${php_configuration_file}" | grep imagick) echo "Imagick extension grep value: $imagick_extension" if [ "${...
  • 105
0 votes
2 answers
836 views

Rookie Question: Regex in ZSH Script

I think my problem is very simple. I want to construct a new filename, which has a fixed prefix and a number (two digits), which is part of the old filename. Example: I have a file 'blableblub23.pdf' ...
  • 31
1 vote
0 answers
77 views

How to change 2 words in a string in BASH where there will be a ":" in one of them

I have worked my best on trying to make a script to iterate thorugh 2 Arrays. The values in each Array needs to get its value input into a string called ExecuteSyncoid based on a string called ...
1 vote
2 answers
2k views

split string using a substring as delimiter and get the later part

Here is my string LIBRARY_TRGT_CANV,CANV_MATCH<anything> I wish to get all the text after last occurance of our delimiter which is _TRGT_: Desired output: CANV,CANV_MATCH<anything> ------ ...
  • 417
0 votes
1 answer
81 views

Need help Formatting a file having key: value [duplicate]

I have a file having the below values: cat data.txt server1: 'calv' server2: 'anot' log: '/u/log/1' server3: 'calv' server4: 'anot' server5: 'secd' server6: 'calv' LIB_TRGT_calv,anot: '/tmp/hello.txt' ...
  • 417
-1 votes
3 answers
220 views

Need to format text file and update based of key value format

I have a file having the below values: cat data.txt server1: calv server2: anot log: /u/log/1 server3: calv server4: anot server5: secd server6: calv LIB_TRGT_calv,anot: /tmp/hello.txt LIB_TRGT_secd: /...
  • 417
0 votes
3 answers
2k views

bash insert backslash for every slash in string

I have a string that is a path: /tmp/something I need to escape the forward slashes with backslashes: \/tmp\/something How can I do this? Maybe sed? Please point me in the right direction.
0 votes
2 answers
98 views

grep results with spaces and special chars

I am trying to grab results from lshw and add them to a a bash array so I can create a new string. I am using lshw -class disk |egrep -A 7 .'-d' |grep 'product' |cut -b 17- the output looks like this ...
  • 1
0 votes
0 answers
19 views

String concatenation failing after personal access token [duplicate]

What specific syntax needs to be changed in the commands below in order for the end result of the commands to produce the expected value of https://ghp_fyrwTOKEN_REDACTEDgugg@github.com/...
  • 5,331
1 vote
3 answers
308 views

Encapsulate only comma-separated strings in quotes

I have some data that is currently in TSV format, but needs to be converted into CSV format. The only problem is that occasionally in the TSV, there are some values that include commas that are not ...
  • 13
0 votes
1 answer
1k views

How to split a key-value string into array pairs?

How can I split the following String into key-value pairs using bash? SOURCE="'key1=val 1' 'key2=val 2'" My output goal is: key1=val 1 key2=val 2 I can only input a single String as a ...
3 votes
1 answer
145 views

Recursively renaming apostrophs ' in filenames (Bash)

Need to remove apostrophes from files. I have tried several approaches, also from Stackexchange. I am on a Synology NAS, so I don't have Python, or Perl and furthermore I have to exclude certain ...
-1 votes
1 answer
47 views

Search & replace for a string separated by whitespace upon substring match found

Below is a sample file content: cat sample.txt -server -XX:+UseParallelGC -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+AggressiveHeap -XX:+PrintHeapAtGC -Djava.util.logging.FileHandler....
  • 417
-1 votes
5 answers
511 views

How to make sure that the last character of each line of a file ends with single quote character

I have a file which has several lines. I wish to update the file while making sure that the last visible character of each line of the file is a single quote '. In case not, then we should add the ...
  • 417
0 votes
1 answer
570 views

How to add single quote at the end of ONLY the last line of a file [duplicate]

I want a non-perl solution where I should be able to append a single quote ' at the last visible (non-escape characters) line of a file and save it back to the file. cat example.txt var1: 'funn' var2: ...
  • 417
1 vote
3 answers
2k views

if-statement with ((...)) string comparison not working in a bash script

I have written below script: #!/usr/bin/bash STR_1="nfosys" STR_2="Infosys" if (( $STR_1 == $STR_2 )) then echo "Strings are equal" else echo "...
  • 91

1
2 3 4 5
16