String manipulation: extracting a part of a string, text replacement, formatting to a given width, etc.
0
votes
1answer
23 views
Search and replace multiple strings in a script using vim(search and replace) commands without sed?
A script that creates a file for weather precipitation is,
#!/bin/ksh
x=10
b=snow
c=rain
d=snow
e=hail
echo "$x,$b,$c,$d,$e" > weatherfile
exit 0
given
3,8s/snow/dry/g
For the search and ...
1
vote
2answers
33 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
370 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
40 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
35 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
31 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
260 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
295 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
29 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
36 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
44 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
31 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
55 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
123 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
49 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
164 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
3answers
117 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
33 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, ...
2
votes
2answers
32 views
How to transform string with character codes into quoted form
I have a string like New\x20Folder. Special characters is represented by their codes. I'd like to transform the string into quoted form: "New Folder".
The only available tools are bash and bunch of ...
0
votes
2answers
61 views
how to split the string after and before the space in shell script?
I am having a variable which shows on echo like this
$ echo $var
129 148
I have to take only 129 as output.
How will I split 129 and 148?
1
vote
2answers
22 views
Substituting file extensions on a variable
I'd like to do the following :
create a .tex and directly show it after compilation with evince, ideally it would be like this:
showpdf file.tex
so I wrote
#!/bin/sh
pdflatex $1 && ...
2
votes
3answers
95 views
Comparing strings
I am having trouble comparing two strings. Even though they are identical when printed, my if statement returns false.
#!/bin/bash
string1="HDMI"
string2="PC Speaker"
hash=$(pacmd list-sinks | ...
9
votes
1answer
229 views
When can I edit strings in an executable binary?
I have an executable binary; let's call it a.out. I can see the binary contains strings
$ strings a.out
...
/usr/share/foo
....
I need to change the string /usr/share/foo to /usr/share/bar. Can I ...
0
votes
3answers
52 views
How can I capture the common characters between two strings
I'm looking to compare two strings and capture the common part e.g.
FILE0000010985.LOG
FILE0000010999.LOG
FILE0000011000.LOG
If I compare 1 to 2, I want the output to be ...
2
votes
2answers
88 views
How to extract the positive numbers in the first column from an output as in the question?
I am running Ubuntu 14.04.1 LTS 64-bit with Bash 4.3.11(1)-release I have a program called harminv producing output as follows:
$ h5totxt hsli0.126.h5 | harminv -vt 0.1 -w 2-3 -a 0.9 -f 200
# ...
0
votes
1answer
89 views
Reading a string till a key word and replacing from there with another string
I am new to UNIX and am practising shell scripting. I have an application where I need string searching and replacement.
There are some lines in a file 1 and file 2. I have a search key word. If the ...
1
vote
2answers
56 views
Why quotes are retained in string variables when surrounded by single quotes?
I needed to retain the double quotes around a string variable defined in bash in order to be able to pass it to a dialect of the Scheme programming language. My question is why are the double quotes ...
1
vote
2answers
112 views
How to extract a part of file name in unix/linux shell script
My file name is Oracle.2347263_testing_152607.csv
I tried this :
echo Oracle.2347263_testing_152607.csv | sed -e 's/.*G.//' \-e 's/.csv//'
but I didn't get the result I expected.
I want to ...
-4
votes
2answers
68 views
how to find a file extension with multiple dots [duplicate]
I'd like to extract the extension out of a filename with multiple dots.
For instance:
gbamidi-v1.0.tar.gz
I should get "tar.gz", not "gz" or "0.tar.gz".
I prefer a solution not relying on bash ...
1
vote
5answers
712 views
How to use grep and cut in script to obtain website URLs from an HTML file
I am trying to use grep and cut to extract URLs from an HTML file. The links look like:
<a href="http://examplewebsite.com/">
Other websites have .net, .gov, but I assume I could make the cut ...
0
votes
4answers
202 views
How do i change the case (upper and lower case) of strings in my variable?
"Enter test: "
read test
if [[ $test == "a" ]]; then
echo "worked"
else
echo "failed"
fi
It's a simple illustration of test i'm doing, but if i enter "A", it will fail, is there anything i ...
1
vote
1answer
87 views
script to parse integer and convert it to char code
I am noob on Unix. I've a very unique problem where and really appreciate your help. I need to parse string containing hash code value and convert the hash code is equivalent char representation. Here ...
2
votes
3answers
190 views
How can I insert the contents of a file into a string in bash
I would like to append the contents of a multi-line text file after a particular line in a string. For example, if the file file.txt contains
line 1
line 2
I'd like do so something like printf ...
5
votes
1answer
85 views
Escaping strings in associative arrays (bash)
I am writing a script which executes a number of programs with arguments. To simplify it as much as possible, consider the following example:
programs=( "ls" "echo" )
declare -A parameters
...
0
votes
1answer
32 views
How do i create a function to test if the input contains any character? [duplicate]
I have this set of example here to test if the user has keyed in any data after he presses enter.
echo -n "Type a digit or a letter > "
read character
blank=""
if [ "$character" != "$blank"]; ...
-1
votes
2answers
41 views
extract line from same string
I have a table like this:
and a file containing 2 string from the third column as:
apple
potato
i would like to extract the header and all the line that contain the string apple and potato to ...
1
vote
2answers
57 views
Modifying part of a string in bash script
I have a file named variables.f90 , having many lines defining different variables, as follows ::
integer::n_monomer=6800
real*8::rx=5.0d0
#... randomly integer and real numbers defined
...
3
votes
8answers
213 views
bc output binary as nibbles separated by whitespace
I'm debugging code which contains quite a few bit shift operations, and I'm using bc a lot to look at what's happening on the bit level.
Here's what I use:
$ echo 'obase=2;598980975283696640' | bc
...
2
votes
0answers
44 views
Generate array of strings combined with number
I would like to generate a local array in a shell/bash script that consists of the following elements: JF-1998, JF-1999, JF-2000,... , JF-2011. That is, a string JF- combined with the years from 1998 ...
0
votes
2answers
50 views
Copying two words from a string into two variables
I want to get the variables $color and $number from a string that in general is like this: "something, numColor (number)". The color might be W, U, B, R, G. If there is no color the variable color ...
2
votes
1answer
67 views
Why string comparision results always false
I am trying to do a one time check about apache's mod-status page for updates
like this(this is just a test script):
firstcontent=$(lynx -dump http://some-server/server-status-page)
echo ...
2
votes
4answers
1k views
Extracting the second word from a string variable
I have a string "rtcpOnNbActive true" stored in a variable x. I want to extract "true" as substring and store in a variable. How can I do this?
1
vote
6answers
146 views
How to convert dashed string to spaces and caps?
Given the string foo-bar, what is a good way to convert it to Foo Bar?
Using Bash, OSX.
0
votes
2answers
146 views
Bash regex string manipulation bug
I have an issue with bash and its regex match ability that I for now think is a bug in bash but could also be me having missed something obvious. I hope it is the latter.
I have made this function in ...
0
votes
1answer
58 views
Issue finding lines using grep in UNIX [closed]
I am trying to use a .txt file with around 5000 patterns (spaced with a line) to search through another file of 18000 lines for any matches. So far I've tried every form of grep and awk I can find on ...
3
votes
4answers
382 views
Command to remove a portion of JSON data from each line?
I have test.json file with different lengths of rows. Some fictitious example:
{ a: 123, b: sd, c: x45, d: 1, e: '' }
{ a: 5, b: bfgg, c: x4c, d: 31, e: '' }
I want to cut the whole substring after ...
4
votes
3answers
340 views
Change case of n-th letter in a string
I want to change the case of the n-th letter of a string in BASH (or any other *nix tools, e.g. sed, awk, tr, etc).
I know that you can change the case a whole string using:
${str,,} # to lowercase
...
11
votes
3answers
802 views
Get all possible combinations of a word in lower/uppercase letters
I want to write a bash script to print all possible lower and upper case permutations of a certain word, e.g. harley:
harley
harleY
harlEy
harLey
...
HARLey
HARLEy
HARLEY
My naive solution is to ...
2
votes
3answers
176 views
Print total free memory from `free -m`
free -m currently puts out something like this.
I would like to know using grep/awk how we can get the total free amount. i.e. 9083