Programming :: Bash Removing String From File?
Aug 7, 2010I am trying to remove everything before my string code...
View 9 RepliesI am trying to remove everything before my string code...
View 9 Replieswrite such script (bash script). I have some text file with name filename.txt I must check if this file contains string "test-string-first", I must cut from this file string which follows string "keyword-string:" and till first white-space and save it to some variable.
For example. File: PHP Code: PHP Code:
Start 15022011 Eng 12-3-42
SN1232324422 11 test-string-first
SN322211 securities
HH keyword-string:123456321-net mark (11-22)
[Code].....
I wanted to find and replace a string from a perl file. I have written a script in bash which runs the following command.
perl -pi -e "s/$findstring/$replacestring/" testfile
where as $findstring = print F_WC_TMP"$line
";
and $replaceString = $line = join ' ', split ' ', $line; print F_WC_TMP"$line
";
But when I am running the above command, i think it is replacing the $findstring with the above mentioned string and hence it contains a $line, it is looking for the variable $line and not finding the exact string. I am confused about how to search for a string that contains $ in it and replace it with another $string.
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 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 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]...
I'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.
I'm writing a bash script to auto run on boot in Tinycore.
This is a watered down version.
Code:
I need it to either not add the time stamp if the awk finds a duplicate or write over the time with the new time if awk finds a duplicate.
BTW this is all pretty much cut-and-paste scripting so please feel free to comment if you know a more elegant way.
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
My simple bash-script replaces --> by the HTML-entity for the right arrow. To be precise, it replaces --*> by → Until now, I used sed, for example:
Code: $ flight='AMS --> JFK'
$ echo "$flight" | sed -e 's/ --*> / → /g'
AMS → JFK With sed, -* matches zero or more dashes, because for sed the * is the Kleene Star matching zero or more instances of the previous element. So with sed, --*> matches exactly what I want:
Code:
->
-->
--->
---->
Because sed seems oversized for that task, I played around to accomplish the same with on-board means of bash without using sed. On first thought, this line looks like doing the same but it isn't:
Code: $ flight='AMS --> JFK'
$ echo "${flight// --*> / → }"
AMS → JFK As I recently learnt in this forum, this * isn't the Kleene Star. It is a multi-character wildcard matching any zero or more consecutive characters. So here, --*> matches:
Code:
->
-->
--->
---->
-<>
->>
-abc> And if you continue your flight to SEA, then the result is totally wrong because the * matches greedily:
Code: $ flight='AMS --> JFK --> SEA' $ echo "${flight// --*> / → }" AMS → SEA
The sed line above would produce the correct result and would match the dashes correctly.
Further to solved LQ thread Bash: how to populate a list of arbitrarily named files?, what is the functional difference between feeding a loop with process substitution and feeding it with a here string with embedded command substitution? ABSG pages: process substitution, here string and command substitution. This works
Code:
while IFS= read -r -d '' file
do
files+=("$file")
done < <(find $dir -type f -print0)
[code]....
Have a bash script which contains a line like this:
if [[ ${array[${last}]} =~ "screenpc.PRODUCTION.*" ]]
which WORKED as expected in bash 4.0.33 and now fails in 4.1.2
Instrumented the script to print the value of the left-hand side and it is exactly what is expected.
As noted above, this has been working fine until we installed Fedora 13 (kernel 2.6.33), and now it fails.
Tried setting shell 'extglob' to On with same results.
Did something change? Are there other shell/bash options that need to be set?this has the whole installation stopped!
This may be a basic bash array/string operation related question, but I couldn't find any direct answer. So here it goes:I have a lot of data sorted in various directories. All directories need same processing except for a special group of directories. I have a symbolic link of the script in discussion in each directory. I want the script to get the name of the current directory, check if that belongs to special group and do specific operations.So I get the name of the directory
Code:
mm=`basename `pwd``
Now the the group of directories that needs something different to be done, contains these
[code]...
I 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 Relatedcopy string a to string b and change string b with toupper() and count the chars
View 3 Replies View RelatedMy script.
This is may script:
Code:
Problem: Output file doest not exclude the values in grep -av
I have script that I'm working on that updates a username in all the files that are called blah.inc for my framework. since i host a bunch of these web apps i need to do it to all of them. so I need to figure out how to update these files automagically with out me watching it to call vim every time. heres what I have so far
Code:
This finds the files but now i need to figure out how to do s/bob/fred/g on those files.
I have a file in which I have string called "already there" in some line of it. I want that if the string is present enter some loop..
like
if(( grep "already there" $file is true ))
then
--
--
fi
But the thing is when I make grep it gives the whole line in which this string is present and gives some error: if[ grep "already there" file ]: command not found. I am very new to shell scripting.
I've found some scripts that replaces a string in a file but it's not quite working for me.
And I'm trying to replace a tag in an xml file that looks like this
So I ran a command line like this perl -w -i -p -e "s/xmlns="'http://mydomain.org/replacethese.xsd'">/>/g" testfile.xml
And I get a error output
Search pattern not terminated at -e line 1.
Found the script from this blog [url]
I want to write some code to search for a specific string in a text file, but without using grep command.
View 5 Replies View RelatedI know how to replace a particular instance (say 3rd one) of a word in a line using sed based on the sed one-liners. However I would like to replace a particular instance of a word in the entire file.
For example, here is a file:
Code:
John
Betty
Jack
Ron
Jack
Paul
So now I would like to replace the second instance of Jack (in red color) with "Rob" (for example). Not quite sure how to do that? I tried couple of things from here but they did not work.
I have question about replacing a string in a file.
How can I replace the printerb's 10.1.1.1 to something else(10.1.1.2, for example) without replace printera's 10.1.1.1 accidentally?
I have tried perl -e -pi "s/10.1.1.1/10.1.1.2/g" /etc/hosts. but, perl replace both 10.1.1.1 to 10.1.1.2.
I need to read a file and parse a string. I know in Perl there is the split command. Is there something similar to that in C++?
View 6 Replies View RelatedCode:
function forcd
{
[code]...
Quote:Originally Posted by topcatI would like to know how i can write a shell script to delete a line if a particular pattern exists?E.g. I have a text file with multiple lines. Say 1000s. in the following pattern.
username@email.com:149.0.3.4:1
username1@email.com:149.0.3.4:1
username1@email.net:149.0.3.4:1
username1@email.edu:149.0.3.4:1
If the patternusername@email.com exists then the line "username@email.com:149.0.3.4:1 should be deleted from the file.I have a very similar question but I need to delete one line in a file which matches one very precise instance of a string only. Let's assume I have a file composed of thousands of lines and let's call the file chap-secrets. Let's take the following sample entries:
Code:
#USERNAME SERVER PASSWORD IP
pp pptpd blahblah *
[code]....
I want to know how can I add something to a specific line.. the output would be something like:
abc
def 123
ghi
Search for string "def" and add something to that line.
I am using C. I have a fuction that returns a string
Code:
I need to format this string so that I can create a text file like so:
So I'm trying:
Code:
But I get errors like: