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

learn more… | top users | synonyms

0
votes
0answers
13 views

Combining strings command and grep: how to limit results to null-terminated strings

I have a feeling that it is strings which is thwarting my efforts here. The binary files I want to use strings on unfortunately yield several matches per file, even though it is a 100% rule that ...
1
vote
1answer
21 views

How do you extract a whole word containing substring?

If I have a file that looks like this: example.png example.jpg example2.jpg example2.png example.swf example2.swf example3.jpg example4.jpg example3.png example3.swf and I want to extract the ...
0
votes
0answers
19 views

Most efficient way to replace a line in a text document? [migrated]

I am learning to code in Unix with C. So far I have written the code to find the index of the first byte of the line that I want to replace. The problem is that sometimes, the number of bytes ...
0
votes
0answers
41 views

Bash concating strings don't work [on hold]

I have searched everywhere and tried a lot of possible option's found here and on Google but none of them seem to work. I have a script that loads variables from a config file and that works (if i ...
3
votes
1answer
68 views

Append (alter) each array element via parameter expansion (i.e. without printf)?

Let the script below exemplify my quandary.. #!/bin/zsh STUFF=( moose-hoof ovary clydsedale ) echo ${MINE=$(printf "MY-%s " $STUFF)} echo ${MINE_EXP=${STUFF/^/MY-}} MY-moose-hoof MY-ovary ...
0
votes
1answer
27 views

Comparing a variable to upper and lower case letters

So I want to validate a response of a user in tcsh script and that's what I have so far set var1="temp" while($var1 != [yY] && $var1 != [Yy][Ee][Ss] && $var1 != [Nn] && ...
3
votes
7answers
319 views

Check if shell variable contains an absolute path

I want to check if a shell variable contains an absolute path. I don't care if the path exists or not—if it doesn't I'm going to create it—but I do want to ensure that I'm dealing with an absolute ...
0
votes
2answers
25 views

search the most latest's file name

I am writing a bash script to look for a file which's file name is most recently and file name like: ... Wed_Dec_30_16:47:41_PHT_2015.zip Wed_Dec_30_16:00:41_PHT_2015.zip ...
2
votes
2answers
37 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
32 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
231 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
69 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
67 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
32 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
52 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
33 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
3answers
71 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
31 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
71 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
3answers
61 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
511 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
68 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
59 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
45 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
73 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
48 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
71 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 ...
2
votes
2answers
50 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 ...
7
votes
5answers
607 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 ...
1
vote
3answers
48 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
49 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
27 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
36 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
432 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
0answers
102 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
38 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
48 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
79 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
90 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
107 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
46 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
75 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
26 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
34 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
39 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
48 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
66 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: ...
-4
votes
2answers
45 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
30 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
63 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 ...