String manipulation: extracting a part of a string, text replacement, formatting to a given width, etc.
1
vote
2answers
26 views
using gsub in awk to replace a word with parentheses
Hi how can I use gsub to replace a word which has parentheses.
Here I want to replace ABC(T) with ABC/G
awk ' {gsub("ABC\(T\)","ABC/G")}; Print $0' "$FILENAME" > tmp.tmp && mv tmp.tmp "$...
0
votes
1answer
48 views
How to compare two variables containing ipv6 addresses?
I have to compare two variables with ipv6 so there are strings and integers together.
I tried it as below:
if [[ "$ipvsrc" -eq "$ipvdst" ]];
then echo -e "Value OK"; fi
error output was:
...
0
votes
0answers
3 views
Reading String type dataset in h5 file using h5 C compiler failed - returns garbage [migrated]
Using H5C APIs, while reading string/float datasets in h5file, able to read float dataset properly, but string is failed with some garbage. Below part of code I have used. Am I missing anything?
===...
1
vote
3answers
71 views
Extract filename from another filename
I receive notification files for different files. I want to extract the filename from these notification files which I would use in another process. The first 4 strings of the notification file will ...
1
vote
2answers
72 views
Perl stripping substring with $ in front
I get that perl thinks the part of the string with $ are perl variables. What I'm trying to do with the perl call, is redefine variables preceded by a $ as \$, but that's throwing other errors. I ...
1
vote
2answers
45 views
How to move string from one line to another
I have been editing a large text file containing pairs of lines as follows (repeated):
\> destination_file.txt
js -e "var e='.jpg',t='b',i='14712583',h='0.us.is.example.com',s='/',n='WIV',u='...
0
votes
1answer
47 views
copy lines of text between two files using string matches
Looking for some help, I have two files, one is a large list of various names the other is coordinates. both files start each line
with an 8 digit code. I want to lookup 8 digit line code from File1 ...
-1
votes
1answer
39 views
Extract string from first character till string pattern
I have a implicit file with **$&*#@** as field delimiter and **$#@&*** as record delimiter. Below is an example,
100$&*#@200$&*#@50$&*#@Some String$&*#@2016-01-01 101010$#@&...
2
votes
1answer
21 views
Strings from two distinct arrays not matching?
I'm trying to filter one array from another array. That is, I'm trying to create a third array with a logical not-intersection.
The best I can tell, it appears this block of code never matches, and ...
1
vote
1answer
46 views
completely ignore lines that start with a specific pattern
I have a script which analyzes the output of a computation software.
sometimes the output comes with some extra data that are irrelevant to my script.
This data can be huge and makes running my simple ...
1
vote
3answers
39 views
How to delete whitespace surrounding equal signs in a string?
I'm trying to determine a way to delete leading and trailing whitespace surrounding the equal sign from a string. The whitespace may (or may not) be present. There may be 0, 1 or more equal signs.
I'...
2
votes
1answer
34 views
How can I use sed to snip a line off of a string?
I'm writing a script to display the domain registrar info and I'm trying to snip off a few lines from what I already have I currently have it grabbing all the indented text from a whois. Now I'm ...
11
votes
3answers
756 views
How do you remove dot character from string without calling sed or awk again?
I have a file called hostlist.txt that contains text like this:
host1.mydomain.com
host2.mydomain.com
anotherhost
www.mydomain.com
login.mydomain.com
somehost
host3.mydomain.com
I have the ...
2
votes
4answers
73 views
String Manipulation Shell Script [duplicate]
I am working on a UPS Monitoring project using NUT Server. My objective is to make a shell script which sends one command and in response receives the status and other parameters from the UPS.
for ...
1
vote
2answers
52 views
How can I add a filter to my grep script to NOT include a string?
I'm working on a script that will separate the Registrar information from a domains whois. So far it's working enough but there are a few things that I want to remove in order for it to be a bit ...
1
vote
3answers
47 views
How can I use string manipulation to detect and remove two different parts of a string?
I'm making a script that will remove the http:// or https:// from a URL. So far I've got it to remove the http://, but I can't seem to figure out how to modify it so that it will remove either that ...
0
votes
1answer
47 views
How to use the 'date' command to display week number of the year?
I want the UNIX date to output:
So this is week 35 of 2016.
Here, of course, 35 and 2016 are outputs of the date command. I have tried the following:
date +%U
This printed out the current week ...
0
votes
1answer
55 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
54 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
169 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
144 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
26 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
67 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
123 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
3answers
222 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 feature?...
1
vote
1answer
41 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 84--name....
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
50 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
46 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
67 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
46 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
103 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
79 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
40 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
30 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
34 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": false,...
3
votes
4answers
88 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
80 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
42 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
45 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
70 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 ...
4
votes
1answer
133 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 MY-...
0
votes
1answer
41 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
517 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
27 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
Wed_Dec_30_16:00:41_PHT_2015....
2
votes
2answers
94 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
127 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
380 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 ...