String manipulation: extracting a part of a string, text replacement, formatting to a given width, etc.

learn more… | top users | synonyms

2
votes
2answers
100 views

How to append string with dash?

I am trying to append create a string that will be evaluated in the middle of a grep line. It is to find possible occurrences of different usernames within a certain file. However, I do not know how ...
0
votes
2answers
155 views

split string at first occurrence of a delimiter

I do a grep in a shell (bash) to search for some ids in many log files: grep "100200300" my.log* result is something like that: my.log:Jan 17 15:04:52 100200300 ok my.log.1:Jan 17 14:35:17 ...
3
votes
3answers
403 views

Replace a string in a string with a new line (\n) in Bash

I tried this command: [silas@mars 11]$ string=xyababcdabababefab [silas@mars 11]$ echo ${string/abab/"\n"} xy\ncdabababefab [silas@mars 11]$ I also tried to replace "\n" to '\n' and to \n . I can't ...
2
votes
2answers
72 views

How do I rename files using the directory name?

My file structure now is: Input 1 pre.nii 2 pre.nii I would like it to be: Input 1_pre.nii 2_pre.nii From the Input folder, I tried: for i in */pre.nii; do echo $i_pre.nii; done ...
4
votes
1answer
93 views

What ${var/a/b/\c} means?

I'm little bit confused about this statement(construction?). I cound't find any sufficient source about this (I do not even know how to call it). What is happening here? bash-4.3$ var="a/b" bash-4.3$ ...
1
vote
3answers
36 views

how to find the start location of a changing substring in a longer string

I have these 200+ character long strings stored in a file. In each string, there exists a pattern such as ##XXX###XXXX where # is a digit between 0 and 9 and X is a character between A and Z in upper ...
0
votes
1answer
54 views

modify all of the files to read something other they what they do

the Objective is to write a proccess that literates through a tree then change the names of everything within that tree including the names of the tree itself (the leafs, the branches, and trunk of ...
1
vote
0answers
51 views

How to extract weather data from a html doc using linux string manipulation

I need to extract these info from this weather forecast site, but I don't know how much about string manipulation in linux: acm_RecentLocationsCarousel.push({name:"Irvine, CA", daypart:'day', ...
1
vote
4answers
188 views

Extract substring with character instead of position

I recently got into nautilus scripts, and for the one I'm writing I'd need to extract a substrong from a filename. My problem is that I found tons of methods to extract a substring based on the ...
4
votes
1answer
126 views

Bash regular expression pattern matching spaces, within double brackets test, using “~=” operator

Simply said, I just can't find a way to match a variable against some pattern containing spaces. Here is what I expected to work (echo 'ok' string) item='foobar baz' pat=".+bar baz" if [[ "$item" =~ ...
-1
votes
4answers
152 views

bash script to reverse line

So i came across this script to reverse an input line #! /bin/bash input="${1}" reverse="" len=${#input} for(( i=${len}-1; i>=0; i-- )) do reverse="$reverse${input:${i}:1}" done ...
1
vote
4answers
82 views

Bash part of string without using chars in quotes

So I want to get from a string like: q='"Something, variable", another part, third one' The part "Something, variable". I could get '"Something' using ${q%%,*}. But how could I let bash ignore ...
6
votes
5answers
1k views

Replace string after last dot in bash

I have the following pattern in a string (an IP address): 123.444.888.235 I want to replace the last number after the dot with 0, so it becomes: 123.444.888.0 How could I do it in bash or ...
0
votes
1answer
98 views

string fill performance in shell scripts: what is the best amount and for what shell?

I was trying to determine the best performance for string fill like in: str+="A" #one per loop I came with this script for bash: #!/bin/bash bReport=false nLimit=${1-3000}; #up to 25000 echo "...
1
vote
4answers
79 views

How to get only one character after match in string (to determine RHEL version from rpm)

I have an open source shell/bash script for setting up and configuring hosting servers, and to install cloudflare I am pulling the version of RHEL using this command: RHEL_VERSION=$(rpm -q --qf "%{...
1
vote
1answer
201 views

Remove leading 0 from specific field

I have a file with multiple fields but only want to remove the leading 0s from the 2nd column (col B) rather than all of the columns. I have tried: sed 's/^0//g' filename > new_filename but this ...
1
vote
1answer
106 views

How to extract date and calculate if it is within “x” days from today?

Currently I have file with structure as Foo Sign: blah SubFoo Sign: blah BarDate: 2017-11-31 Foo Sign: blah BarDate: Never Foo Sign: blah BarDate: 2016-12-20 Foo Sign: blah ...
0
votes
1answer
53 views

Raspbian: evaluate conditional strings and/or numbers

I am having a really a hard time to evaluate conditional strings and numbers. After hours of reading and trying with (my Beginners Guide) I keep getting the error msgs like syntax error in conditional ...
1
vote
2answers
79 views

Use “find” from a specific relative directory [closed]

Suppose I'm in the following directory: /A-dir/B-dir/mydir/… some directories …/x-dir I want find to search from mydir, but mydir is not specifically known in advance; it is defined by the current ...
1
vote
2answers
96 views

How to replace values in a string using sed but keep the string intact

How to replace values in a string using sed but keep the string intact? For example echo "01XX1234" I'm looking to replace XX with a hex value and leave the 01 and the 1234 intact. I've tried every ...
6
votes
5answers
639 views

How to abbreviate /path/to/file to /p/t/file

I'm looking for an elegant one-liner (eg, awk) that will shorten a string of a Unix path using the first character of each parent/intermediate level, but the full basename. Easier to show by examples:...
1
vote
3answers
60 views

How to extract “foo” from “foo-bar-baz” in bash? [duplicate]

I'm trying to extract foo from the string "foo-bar-baz" in bash. Any ideas how to do it using string substitution or something similar (without external tools). I tried this but it didn't work: $ ...
2
votes
3answers
102 views

List variables with prefix where the prefix is stored in another variable

I'm trying to list all the variables with a certain prefix, but that prefix is dynamically determined. For example: prefix=apple_ apple_one=a1 apple_two=a2 If I simply do: echo ${!apple_@} I ...
1
vote
1answer
42 views

date: Getting next certain hour

I want to get a date-time string in the future, for example I always want to get the next time of 9:00 am. I know the string formats like "tomorrow" or "next day" to get a certain date-time string. ...
3
votes
1answer
41 views

Removing substrings from a shell variable

I have some string that read like this one here Dio - We Rock-Greatest Hits (2CD) (2004) I use this NewDirectoryName=${NewDirectoryName%'(" ")'* } NewDirectoryName=${NewDirectoryName//[^A-Za-z ]/ ...
1
vote
4answers
2k views

Bash: remove numbers (strip numeric characters) from string variable

I want to strip all numeric characters from a String variable, eg from: VARIABLE=qwe123rty567 to: echo $VARIABLE > qwerty I've search many posts but they either use sed to output to file/file ...
1
vote
1answer
269 views

Find and delete lines containing multiple patterns in a file

I have a list of strings and want to find and delete lines containing these strings in a file. A short example of the list of strings is listed as below. File S 1Mo 32,33 2Mo 7,26 2Mo 7,27 2Mo 7,28 ...
0
votes
1answer
53 views

if statement not working properly

I have a loop comparing strings that it still does not kick it out if it matches. the set up is many different types of file extensions, only want to delete the ones I do not want keeping the ones I ...
0
votes
2answers
66 views

How to remove non english chars from a string

I have a BASH script I wrote that removes META data off mp3 (etc) then I chop them up to reform them if needed resample the mp3 and reasign the data to make a directory to corraspond to artist / album ...
2
votes
2answers
339 views

How to escape single or double quotes when passing a variable to bash script?

Let;s say I have a script which echoes $1 #!/bin/bash echo $1 It's called test.sh. Then I call /bin/test.sh 'test'. The output is test. But this doesnt work: /bin/test.sh 'te'st' There's a ...
1
vote
3answers
106 views

How to check for valid $PATH directories, and output them telling whether each one is valid or not?

Ok so I got this: check="${PATH//:/'\n'}" The above gets me each path in a simplified manner, and formats it nicely into a variable. Now how do I check each line for existence, and state that for ...
1
vote
3answers
161 views

Match lines beginning with the same pattern in two text files

I am now having two text files with some of the lines beginning with the same pattern. For example, File A 1Mo 1,1 I love you. 1Mo 1,2 I like you. Hi 23,3 I am hungry. Hi 23,4 I am foolish. File ...
1
vote
3answers
50 views

Bash How can I wrap text at a certain number of space separated inputs

I have a long string like: "1 2 6 9 18 19 25 67 89 102 140 187" and I want to fold or wrap it after a specified number of inputs or spaces using (e.g. fold on the third input) rather an the number of ...
2
votes
1answer
89 views

Bash String Expansion Corrupt

I have the following: echo "PLATFORM = $PLATFORM" DATE_STRING=`date +"20%y-%m-%d"` echo "DATE_STRING = $DATE_STRING" # VERSION_LINE will be something like: '#define VERSION_STRING "1.2p2"' ...
1
vote
1answer
31 views

How can I access a column of a line from within a string?

I have a file I'm traversing in a for loop. I want to do something like... for line in file.out; do echo "Some text before $line($2) $line($3)"; done Where, for every line I insert the 2nd and 3rd ...
0
votes
1answer
62 views

Expanding a brace expansion string held in a variable for use in a for loop?

As a complete newbie to bash, I've got the following script: FOOS=foo{1..5} for i in `echo $FOOS` do echo bar done Now I (think I) get the idea that the brace expansion only works with literals - ...
0
votes
1answer
41 views

Can you replace displayed text with something else in command window?

I am trying to manage user accounts. They are basically just numbers, but I would rather see user last names on the screen. Is it possible to automatically replace displayed text with something else? ...
1
vote
2answers
61 views

How do I stream edit progress-like output?

I'm having trouble editing some progress output. In this case it's Postgres' pg_basebackup, but it's similar to other progress output like curl and wget. Take for example the output of this: ...
2
votes
2answers
108 views

Remove specific (known) extension from filename if it's there

I'm not even sure whether grep is meant to be used for regex in one-line data. Googling led me to this lookahead expression that, along with grep, matches any .ext file and returns bare name: XX@XX:/$...
-4
votes
2answers
46 views

A script that can check subdiractories for a file that contains 'hello' and gives back directory name

i need a script where you give a directory name as a parameter and the script check all the subdirectories .php files and if any file contains a string 'Hello' then the script list the matches files ...
1
vote
2answers
40 views

extract part of the text from all matching lines

I am running a for loop against set of servers to find the cert expiry. Output of the cert command will be below for each server: line 1 line 2 line 3 Valid From: Tuesday, 29 September 2015 10:40:13 ...
2
votes
2answers
106 views

Cleaning PHP exploits from infected files using sed

Recently my server has been a target for a series php exploit attacks and using ClamAV I was able to identify many infected files. The catch here is that there are legitimate files that are infected ...
4
votes
2answers
70 views

How to use the results of “file” (Name of Creating Application: Microsoft Word) to search for a specific string?

I am trying use the results of "file" which returns info about the file/s I specify. E.g. file *.doc 'all .doc extensions This then returns information about the file including "Name of Creating ...
0
votes
0answers
24 views

How to reference a variable by its string “name”? [duplicate]

Say for example... #!/bin/bash a1=one a2=two a3=three for x in {1..3}; { echo -n "\$a$x "; } Actual.. $a1 $a2 $a3 Expected.. one two three How can one echo, reference, call, or what ...
2
votes
2answers
108 views

Identify sub-directories that do not contain a specific string in a specific file

I have a directory named dir1 which has about 800 sub-directories named disp-001, disp-002, ... disp-800. I need to find sub-directories that either do not contain a file stdout or if they do, that ...
31
votes
8answers
11k views

How to generate a random string?

I would like to generate a random string (e.g. passwords, user names, etc.). It should be possible to specify the needed length (e.g. 13 chars). What tools can I use? (For security and privacy ...
3
votes
3answers
71 views

How do I cut a string already separated by spaces?

Lets say there's a string like this test="1/2/3 4/5/6 7/8/9/0" They are separated by spaces, as well as '/'. I want to return a result like this by taking the second field of each string segment. ...
2
votes
1answer
65 views

python Vs bash string slicing

My file a contains the text, bcd\\\\. With bash, i read the file and print characters from 4th to 8th position as, tmp=$(cat a) echo "${tmp:3:4}" It prints, \\\\ All happy. Now i use python'...
18
votes
2answers
3k views

Is there something like JavaScript's “split()” in the shell?

It's very easy to use split() in JavaScript to break a string into an array. What about shell script? Say I want to do this: $ script.sh var1_var2_var3 When the user give such string ...
0
votes
1answer
248 views

RRDtool update not working when integers and floats are mixed

I'm trying to store ADSL stats for my router. I didn't do rrdtool info on the others beccause they are now showing the correct value after using the same command as in the script to update. The weird ...