Sponsored Content
Top Forums Shell Programming and Scripting Trouble with passing Variable from bash to awk gsub command Post 302580561 by Nostyx on Friday 9th of December 2011 12:27:12 AM
Old 12-09-2011
Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing.


Code:
#!/bin/bash
echo "Enter Username"
read Username

awk -F: -v var=${Username} '/^var:/ {gsub(/foo/,"bar", $2); print}' FILENAME

Have already tried...

Code:
awk -F: -v var=${Username} "/^$var:/" '{gsub(/foo/,"bar", $2); print}' FILENAME

awk -F: -v var=${Username} "/^var:/" '{gsub(/foo/,"bar", $2); print}' FILENAME

awk -F: -v var="$Username" "/^var:/" '{gsub(/foo/,"bar", $2); print}' FILENAME

Desired output is to have the script replace "foo" with "bar" in field 2 of FILENAME only on lines beginning with Username entered into read.


After this step the idea is to replace "foo" with ANY data that's in the specified Field, and the Field number to also be specified by the user via a read command.
So if anyone knows a simpler command for doing that all in 1 go that'd be much appreciated too.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

passing variable values to awk command

Hi, I have a situation where I have to specify a different value to an awk command, I beleive i have the gist of this done, however I am not able to get this correct. Here is what I have so far echo $id 065859555 This value occurs in a "pipe" delimited file in postition 8. Hence I would... (1 Reply)
Discussion started by: jerardfjay
1 Replies

2. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

3. Shell Programming and Scripting

Passing Bash variable to javascript

How do I pass a bash variable to a javascript? I've tried #!/bin/bash echo "Content-type: text/html" echo "" echo "<html>" echo "<head>" counter=0 echo '<script> window.parent.document.forms.counter.value = "$counter"; </script>' I have an iframe script which I am trying to pass a... (3 Replies)
Discussion started by: numele
3 Replies

4. Shell Programming and Scripting

awk's gsub variable in replacement

I been trying to figure out how to use element of array as a replacement pattern. This works as I expected: $ echo "one two three" | awk '{ gsub(/wo/,"_BEG_&_END_",$2); print }' one t_BEG_wo_END_ three $ echo "one two three" | awk '{ tmp="foo"; gsub(/wo/,"_BEG_" tmp "_END_",$2);... (5 Replies)
Discussion started by: mirni
5 Replies

5. Shell Programming and Scripting

Trouble with passing variable to sed

Here is my code #!/bin/bash username=gnowicki sed '$s/$/ $username/' < sshd_config 1 <> sshd_config what this is supposed to do is take the name gnowicki and put it at the end of the last line of the sshd_config and it works except not using the variable, if I put the name "gnowicki" where... (2 Replies)
Discussion started by: slufoot80
2 Replies

6. Shell Programming and Scripting

Passing string as variable(s) in bash

I'm trying to write a basic bash script that takes input you give (what directory, if any, what name, if any ....) and passes the information to find. I'm trying to just create a string with all variables and then pass it to find. So far I have this extremely simple: #!/bin/bash -f ... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

7. Shell Programming and Scripting

Assign awk gsub result to a variable

Hello, I have searched but failed to find what exactly im looking for, I need to eliminate first "." in a output so i can use something like the following echo "./abc/20141127" | nawk '{gsub("^.","");print}' what i want is to use gsub result later on, how could i achieve it? Let say... (4 Replies)
Discussion started by: EAGL€
4 Replies

8. Shell Programming and Scripting

Passing variable from bash to perl script

Hi All, I need to pass a variable from bash script to perl script and in the perl script i am using those variables in the sql query but its giving error : Use of uninitialized value $ENV{"COUNTRYCD"} in concatenation (.) or string at /GIS_ROOT/custom/tables/DBread_vendor.pl line 50. Can ... (6 Replies)
Discussion started by: NileshJ
6 Replies

9. Shell Programming and Scripting

File creation using awk and gsub command

I have input file 04000912|100:|||||]|101:||]|creDate:1451876825000|1441324800000:]|1444003200000:]|1446595200000:]|1449187200000:]|1451865600000:] I have to get output as below ID|Re_Date|Re_Value|Re_date 04000912|100|40.0|44 04000912|100|50.0|55 04000912|100|60.0|66... (4 Replies)
Discussion started by: shabeena
4 Replies

10. Shell Programming and Scripting

awk gsub command to replace multiple spaces

Hi Forum. I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful. Original Data: 4365 monte des source rue,, ,<TAB>trevost<TAB>QC Desired Data:... (1 Reply)
Discussion started by: pchang
1 Replies
DOODLE(1)						      General Commands Manual							 DOODLE(1)

