Programmatically modifying a specific line in a text file - unix

Help with what I suspect to be pretty simple would be much appreciated.
I want to be able to programmatically change a certain line in a configuration file. Specifically, the file looks like
...Other lines...
# The name of the database to mount
dbms.active_database=test.db
...Other lines...
I want to be able to replace "test.db" with "production.db", etc. Other programs may add or delete lines, so I can't count on a consistent line number, but there is no other line containing "dbms.active_database="
How might I best do this?
Thank you.

With sed:
sed 's/\(^dbms\.active_database=\)test\.db/\1production.db/' file

awk -v name0fNewDb="HEythere.db" 'BEGIN{FS=OFS="="}/dbms.active_database/{$2=name0fNewDb}1'
...Other lines...
# The name of the database to mount
dbms.active_database=HEythere.db
...Other lines...
If I understood problem statement right, this should help you in doing the changes only on the line which consist of "dbms.active_database". Where it will replace the right part of = . name0fNewDb is a variable to which you can assign any text of your requirement.

Related

using grep to print specific lines in another file

I want to use grep command to print only those lines that contains a specific string in another file. My file is in xsls format and looks like:
I only want to extract those lines that contain 5'UTR in Annotation column.
The command I use is:
grep 'UTR$' Lee2012.xslx > utr.txt
However, I end up getting an empty file. I'll appreciate if someone can explain what I am doing wrong. Insights will be appreciated. Thank you!
Your sixth column ends with UTR, not the whole line, that is why you get no results.
To get all lines where the sixth column ends with UTR, you can use
awk '$6 ~ /UTR$/' Lee2012.xslx > utr.txt
This assumes your column (field) separators are whitespace.

Terminal of unix can be without square brackets

Can i make look like root#arch$.
Instead if [root#arch~],
I don't like square braces.
Help me if you know i want to remove square[] bracess.
The prompt you're talking about is named PS1, and you can customise its appearance in your .bashrc file.
So edit that file and add something like the following:
PS1="\\u#\\h:\\w"
The next time you log in you should have your new appearance.
PS1="\[\e[1;31m\]\u\[\e[0;15m\]#\\h \\w "
Insert the above code in '~/.bashrc' file, whenever you open a terminal '~/.bashrc' file will be executed hence you get custom prompt.
Tinker with numbers above to get desired colors.

Hashing parts of file using sed

I am creating a script to hash some parts of the text. The point is, I would like to do it using sed. Here is how example string looks like:
..HASHSTART.......HASHSTART..HASHEND..HASHEND..HASHSTART.....
.....HASHEND....HASHSTART.........
I have three problems with it:
I would like to match "HASHSTART" and "HASHEND" properly. So for example if i will find first HASHSTART, then i would like to hash everything to first find of HASHEND, even if there is more HASHSTARTs between.
I would like to hash it even if there is a new line between HASHSTART and HASHEND.
If there is HASHSTART at the end and no HASHEND till end of the file, I would like to hash everything after.
By "hash" I mean here to change from . to #.
So with this input the output should be:
..HASHSTART##################HASHEND..HASHEND..HASHSTART#####
#####HASHEND....HASHSTART#########
Thank you very much for any help.
Like this?
awk -F 'HASHSTART' -vw=3 '{i=index($w,"HASHEND");if(i)print substr($w,1,i-1)}' file
-vw=3 postion (2,)

Striping out time components for data in a csv file with | seperated variables

A bit new to UNIX but I have a question with reagrds altering csv files going into a datafeed.
There are a few | seperated columns where the date has come back as (for example)
|07-04-2006 15:50:33:44:55:66|
and this needs to be changed to
|07-04-2006|
It doesn't matter if all the data gets written to another file. There are thousands of rows in these files.
Ideally, I'm looking for a way of going to the 3rd and 7th piped columns and taking the first 10 characters and removing anything else till the next |
Thanks in advance for your help.
What exactly do you want?
You can replace |07-04-2006 15:50:33:44:55:66| by |07-04-2006| using File IO.
This operates on all columns, but should do unless there are date columns which must not be changed:
sed 's/|\(..-..-....\) ..:..:..:..:..:..|/|\1|/g'
If you want to change the files in place, you can use sed's -i option.

replace a string in huge file by incrementing with it number of appearance in file?

I have a file of 500MB and it has strings :
string_1 ..... string_500,
I need generate the copy of this file which has :
string_501.......string_1000
I need to do this up to string_500000, what's the best way to solve this?
If it is literally as you describe, a constant string with variable suffixes then just generate the new file forgetting the old one.
If it's actually
wibble_1 something_2 that_3 changes_4 randomly_5
the I'd read and parse the thing in perl
If you just want to generate a sequence of strings (string_501 to string_500000), you can do this:
for i in `seq 501 500000`
do
echo string_${i}
done

Resources