General :: Get Part Of A String In Bash ?
Mar 10, 2011I've a string "this.is.a.name", and I would like to return "is.a.name". How can I do that in bash?
View 3 RepliesI've a string "this.is.a.name", and I would like to return "is.a.name". How can I do that in bash?
View 3 RepliesI would like to return the last part of a string in an array of strings in bash.
The array contains in each position the content below:
Code:
a.b.c
a.d.f
a
a.d
[Code].....
So if I'm given a location of a file like:
How can I just take the type of the file at the end? I know I can use strrchr() for a period to get the pointer to the period just before file type. Is there a build in string function that will just take the rest of the string from a certain point on forward in the string? I know it wouldn't be much work to make it myself, but I figured I would find out if it already existed before doing it.
I have two scripts, one of which is very long (around 11000 lines), and i need to run this two lines at a time within the other script. Is there any way to do this?
View 2 Replies View RelatedHere's the bash script:
Code:
FILES="/usr/sbin/accept
/usr/sbin/pwck
/usr/sbin/chroot
/usr/bin/fakefile
[code]....
Notice the extra" file size" lines in there? What's causing that? I'm trying to learn more bash skills. I have no experience with awk because I have been unable to understand it's basic necessity. But I thought maybe if I try it with some test scripts I might become more interested in using it more and expand my very limited capabilities.
I'm not used to using "sed", but I need to use it to extract part of a string piped to it.
However, the "1" mentioned in the tutorials I read doesn't extract the token between the two brackets and returns the whole input string:
Code:
/tmp# echo "before [myfile.txt] after" | sed 's/[(.+)]/1/' before [myfile.txt] after
I expected simply "myfile.txt".
I have a macro which I use with ROOT. In this macro I want to check if a part of string exist so I can ignore it inside a loop. So, inside a loop I want to have something like:
Code:
if (string == "pre_ti_data_bdt*" || string == "pre_ti_data_nn*")
continue;
but of course I cannot use * in this piece of code! How to do this trick in C++?
I have to create a bash script that takes an arbitrary length number from the command line, and add up each individual digit
Ex:
server> myscript.sh 123
server> 1 + 2 + 3 = 6
The problem I'm having is pulling out each character.
Is there a way in bash I can parse the input string for each character? I can't figure out a way to do this.
how do I split a string into an array?In this string:"this is a story"how do I split it by the space?
View 8 Replies View RelatedIn a file,
I need to remove a part of string: /o
string:
or
the string can be
here I need to remove /d2
So, I need to remove everything after when I get last /
I do this:
Code:
a@b:~$ export A=hi
a@b:~$ echo $A
hi
a@b:~$ bash -c "export A=blah; echo $A"
hi
a@b:~$
Why doesn't the bash command print the new value of $A? Is there a way to make it do so?
I would like to evaluate a postfix string using bash shell script,but I do not know how to start.
View 1 Replies View RelatedIs there any inbuilt functionality in Unix shell script so that i can able to convert lower case string input to an upper case? I dont want to use high level languages like java,python or perl for doing the job.
View 4 Replies View RelatedI have a file (.tmpfile) and inside it is a string which i only know part of, the rest being a random group of characters... I would like to know how to pull the whole string out of the file and into a variable.
View 13 Replies View RelatedI'm trying to write a script where I want to check if any of the parameters passed to a bash script match a string. The way I have it setup right now is if [ "$3" != "-disCopperBld" -a "$4" != "-disCopperBld" -a "$5" != "-disCopperBld" -a "$6" != "-disCopperBld"]but there might be a large number of parameters, so I was wondering if there is a better way to do this?EDIT:I tried this chunk of code out, and called the script with the option, -disableVenusBld, but it still prints out "Starting build". Am I doing something wrong?
while [ $# -ne 0 ]
do
arg="$1"
[code]....
(variable substitution?)
(parameter expansion?)
Code:
run_repeatedly()
{
NUM=0
while [ <irrelevant stuff here> ]
[Code]....
run_repeatedly "programX -o "./messy/path/output-$NUM.txt"" The echo inside the loop prints "...-$NUM.txt"; obviously I'm aiming to have bash substitute the iteration number so that I end up with many output files not 1.
I'm trying to split a string, to later iterate using a for loop like
Code:
for (( i=0; i<5; i++))
But, my script returns an array with the size 1.
Here's the script:
Code:
aver=$(grep "avg" A.txt | awk '{ print $2 }');
a=$(echo $aver | tr " " "
");
[Code]....
If I have files named like this:
abc_one.c
egx_two.c
tsf_two.c
[code]...
OK I have a simple script that does:
Code:
# Create temporary file:
pwFile="~/Tmp/temp.cnf"
echo "$password" > "$pwFile"
But I get an error message:
Code:
~/Tmp/temp.cnf: No such file or directory
I am using gnu bash 3.2I need to split the string into array like
a=this_is_whole_world.file # split [_.]
I need to split this on _ and . some thing like this
a[0]=this
a[1]=is
a[2]=whole
a[3]=world
a[4]=file
preferable using bash regex. if not sed is also ok.
I want to write a bash script, which will read two strings: firstname and surname, then the script will generate a username. For example: the user name for "Peter Brown" will be brownp.
My questions are:
1. how can I get first letter from variable $firstname?
2. how can I join the two strings together?
I have a program that loops over each word in a sentence. I need to append a constant to the beginning and end of each word. It works up until the last word on the line.
Code:
Output:
Here is the code:
Code:
How ever when I run this script I get the following error
Quote:
I just don't get it, I have racked my brain trying to figure out every combination of how I should write this if statement and I can't get it to work.
Code:
g echo ${mm[$j]}
4 BashNotes
[code]...
I am trying to remove everything before my string code...
View 9 Replies View RelatedI have the following function that does not iterate through the array I want to be able to do some manipulation on each element in the array[@].it appears the below array has only one item in the array whereas i want the array to have 3 items hence the loop three times printing the message Any ideas why this is not happening ?
function foo() {
name =$1
array=( "$2" )
[code]...
copy string a to string b and change string b with toupper() and count the chars
View 3 Replies View RelatedI'm looking for a script which is testing how complex an "added" string is, for example like the user is changing his password and check how complex it is, if it included letters (lower/upper case), numbers and other characters.Im doing this for password check, the user type's the password and must be 6 characters long, have upper case letters lets say and numbers and so on.If anyone knows where I could find some bash script which is doing this, it would be really cool.
View 2 Replies View RelatedI'm currently learning bash programming and am trying to create a program to maintain a list of people. Here is the code so far:
Code:
#!/bin/bash
# Namelist
clear
[code]....
As you can see, I attempted to build in a protective measure to prevent duplicate entries using "if grep -Fxq "$FN $LN" /home/andrea/namelist.txt", FN and LN being first name and last name as you can see in the first read operation. However, this fails to actually work. Demonstrations:
[URL]
It saves the details to the file rather than displaying the error message, or in other words, it doesnt check the file properly.
Bash 3.1.7
Code:
Code:
Code:
I think read A1 A2 makes A1, A2 string variables. Then, when A2 gets the value 01, '01' should be a string. But for some reason bash takes it as numeric. I know there are no types in bash.