NAME
doodle - a tool to search the meta-data in your files SYNOPSIS
doodle [OPTIONS] ([FILENAMES]*|[KEYWORDS]*) DESCRIPTION
doodle is a tool to index files. doodle uses libextractor to find meta-data in files. Once a database has been built, doodle can be used to quickly find files of which the meta-data matches a given search-string. This way, doodle can be used to quickly search your file sys- tem. Generally, the first time you run doodle you pass the option -b to build the database. Together with -b you specify the list of files or directories to index, for example $ doodle -b $HOME Indexing with doodle is incremental. If doodle -b is run (with the same database) twice it will update the index for files that were changed. doodle will also remove files that are no longer accessible. doodle will NOT remove files that are still present but no longer specified in the argument list. Thus invoking either $ doodle -b /foo /bar # or $ doodle -b /foo ; doodle -b /bar will result in the same database containing both the index for /foo and /bar. Note that the only way to only un-index /foo at this point is to make /foo inaccessible (using for example chmod 000 /foo or even rm -rf /foo) and then run doodle -b again. In networked environments, it often makes sense to build a database at the root of each file system, containing the entries for that file system. For this, doodle is run for each file system on the file server where that file system is on a local disk, to prevent thrashing the network. Users can select which databases doodle searches. Databases cannot be concatenated together. Once the files have been indexed, you can quickly query the doodle database. Just run $ doodle keyword to search all of your files for keyword. Note that only the meta-data extracted by libextractor is searched. Thus if libextractor does not find any meta-data in the files, you may not get any results. You can use the option -l to specify non-standard libextractor plugins. For example, doodle could be used to replace the locate tool from the GNU findutils like this: $ alias updatedb="doodle -bn -d /var/lib/doodle/doodle-locate-db -l libextractor_filename /" $ alias locate="doodle -d /var/lib/doodle/doodle-locate-db" OPTIONS
-a NUMBER, --approximate=NUMBER do approximate matching with mismatches of up to NUMBER letters -b, --build build the doodle database (passed arguments are directories and filenames that are to be indexed). In comparison with GNU locate the doodle binary encapsulates both the locate and the updatedb tool. Using the -b option doodle builds or updates the database (equivalent to updatedb), without -b it behaves similar to locate. -d FILENAME, --database=FILENAME use FILENAME for the location of the database (use when building or searching). This option is particularly useful when doodle is used to search different types of files (or is operated with different extractor options). Using this option doodle can be used to build specialized indices (i.e. one per file system), which can in turn improve search performance. When searching, you can pass a colon-separated list of database file names, in that case all databases are searched. Note that the disk-space consumption of a single database is typically slightly smaller than if the database is split into multiple files. Nevertheless, the space-savings are likely to be small (a few percent). You can also use the environment variable DOODLE_PATH to set the list of database files to search. The option overrides the environment variable if both are used. If the option is not given and DOODLE_PATH is not set, "/var/lib/doodle" is used. -e, --extract print the extracted keywords for each matching file found. Note that this will slow down the program a lot, especially if there are many matches in the database. Note that if the options given for libextractor are different than the options used for building the index the results may not contain the search string. -f, --filenames include filenames (full path) in the set of keywords -h, --help print help page -i, --ignore-case be case-insensitive -l LIBRARIES, --library=LIBRARIES specify which libextractor plugins to use (for building the index with -b or for printing information about files with -e) -L FILENAME, --log=FILENAME log all encountered keywords into a log file named FILENAME. This option is mostly useful for debugging. -m LIMIT, --memory=LIMIT use at most LIMIT MB of memory for the nodes of the suffix-tree (after that, serialize to disk). Note that a smaller value will reduce memory consumption but increase the size of the temporary file (and slow down indexing). The default is 8 MB. -n, --nodefault do not load the default set of plugins (only load plugins specified with -l) -p, --print make a human-readable screen dump of the doodle database (only really useful for debugging) -P PATH, --prunepaths=PATH Directories to not put in the database, which would otherwise be. The environment variable PRUNEPATHS also sets this value. Default is "/tmp /usr/tmp /var/tmp /dev /proc /sys". This option can also be used when searching, in which case search results in the spec- ified directories will be ignored. -v, --version print the version number -V, --verbose be verbose ENVIRONMENT
DOODLE_PATH Colon-separated list of databases to search. Note that when building the database this path must either only contain one filename or the option -b must be used to specify the database file. Default is "/var/lib/doodle". PRUNEPATHS Space-separated list of paths to exclude. Can be overridden with the -P option. NOTES
Doodle depends on libextractor. You can download libextractor from http://gnunet.org/libextractor/. SEE ALSO
extract(1), slocate(1), updatedb(1), libextractor(3), libdoodle(3) LEGAL NOTICE
libdoodle and doodle are released under the GPL. REPORTING BUGS
Report bugs to mantis <https://gnunet.org/bugs/> or by sending electronic mail to <christian@grothoff.org> AUTHORS
doodle was originally written by Christian Grothoff <christian@grothoff.org>. AVAILABILITY
You can obtain the original author's latest version from http://grothoff.org/christian/doodle/. doodle Jan 1 2010 DOODLE(1)
All times are GMT -4. The time now is 11:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy