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

learn more… | top users | synonyms

0
votes
1answer
51 views

grep with piping and showing multiple lines

I am trying to use grep to search a large text file, and return the paragraph containing a few key words. I also want to return the surrounding lines in the result. So, for example I have the ...
1
vote
4answers
44 views

Using cut to return the middle of a string

I have an xml string echoed to stdout along the lines of <xml:attribute>{41c33a-4893b-3627a-617a}</xml:attribute> I want to return the string {41c33a-4893b-3627a-617a} in this case. I ...
3
votes
4answers
133 views

Check if a shell variable has more than one line using built-ins?

I can do this by calling the external utility sed (for a known non-empty $myvar) like so: if [ "$(printf %s "$myvar" | sed -n '$=')" -eq 1 ]; then echo "Your variable has only one line, proceeding" ...
3
votes
5answers
106 views

Bash while loop read from colon-delimited list of paths using IFS

I am trying to write a bash function that behaves similarly to the where builtin in tcsh. In tcsh, where lists all the builtins, aliases, and the absolute paths to executables on the PATH with a given ...
0
votes
1answer
23 views

assign string components into variables [duplicate]

Looking for a smarter way to do something I'm doing already. I have a string: "firstbit.secondbit.thirdbit.fourthbit.fifthbit.sixthbit" The content between the periods changes, but the delimited ...
2
votes
0answers
45 views

put string to commandline

I am making a bashfunction that show a menu binded to pgdown to browse through a a window which list my history (like 4dos/4nt etcetera). I have made so one line is always highlighted. $pos is ...
0
votes
1answer
46 views

ZSH, concatenate passed in arguments into a single string

I would like to create a simple bash function to use for my convenience. Following the answer given at: Joining bash arguments into single string with spaces I've been able to mash up this small piece ...
4
votes
3answers
119 views

Remove substring from front and back of variable

I know that I can remove substrings from the front of a variable: X=foobarbaz echo ${X#foo} # barbaz ...and from the back of a variable: echo ${X%baz} # foobar How do I combine the ...
1
vote
4answers
84 views

How to break a long string into multiple lines in the prompt of read -p within the source code?

I am writing an installation script that will be run as /bin/sh. There is a line prompting for a file: read -p "goat can try change directory if cd fails to do so. Would you like to add this ...
1
vote
1answer
36 views

Bash: How to get a value from an array to create command from it

Is it possible to use bash to fetch a string from an array provided in the following syntax within a config file? | | Version | PackageManager | Webserver | Database | ...
1
vote
2answers
26 views

How prepend appropriate amount of 0's to numbers contained in image names?

I've got a folder /User/me/Desktop/folder/images with files like key 1--name.jpg, key 10--name1.jpg, etc. The files are sorted incorrectly when fed to my site so I get an arrangement like key ...
0
votes
0answers
26 views

Problem matching strings in bash script

I`m making a script to rename files in a folder. The new name for the files is in a config file. I have to be sure that the file I want to rename corresponds to the current one in the config file. My ...
1
vote
3answers
49 views

Get directory name from file name

I want to make a directory name by extracting parts of a file name. Here are some examples: server.log.2016-03-20-14 → 20160320 server-2016-03-17-13-16-Restart.log → 20160317 I'm using HP-UX.
1
vote
3answers
40 views

How to get a substring based on index of another string

I need to extract the integer value from some log files. The integer value always appears after a substring return code. But it may or may not have additional stuff after this substring. Below are two ...
3
votes
1answer
50 views

Rewrite arguments before passing them to a command

I use rtorrent. When using magnetic-links, it creates a "meta"-file (.meta). This gets the form of a long hexadecimal-number (0-9, A-F). For example: 0123456789ABCDEF0123456.meta To "use" an ...
3
votes
3answers
38 views

Strings - Extract File Version Recursive

I have been using strings to extract and compare file versions between directories. I don't seem to be able to do this recursively. The method I have been using per file is: strings -e l ...
2
votes
5answers
71 views

how to cut fields of a directory name?

I have directory named 140905_M01771_0112_000000000-ABBYT. I want to get the third field i.e. 0112. I tried: cut -d _ -f 3 131230_M01771_0032_000000000-A7874 giving me error: cut: ...
3
votes
5answers
69 views

How to extract data from a file and construct a filename from one of the values

I'm trying to pull two values from a configuration file and write them to a new file. The hard part is that I want the name of the new file to be determined by one of the values in the input file. ...
3
votes
1answer
37 views

How to split a string on a control character?

I know how to split a string on a printable separator character; e.g. if the separator is ,: FOO='x,y,z' printf "%s\n" ${(s:,:)FOO} x y z But what if the separator is a control character, such as ...
-3
votes
2answers
26 views

How can I reformat MAC address and IP pairs, ensuring each byte of the MAC has a leading zero?

I've got a table like this: 10.6.6.12 0:50:56:9b:a6:2 10.6.6.13 fe:9:31:b6:71:1d I must have something like this: 10.6.6.12,00:50:56:9b:a6:02 10.6.6.13,fe:09:31:b6:71:1d A 0 is always added ...
1
vote
3answers
31 views

How can I parse JSON to test the string value of a nested key?

For example, here is my program's output (bspwm, if you wanted to know): { "id": 29360131, "splitType": "vertical", "splitRatio": 0.5, "birthRotation": 90, "vacant": true, "sticky": ...
3
votes
4answers
56 views

Bash converting path names for sed so they escape

I'm having a problem with a script. It is meant to change a value in a file called %DIR% so that it becomes a path name. The problem is the slashes in the directory name upset sed, so I get weird ...
0
votes
2answers
71 views

First line missing in concatenation of SSH output [closed]

I am trying to concatenate two strings in bash script but I am not able to get the expected result, USERNAME="ubuntu" MASTER="some IP" KEYFILE="/Path/to/keyfile.pem" STR1=`sudo ssh -q -t -o ...
0
votes
2answers
37 views

Retrieve string between two strings

I'm trying to extract a substring in the middle of two strings. I execute this grep command: echo "http://www.miweb.es/midoc-87-documento-texto_mf_4150310_1.txt" | grep -Po "(?<=midoc-).*(?=-)" ...
1
vote
1answer
40 views

Remove a part of a path from a variable content

In a Bash I have a variable: LOCAL_PATH="/this/is/a/path/" Then I have another variable, which content starts with LOCAL_PATH and it should be removed: LINE="/this/is/a/path/which/is/longer" The ...
1
vote
1answer
57 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 ...
3
votes
1answer
102 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
35 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
408 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
26 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
75 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
75 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
315 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
87 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
34 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
40 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
134 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
79 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
107 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
78 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
832 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
86 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
70 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
107 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
90 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
51 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
78 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
81 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 ...