String manipulation: extracting a part of a string, text replacement, formatting to a given width, etc.
15
votes
2answers
712 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
20 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 ...
1
vote
1answer
15 views
Storing integer values from string in rrd
I want to collect stats from my DSL modem every 10 minutes. According to lots of websites it's best to use rrd for this. My modem(TD-W8968) struggles with SNMP so I've made an expect script to pull ...
0
votes
0answers
18 views
Issues with storing an echo of a date conversion into a string variable inunix
Apologies but this question comes from a question previously raised: How can I convert a local date-time into UTC date-time? Toby has been absolutely great helping me with some conversion string-->BST ...
0
votes
1answer
21 views
replacing first link of a text “awk equivalent of sed command”
I wanted to replace the first line of a text with sed it should be like
sed -i.bak "1 s/^.*$/"CompoundState\tMethod\tApproach\tS^2\tEnergy\tPath"/" awk.xls
but I also need to run it on OSX but it ...
0
votes
0answers
36 views
Subtract a Number from variable which contains a sentence
This is extended question from
http://stackoverflow.com/questions/32081168/reading-a-log-file-with-different-sequences-using-shell-script
I'm new to shell. I've written a shell script a code with ...
0
votes
0answers
36 views
Concatenating two strings cuts string
I extract two numerical strings from a textfile and and want to combine them to create a new string. The new string will be a filename.
This is the textfile dates.dat:
378464,2015-01-31
...
0
votes
1answer
45 views
Grep all strings from several binary files into one txt file
I've decided to play around dumping all process memory of couple utilities in order to find out, how much trace of sensitive info an attacker could theoretically obtain from app's memory.
I've found ...
0
votes
1answer
45 views
Find part of String and replace entire string in a file
I have a file named A.xml which contains a strings such as "Ticket_Release1" and "Ticket_V2".
I want to find and replace all the strings in the file starting with Ticket_ with something called ...
-1
votes
1answer
35 views
Comparing folder name to line from text file
I'm attempting to compare the name of a directory to a line in a text file. However even when the name of the directory and the line in the text file are the same I can't seem to get them to be ...
1
vote
5answers
94 views
How to get all numbers out of a string and add them?
I have to parse a .xml file generated to summarize the results of a running a testSuite on some software. In a line I have, for example:
<Summary failed="10" notExecuted="0" timeout="0" ...
8
votes
4answers
395 views
How to assert that a string has a newline character and, if so, remove it
I have a string that is the result of some operation I have no control over. When I print this variable using echo, I get:
echo $myvar
hello
However, when I do
if [ $myvar = "hello" ]; then
...
4
votes
3answers
70 views
Go from a String to an Array of “words” in Bash
I need to go from a string to an array where each entry is each word on that string. For example, starting with:
VotePedro="Vote for Pedro"
I need the array:
Vote
For
Pedro
Which I should then ...
0
votes
1answer
22 views
Remove Ubuntu strings from Mini install
I have an assignment to remove a string from Ubuntu Mini v. 15.04. How would I remove the Ubuntu String on the top? It says "Ubuntu 15.04 hostname tty1". I just want to remove Ubuntu 15.04 string.
...
0
votes
1answer
18 views
Substituting the value with spaces for a variable in bash script - the correct way
I have a compilation script that can compile the program with and without the debug symbols.
I want to ask via bash prompt ( read ) the user to choose whether he wants a debug version or not with ...
2
votes
3answers
47 views
Find strings in file1, count occurrences in file2
I have file1.txt with string values such as
New Drug Application
Drug Product
Dosing instructions
I need to count how often these strings occur in file2.txt with data such as
Regulatory New Drug ...
-3
votes
2answers
44 views
sed or awk to get a string
I have a shell script with
string=<deploymentTargets xmi:type="appdeployment:ClusteredTarget" xmi:id="ClusteredTarget_143378365
7353" name="cluster1"/>
I want the value between name=" and ...
2
votes
3answers
182 views
extract string between double quotes
I have requirement that i need to extract string using quotes. My string as follows.
"abcd efgh" "ijkl mnop" "qrst uvwxyz"
can you help me to get the string between second double quotes(ijkl mnop) ...
-1
votes
1answer
148 views
How create calculator in linux script?
I am trying to create a calculator:
echo "What is your number?"
read n1
echo "what is your second number?"
read n2
echo "what do you want to do?"
echo "1. add"
echo "2. subtract"
echo "3. divide"
...
3
votes
2answers
212 views
Substitute for loop variable in a string pattern
I am trying to iterate over an array of file names and substitute the file name inside an absolute path. The code is,
#!/bin/bash
jsArray=(moment.js datatable.js jquery.js jquery.tmpl.js dt_jq_ui.js ...
0
votes
1answer
61 views
Jquery like templates for formatting strings in bash
#!/bin/bash
rm all
for f in assets/css/*.css;
do
printf "<style type='text/css' >\n" >> all
cat $f >> all
printf "</style>\n <!-----$f---->" >> all
echo "$f ...
0
votes
3answers
208 views
get value after specific word
I have this file
1 deiauk David Smith from California 12 58
2 edvin from Nevada 12 5 8 95 2 48 5
3 jaco My Name Is Jacob I'm from NY 5 6 845 156 585
4 from Miami
And I need to get values after ...
2
votes
2answers
516 views
Awk: match exact string in line
I have this file
a deiauk Biking US 200 G
b kespaul 202 A
c deiauk NY 222 5 Z
And I want to match the exact string 200 using awk.
So my result should be
a deiauk Biking US 200 G
Here's my code
...
0
votes
2answers
57 views
Search and replace with a regular expression in vi?
I would like to replace a string with regular expression that appears in multiple lines with the character "x":
The problem is to replace words with the x in front that has an integer after the x, ...
1
vote
1answer
95 views
Why does bash try to execute strings in a string substitution?
My script is supposed to take input from a pipe and replace newline characters with commas, using the bash's string substitution:
#! /bin/bash
read -d -r input
echo $input
$input=${input//\n/,}
...
2
votes
2answers
53 views
Extra space with counted line number?
I count the number of lines of my file with this command on OSX:
nl=$(wc -l < ~/myfile.txt)
Say, nl turns out to be 100. Now, I wish to use the result nl in another command, but weirdly,
echo ...
2
votes
3answers
90 views
How can I prepend and append to each member of an array?
I have an array:
CATEGORIES=(one two three four)
I can prepend to each array member using parameter expansion:
echo ${CATEGORIES[@]/#/foo }
I can append to each array member the same way:
echo ...
4
votes
4answers
126 views
Stable sort a file by presence of ordered substrings on each line
I have a list of sound sources that I'm processing with a script. An example would be:
alsa_input.usb-AVEO_Technology_Corp._USB2.0_Camera-02-Camera.analog-mono
...
0
votes
1answer
54 views
Parsing string on multiple parts without separator
115:wc -l:find . -iname "*test*":find /tmp/ -iname "*test*"
I would like to parse the string above.
In my case, a string consists of 4 columns (every command has : separator) but it can be much ...
0
votes
1answer
63 views
Take output field data string into variable
I want to grep for some specific lines from a log file, and then capture a specific part of that output in a variable and use it in other commands.
The grep command I have is the following, where $1 ...
1
vote
2answers
70 views
Apply a filter to the value of a variable using backticks
Sorry for asking such a simple question, but reading about this issue on the internet has proven fruitless. I'm trying to better understand the grave/backtick operator and I've been doing okay so far ...
1
vote
1answer
410 views
Using sed to replace a string with special chars with another string with special characters
I'm trying to automate switching out a bash prompt for another in .bashrc
Original String:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
...
1
vote
2answers
47 views
How can I properly use the 'sed' s command in this particular scenario
There's a very simple question that's been bugging me about the sed command. Reading the documentation about this command has left me confused (mostly because English isn't my primary language).
So, ...
13
votes
6answers
504 views
/bin/dash: check whether $1 is a number
What would be the best way to check whether $1 is an integer in /bin/dash ?
In bash, I could do:
[[ $1 =~ ^([0-9]+)$ ]]
But that does not seem to be POSIX compliant and dash does not support that
2
votes
2answers
146 views
how to strip the last slash of the directory path?
I have a script which requires an directory as one argument.
I want to support the two form: one is like
a/b/c
(no slash at the end) and another is like
a/b/c/
(has slash at the end).
My ...
2
votes
3answers
60 views
Extract a part of a string in a bash script
I have a variable with some text in it. I need to get a specific bit of information out of it. For example I have
export OUTPUT="Running \"protractor:admin\" (protractor) task Using ChromeDriver ...
2
votes
1answer
92 views
I have a multi-line string that I need to iterate over, but I'm not using bash. Advice?
I'm using Dash because this is to run on my rPi. Bash is not an option.
I'm rewriting a Bash script to work with Dash. I'm hoping to make it as POSIX-compliant (portable) as possible. The thing is ...
4
votes
4answers
1k views
Split single string into character array using ONLY bash
I want to split 'hello' into h e l l o in an array using only bash, I could do it in sed with sed 's/./& /g' but I want to know how to split a string into an array in Bash when I do not know what ...
0
votes
0answers
10 views
Recursively replace a specific occurrence of a string in some files [duplicate]
Inside a folder, there are in certain files, a specific URL string (that is equal in all of them), that I need to replace with another URL string.
What can I do?
4
votes
4answers
445 views
How do I extract parts of a string to variables?
I have a line from some file like this:
attempting to create a 512^3 level (with Dirichlet BC) using a 16^3 grid of 32^3 boxes and 800 tasks...
I want to extract the 512^3, 16^3, 32^3 and 800 four ...
0
votes
1answer
51 views
replacing multi line string with another multi line string using sed
for eg.
input :
// copyright
package com.base
import com.base
import com.base
...
i want to replace the pattern "// copyright\n\n^package.*" with another string
i'm trying to do using
sed -e ...
1
vote
1answer
63 views
List all regular files containing (in their name, directory or their content) a specific (sub)string
Like in the title, and also I want to print the number of the line in each (text) file where the substring occurs.
So far I have:
find /sys | grep "filesystem"
But it appears to only look at the ...
-2
votes
1answer
61 views
How do I insert a character in front of a string? [closed]
For example I have QQQQQ111025 in a middle of a line. Where 1 can be any character numerical or alphabetical.
I need to insert 1 in the front of the QQQQQ or I need to replace the QQQQQ with 1. Keep ...
1
vote
2answers
38 views
Problem with using expr
I've got two strings
str1="( 1 + 2 + 3 + 4 + 5 ) / 3 + 5"
and
str2="( 1 + 2 + 3 + 4 + 5 ) / 3 + 5 * 2"
The command
result=` expr $str1`
returns a correct value, while
result=` expr $str2`
...
1
vote
2answers
68 views
Splitting the working directory in a bash script
If a do:
IFS="/" read -ra PARTS
And type in a path manually, it creates the array "PARTS" as hoped, however:
IFS="/" read -ra PARTS <<< $(pwd)
creates an array with a single element, ...
3
votes
3answers
206 views
Bash : Native way to get rid of quotation around each array member
I read an array from another script. This array needs to put " " around all array members since some members are empty.
in_file=./data
vector=($(./readdata.sh 0 $in_file))
for index in ${!vector[@]}
...
1
vote
1answer
61 views
Pattern matching and removal of that pattern using sed
I have 2 files with the following names:
File 1: RvA_X-IRB-bil-CA101-0+010000-20150327212332-055582-P
File 2: RvA_X-IRB-bil-CA101-1+020000-20150327212332-055582-P
I am using this command to match ...
4
votes
3answers
350 views
Get last part of string after hyphen
Is there a simple command line to extract the last part of a string separated by hyphens? E.g., I want to extract 123 from foo-bar-123.
3
votes
2answers
286 views
Shell test to find a pattern in a string
I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path.
if [[ $line =~ ...
1
vote
2answers
76 views
Read file remove spaces and store in array
I have a file with the following content:
list:
blue,none
red,none
orange,plot
baseball , none
university,none
school,none
desk,plot
monitor,none
earphone,none
I need to read this file, ...