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
1 answer
24 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 [ "${...
user avatar
  • 103
0 votes
2 answers
38 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' ...
user avatar
  • 1
1 vote
0 answers
39 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 ...
user avatar
0 votes
2 answers
92 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> ------ ...
user avatar
  • 297
0 votes
1 answer
40 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' ...
user avatar
  • 297
-1 votes
3 answers
73 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: /...
user avatar
  • 297
0 votes
3 answers
64 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.
user avatar
0 votes
2 answers
30 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 ...
user avatar
  • 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/...
user avatar
  • 5,074
1 vote
3 answers
48 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 ...
user avatar
  • 13
0 votes
1 answer
82 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 ...
user avatar
3 votes
1 answer
71 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 ...
user avatar
-1 votes
1 answer
44 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....
user avatar
  • 297
-1 votes
5 answers
110 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 ...
user avatar
  • 297
0 votes
1 answer
79 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: ...
user avatar
  • 297
1 vote
3 answers
110 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 "...
user avatar
  • 91
0 votes
5 answers
95 views

Replace hyphen(s) with underscore(s) within angle brackets in a file

I’m aware of how to replace string; e.g., replace hyphen with underscore in a file. However, I wish to replace all hyphens with underscore for all text that falls within angle brackets in a given file....
user avatar
  • 297
2 votes
2 answers
55 views

sorting files using string

I have a directory of 1000+ files (follow the same formatting) example file would be giroof-doas-ob10roof-L1-202007020832-10.std Where year is 2020 month is 07 day is 02 hour is 08 min is 32 ...
user avatar
1 vote
1 answer
161 views

Grep a list of strings [closed]

I have a multiple strings that I want to find them in a list of files that are in a same folder. So, I want to put all the strings in a sort of list and then apply the command: grep -l string *,so I ...
user avatar
0 votes
1 answer
55 views

Finding a string in multiple folders that are in the same directory

I have a directory A that has multiple folders, and I want to find a string in those folders. How can I do it? I have tried grep -rw string * in A, but it takes a lot of time.
user avatar
3 votes
4 answers
456 views

How to swap odd and even words in each line?

I have a bash script that swaps odd and even strings in one file, and saves it to another: #!/bin/bash infile="inputfile" outfile="outputfile" { while read -r odd && read ...
user avatar
  • 39
0 votes
1 answer
44 views

Why echo is printing string variables before additional text that have been merged with it?

I have code in bash for making latex file and in this fragment I want to make a table in a loop: while IFS= read -r line; do if [[ "$line" == *"comment"* ]]; then ...
user avatar
0 votes
1 answer
46 views

How can I use ! with \ to wrap command in bash?

I am in a situation that can not find a solution for my case. I need to run a long command in bash, say: node -p "const db = {1:1, 2:2};Object.keys(db).sort((a, b)=> a-b).map(Number).filter((n)...
user avatar
  • 103
-3 votes
1 answer
23 views

How to use sed to delete this string </p>\n</body>\n</html>"

How to delete this string using sed? </p>\n</body>\n</html>"
user avatar
0 votes
0 answers
9 views

Limit gettext translation length to avoid truncation

I work on an application which uses gettext to translate internal strings, e.g.: char * button_caption = gettext("Printer Settings"); The problem I'm regularly running into is with ...
user avatar
0 votes
1 answer
30 views

cut command: how use the delimiter to be considered/included within the field position?

I want extract the version release of some file .gz/.zip installers. Therefore if is executed (shown in this way for simplicity purposes): echo "apache-maven-3.8.4-bin.tar.gz" | cut -d'-' -f ...
user avatar
1 vote
2 answers
80 views

Remove duplicates of specific line keeping only the first appearance of each without touching other unspecified duplicates

I'm trying to edit a text file containing several duplicates. The goal is to keep only the first match of a string and remove the rest duplicate lines of the same string. In the example file * Title 1 ...
user avatar
0 votes
1 answer
10 views

select lines in file with same string pattern

Let´s suppose I have the following file.txt: asiub sj abq b aia ainp oo test = 123d sub ,. aiba 87ab test = 129szs bq test = aqua ayqvq 133s I want to print only the lines of file.txt that contain ...
user avatar
  • 131
1 vote
6 answers
123 views

Append string to the end of the penultimate line of a file

I want to append / concatenate a text in one line before the last line. So if my original file is: 1 sometext 2 sometext 3 sometext 4 sometext 5 sometext my new file is 1 sometext 2 sometext 3 ...
user avatar
7 votes
2 answers
252 views

Escape characters in sed transliterate

I want to use sed's transliterate (y///) to replace one set of characters by another. I would expect this to work just as well as using the tr program. $ echo '[]{}abc' | tr '[ab}' 'gefh' g]{hefc ...
user avatar
3 votes
2 answers
356 views

How to search for a string in a very large file with very long lines?

It turns out I was accidentally using grep wrong yesterday. I just checked my bash history and saw what I was executing: grep search-string-here -f large-file-with-long-lines.txt And that was what ...
user avatar
0 votes
4 answers
86 views

Is it possible to move a string which starts with # after another string to the preceding line?

I have a script file that looks like this: auditctl -a always,exit -F arch=b64 -S openat -F auid=1000 <TAB> #description of command.... auditctl -a .... ...
user avatar
  • 10.9k
0 votes
3 answers
224 views

How do I return an element from a bash array using a partial string match?

I have a bash array containing several elements, only one of which will be in the format nn.wr , where nn is a variable integer. eg array=( "abcd" "01.wr" "ef_gh" ) ...
user avatar
4 votes
2 answers
233 views

Repeat char n times in zsh prompt

I'm working on a custom ZSH prompt and I want to repeat a char n times in a string (such as spaces for padding). This string is printed with print -rP (the -r flag ignores echo escape conventions and ...
user avatar
  • 43
2 votes
4 answers
91 views

sed command failing in script with arguments

I am trying to perform a two step string editing: Match for the string "payload". If found, then add some "#if #endif" code as shown in output file. Then copy the line containing &...
user avatar
1 vote
3 answers
49 views

Rename file by removing repetitive substrings contained in filenames

I need to rename a bunch of files with the following pattern: sub-2795479_ses-V1_task-rest_acq-REST1_dir-AP_run-01_run-01_run-01_run-01_run-01_run-01_run-01_run-01_run-01_run-01_run-01_run-01_run-...
user avatar
1 vote
2 answers
46 views

Increment mountpoint name with awk

I am currently writing a script that makes mounting of LUKS devices easier. In the script I do have a default mountpoint name (data_1), - however, if a mountpoint like that already exists, I want to ...
user avatar
  • 83
-1 votes
4 answers
236 views

Shell-script extract value pair with sed/awk?

Given a string like this: NAME="bla" TYPE="test" SIZE="10" I want to get the values for each pair into into separate shell-script vars. So, for example, say we have ...
user avatar
  • 83
-1 votes
2 answers
40 views

Search and replacing a string on specific file extensions with the folder name

I want to find a string ("AAA") in an specific file extension ("*.txt") inside a directory tree (../MyParentFolder), and replace it with the subfolder name (MySubfolder). I know a ...
user avatar
0 votes
0 answers
45 views

How do I find bigrams in a .txt file and sort them according to frequency?

I want to find all the bigrams in a file and sort them ascendingly according to frequency. This is how far I have gone: cat filename.txt | tr [:upper:] [:lower:] | tr -d [:punct:]
user avatar
  • 1
1 vote
3 answers
95 views

Vary positions in a string to generate a list of outputs

I am dealing with short strings (they're DNA sequences) of ~30 length. For my purposes, every 5th position needs to be swapped for any of the 4 DNA bases (A, C, T, G). e.g. if I have an input of ...
user avatar
0 votes
1 answer
34 views

case-substring function and quoting

i use a simple function in a script in order to ckeck if a string contains a particular substring: #!/bin/bash # subs() { case $2 in *$1*) return 0 ;; *) ...
user avatar
-1 votes
3 answers
92 views

Finding element of file in KSH

please help me figure out what I am doing wrong. I have this file called "res.xml" <sequence type="a"> <transaction> <branchSerial>1</branchSerial> &...
user avatar
-1 votes
1 answer
24 views

xgettext incorrectly sorts strings

I'm trying to produce translation files from C sources with translation entries coming in the exact same order as they appear in the sources. I do not want them sorted alphabetically, or coming in ...
user avatar
1 vote
1 answer
561 views

Putting string with newline separated words into array

I currently have a a bash variable that holds a string similar to this one, where each different phrase is separated by a newline: var="1st word 2nd word 4th word" Note there is an empty ...
user avatar
  • 77
0 votes
5 answers
352 views

convert only the first column in a file to lower case

I have a file on RedHat with the data below: $ cat hello.txt mumdfw2as123v USER=wladmin MOUNTPOINT=/apps MUMFW2as97v.mrshmc.com USER=wladmin ...
user avatar
  • 297
0 votes
2 answers
154 views

How to a split string by spaces, which contains quoted values

Take the following bash example (I should mention that my real output comes from vboxmanage list vms) TEST='"ubuntu server" foo bar' echo $TEST "ubuntu server" foo bar How do I ...
user avatar
  • 103
0 votes
2 answers
49 views

how to use sed to replace a string of characters in quotes

I want to use sed to replace the current time in the code below time0=$(date '+%Y-%m-%d') time1=$(date '+%H:%M:%S') time=$(echo ""$time0"T"$time1"Z") replace $time on ...
user avatar
  • 11
0 votes
2 answers
79 views

How to sort filename list (txt-file) by sub-strings of the filename/path with multiple levels. Special challenge: Two types of filename conventions

I'd like to sort the following list of filenames / pathes. L1_Data/level1/192027/LC08_L1TP_192027_20201126_20210316_01_T1 DONE L1_Data/level1/192028/LC08_L1TP_192028_20201126_20210316_01_T1 DONE ...
user avatar
  • 111
1 vote
1 answer
164 views

string from array

I have an array named nmser in a bash script and I would like to make a string from it and store it in the variable flsel. flsel=echo "${nmser[*]}"
user avatar
  • 317

1
2 3 4 5
15